protected internal virtual ITimerJobEntity GetJobToDelete(ICommandContext commandContext) { if (timerJobId is null) { throw new ActivitiIllegalArgumentException("jobId is null"); } //if (log.DebugEnabled) //{ // log.debug("Deleting job {}", timerJobId); //} ITimerJobEntity job = commandContext.TimerJobEntityManager.FindById <ITimerJobEntity>(new KeyValuePair <string, object>("id", timerJobId)); if (job == null) { throw new ActivitiObjectNotFoundException("No timer job found with id '" + timerJobId + "'", typeof(IJob)); } // We need to check if the job was locked, ie acquired by the job acquisition thread // This happens if the the job was already acquired, but not yet executed. // In that case, we can't allow to delete the job. if (job.LockOwner is object) { throw new ActivitiException("Cannot delete timer job when the job is being executed. Try again later."); } return(job); }
/// <summary> /// /// </summary> /// <param name="otherJob"></param> /// <returns></returns> protected internal virtual ITimerJobEntity CreateTimerJobFromOtherJob(IAbstractJobEntity otherJob) { ITimerJobEntity timerJob = processEngineConfiguration.TimerJobEntityManager.Create(); CopyJobInfo(timerJob, otherJob); return(timerJob); }
protected internal virtual void SendCancelEvent(ITimerJobEntity jobToDelete) { if (Context.ProcessEngineConfiguration.EventDispatcher.Enabled) { Context.ProcessEngineConfiguration.EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, jobToDelete)); } }
public virtual object Execute(ICommandContext commandContext) { foreach (string jobId in jobIds) { IJobEntity jobToDelete = commandContext.JobEntityManager.FindById <IJobEntity>(new KeyValuePair <string, object>("id", jobId)); if (jobToDelete != null) { // When given job doesn't exist, ignore if (commandContext.ProcessEngineConfiguration.EventDispatcher.Enabled) { commandContext.ProcessEngineConfiguration.EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, jobToDelete)); } commandContext.JobEntityManager.Delete(jobToDelete); } else { ITimerJobEntity timerJobToDelete = commandContext.TimerJobEntityManager.FindById <ITimerJobEntity>(new KeyValuePair <string, object>("id", jobId)); if (timerJobToDelete != null) { // When given job doesn't exist, ignore if (commandContext.ProcessEngineConfiguration.EventDispatcher.Enabled) { commandContext.ProcessEngineConfiguration.EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.JOB_CANCELED, timerJobToDelete)); } commandContext.TimerJobEntityManager.Delete(timerJobToDelete); } } } return(null); }
public virtual object Execute(ICommandContext commandContext) { ITimerJobEntity jobToDelete = GetJobToDelete(commandContext); SendCancelEvent(jobToDelete); commandContext.TimerJobEntityManager.Delete(jobToDelete); return(null); }
public override void Execute(IExecutionEntity execution) { IJobManager jobManager = Context.CommandContext.JobManager; // end date should be ignored for intermediate timer events. ITimerJobEntity timerJob = jobManager.CreateTimerJob(timerEventDefinition, false, execution, TriggerTimerEventJobHandler.TYPE, TimerEventHandler.CreateConfiguration(execution.CurrentActivityId, null, timerEventDefinition.CalendarName)); if (timerJob != null) { jobManager.ScheduleTimerJob(timerJob); } }
protected internal virtual void LockJob(ICommandContext commandContext, ITimerJobEntity job, int lockTimeInMillis) { // This will trigger an optimistic locking exception when two concurrent executors // try to lock, as the revision will not match. var cl = new DateTime(commandContext.ProcessEngineConfiguration.Clock.CurrentTime.Ticks); cl.AddMilliseconds(lockTimeInMillis); job.LockOwner = asyncExecutor.LockOwner; job.LockExpirationTime = cl; }
public override void Execute(IExecutionEntity execution) { if (!(execution.CurrentFlowElement is BoundaryEvent)) { throw new ActivitiException("Programmatic error: " + this.GetType() + " should not be used for anything else than a boundary event"); } IJobManager jobManager = Context.CommandContext.JobManager; string timerConfig = TimerEventHandler.CreateConfiguration(execution.CurrentActivityId, timerEventDefinition.EndDate, timerEventDefinition.CalendarName); ITimerJobEntity timerJob = jobManager.CreateTimerJob(timerEventDefinition, interrupting, execution, TriggerTimerEventJobHandler.TYPE, timerConfig); if (timerJob != null) { jobManager.ScheduleTimerJob(timerJob); } }
/// <summary> /// /// </summary> /// <param name="timerEntity"></param> protected internal virtual void ExecuteTimerJob(IJobEntity timerEntity) { ITimerJobEntityManager timerJobEntityManager = processEngineConfiguration.TimerJobEntityManager; IVariableScope variableScope = null; if (!(timerEntity.ExecutionId is null)) { variableScope = ExecutionEntityManager.FindById <VariableScopeImpl>(timerEntity.ExecutionId); } if (variableScope == null) { variableScope = NoExecutionVariableScope.SharedInstance; } // set endDate if it was set to the definition RestoreExtraData(timerEntity, variableScope); if (timerEntity.Duedate != null && !IsValidTime(timerEntity, timerEntity.Duedate.GetValueOrDefault(DateTime.Now), variableScope)) { if (logger.IsEnabled(LogLevel.Debug)) { logger.LogDebug($"Timer {timerEntity.Id} fired. but the dueDate is after the endDate. Deleting timer."); } processEngineConfiguration.JobEntityManager.Delete(timerEntity); return; } ExecuteJobHandler(timerEntity); processEngineConfiguration.JobEntityManager.Delete(timerEntity); if (logger.IsEnabled(LogLevel.Debug)) { logger.LogDebug($"Timer {timerEntity.Id} fired. Deleting timer."); } if (!(timerEntity.Repeat is null)) { ITimerJobEntity newTimerJobEntity = timerJobEntityManager.CreateAndCalculateNextTimer(timerEntity, variableScope); if (newTimerJobEntity != null) { ScheduleTimerJob(newTimerJobEntity); } } }
/// <summary> /// /// </summary> /// <param name="timerJob"></param> public virtual void ScheduleTimerJob(ITimerJobEntity timerJob) { if (timerJob == null) { throw new ActivitiException("Empty timer job can not be scheduled"); } processEngineConfiguration.TimerJobEntityManager.Insert(timerJob); ICommandContext commandContext = Context.CommandContext; IActivitiEventDispatcher eventDispatcher = commandContext.EventDispatcher; if (eventDispatcher.Enabled) { eventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.TIMER_SCHEDULED, timerJob)); } }
/// <summary> /// /// </summary> /// <param name="timerJob"></param> /// <returns></returns> public virtual IJobEntity MoveTimerJobToExecutableJob(ITimerJobEntity timerJob) { if (timerJob == null) { throw new ActivitiException("Empty timer job can not be scheduled"); } IJobEntity executableJob = CreateExecutableJobFromOtherJob(timerJob); bool insertSuccesful = processEngineConfiguration.JobEntityManager.InsertJobEntity(executableJob); if (insertSuccesful) { processEngineConfiguration.TimerJobEntityManager.Delete(timerJob); TriggerExecutorIfNeeded(executableJob); return(executableJob); } return(null); }
public virtual object Execute(ICommandContext commandContext) { ITimerJobEntity job = commandContext.TimerJobEntityManager.FindById <ITimerJobEntity>(jobId); if (job != null) { job.Retries = retries; if (commandContext.EventDispatcher.Enabled) { commandContext.EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.ENTITY_UPDATED, job)); } } else { throw new ActivitiObjectNotFoundException("No timer job found with id '" + jobId + "'.", typeof(IJob)); } return(null); }
protected internal virtual void restoreTimerStartEvent(IProcessDefinition previousProcessDefinition, StartEvent startEvent, EventDefinition eventDefinition) { TimerEventDefinition timerEventDefinition = (TimerEventDefinition)eventDefinition; ITimerJobEntity timer = TimerUtil.CreateTimerEntityForTimerEventDefinition((TimerEventDefinition)eventDefinition, false, null, TimerStartEventJobHandler.TYPE, TimerEventHandler.CreateConfiguration(startEvent.Id, timerEventDefinition.EndDate, timerEventDefinition.CalendarName)); if (timer != null) { ITimerJobEntity timerJob = JobManager.CreateTimerJob((TimerEventDefinition)eventDefinition, false, null, TimerStartEventJobHandler.TYPE, TimerEventHandler.CreateConfiguration(startEvent.Id, timerEventDefinition.EndDate, timerEventDefinition.CalendarName)); timerJob.ProcessDefinitionId = previousProcessDefinition.Id; if (!ReferenceEquals(previousProcessDefinition.TenantId, null)) { timerJob.TenantId = previousProcessDefinition.TenantId; } JobManager.ScheduleTimerJob(timerJob); } }
/// <summary> /// /// </summary> /// <param name="job"></param> /// <returns></returns> public virtual ITimerJobEntity MoveJobToTimerJob(IAbstractJobEntity job) { ITimerJobEntity timerJob = CreateTimerJobFromOtherJob(job); bool insertSuccesful = processEngineConfiguration.TimerJobEntityManager.InsertTimerJobEntity(timerJob); if (insertSuccesful) { if (job is IJobEntity) { processEngineConfiguration.JobEntityManager.Delete((IJobEntity)job); } else if (job is ISuspendedJobEntity) { processEngineConfiguration.SuspendedJobEntityManager.Delete((ISuspendedJobEntity)job); } return(timerJob); } return(null); }
public virtual IJobEntity Execute(ICommandContext commandContext) { if (jobId is null) { throw new ActivitiIllegalArgumentException("jobId and job is null"); } ITimerJobEntity timerJob = commandContext.TimerJobEntityManager.FindById <ITimerJobEntity>(new KeyValuePair <string, object>("id", jobId)); if (timerJob == null) { throw new JobNotFoundException(jobId); } if (log.IsEnabled(LogLevel.Debug)) { log.LogDebug($"Executing timer job {timerJob.Id}"); } return(commandContext.JobManager.MoveTimerJobToExecutableJob(timerJob)); }
public override void Execute(IExecutionEntity execution) { if (!(execution.CurrentFlowElement is BoundaryEvent)) { throw new ActivitiException("Programmatic error: " + this.GetType() + " should not be used for anything else than a boundary event"); } if (timerEventDefinition.TimeDate == "-1" || timerEventDefinition.TimeCycle == "-1" || timerEventDefinition.TimeDuration == "-1") { ProcessEngineConfigurationImpl processConfig = Context.CommandContext.ProcessEngineConfiguration; var element = execution.CurrentFlowElement as BoundaryEvent; var beab = new BoundaryEventActivityBehavior(element.CancelActivity); beab.Trigger(execution, TriggerTimerEventJobHandler.TYPE, null); processConfig.ExecutionEntityManager.Delete(execution); //if (element.CancelActivity) //{ // IExecutionEntityManager executionEntityManager = processConfig.ExecutionEntityManager; // IExecutionEntity attachedRefScopeExecution = executionEntityManager.FindById<IExecutionEntity>(execution.ParentId); // processConfig.ExecutionEntityManager.Delete(attachedRefScopeExecution); // processConfig.ExecutionEntityManager.Delete(execution); //} } else { IJobManager jobManager = Context.CommandContext.JobManager; string timerConfig = TimerEventHandler.CreateConfiguration(execution.CurrentActivityId, timerEventDefinition.EndDate, timerEventDefinition.CalendarName); ITimerJobEntity timerJob = jobManager.CreateTimerJob(timerEventDefinition, interrupting, execution, TriggerTimerEventJobHandler.TYPE, timerConfig); if (timerJob != null) { jobManager.ScheduleTimerJob(timerJob); } } }
protected internal virtual IList <ITimerJobEntity> GetTimerDeclarations(IProcessDefinitionEntity processDefinition, Process process) { IJobManager jobManager = Context.CommandContext.JobManager; IList <ITimerJobEntity> timers = new List <ITimerJobEntity>(); if (CollectionUtil.IsNotEmpty(process.FlowElements)) { foreach (FlowElement element in process.FlowElements) { if (element is StartEvent startEvent) { if (CollectionUtil.IsNotEmpty(startEvent.EventDefinitions)) { EventDefinition eventDefinition = startEvent.EventDefinitions[0]; if (eventDefinition is TimerEventDefinition timerEventDefinition) { ITimerJobEntity timerJob = jobManager.CreateTimerJob(timerEventDefinition, false, null, TimerStartEventJobHandler.TYPE, TimerEventHandler.CreateConfiguration(startEvent.Id, timerEventDefinition.EndDate, timerEventDefinition.CalendarName)); if (timerJob != null) { timerJob.ProcessDefinitionId = processDefinition.Id; if (processDefinition.TenantId is object) { timerJob.TenantId = processDefinition.TenantId; } timers.Add(timerJob); } } } } } } return(timers); }
/// <summary> /// The event definition on which the timer is based. /// /// Takes in an optional execution, if missing the <seealso cref="NoExecutionVariableScope"/> will be used (eg Timer start event) /// </summary> public static ITimerJobEntity CreateTimerEntityForTimerEventDefinition(TimerEventDefinition timerEventDefinition, bool isInterruptingTimer, IExecutionEntity executionEntity, string jobHandlerType, string jobHandlerConfig) { ProcessEngineConfigurationImpl processEngineConfiguration = Context.ProcessEngineConfiguration; string businessCalendarRef = null; IExpression expression = null; ExpressionManager expressionManager = processEngineConfiguration.ExpressionManager; // ACT-1415: timer-declaration on start-event may contain expressions NOT // evaluating variables but other context, evaluating should happen nevertheless IVariableScope scopeForExpression = executionEntity; if (scopeForExpression == null) { //scopeForExpression = NoExecutionVariableScope.SharedInstance; } if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeDate)) { businessCalendarRef = DueDateBusinessCalendar.NAME; expression = expressionManager.CreateExpression(timerEventDefinition.TimeDate); } else if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeCycle)) { businessCalendarRef = CycleBusinessCalendar.NAME; expression = expressionManager.CreateExpression(timerEventDefinition.TimeCycle); } else if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeDuration)) { businessCalendarRef = DurationBusinessCalendar.NAME; expression = expressionManager.CreateExpression(timerEventDefinition.TimeDuration); } if (!string.IsNullOrWhiteSpace(timerEventDefinition.CalendarName)) { businessCalendarRef = timerEventDefinition.CalendarName; IExpression businessCalendarExpression = expressionManager.CreateExpression(businessCalendarRef); businessCalendarRef = businessCalendarExpression.GetValue(scopeForExpression).ToString(); } if (expression == null) { throw new ActivitiException("Timer needs configuration (either timeDate, timeCycle or timeDuration is needed) (" + timerEventDefinition.Id + ")"); } IBusinessCalendar businessCalendar = processEngineConfiguration.BusinessCalendarManager.GetBusinessCalendar(businessCalendarRef); string dueDateString = null; DateTime?duedate = null; object dueDateValue = expression.GetValue(scopeForExpression); if (dueDateValue is string) { dueDateString = (string)dueDateValue; } else if (dueDateValue is DateTime) { duedate = (DateTime)dueDateValue; } else if (dueDateValue is Nullable <DateTime> ) { //JodaTime support duedate = (DateTime?)dueDateValue; } else if (dueDateValue != null) { throw new ActivitiException("Timer '" + executionEntity.ActivityId + "' was not configured with a valid duration/time, either hand in a java.util.Date or a String in format 'yyyy-MM-dd'T'hh:mm:ss'"); } if (duedate == null && !string.IsNullOrWhiteSpace(dueDateString)) { duedate = businessCalendar.ResolveDuedate(dueDateString); } ITimerJobEntity timer = null; if (duedate != null) { timer = Context.CommandContext.TimerJobEntityManager.Create(); timer.JobType = JobFields.JOB_TYPE_TIMER; timer.Revision = 1; timer.JobHandlerType = jobHandlerType; timer.JobHandlerConfiguration = jobHandlerConfig; timer.Exclusive = true; timer.Retries = processEngineConfiguration.AsyncExecutorNumberOfRetries; timer.Duedate = duedate; if (executionEntity != null) { timer.Execution = executionEntity; timer.ProcessDefinitionId = executionEntity.ProcessDefinitionId; timer.ProcessInstanceId = executionEntity.ProcessInstanceId; // Inherit tenant identifier (if applicable) if (!string.IsNullOrWhiteSpace(executionEntity.TenantId)) { timer.TenantId = executionEntity.TenantId; } } } if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeCycle)) { // See ACT-1427: A boundary timer with a cancelActivity='true', doesn't need to repeat itself bool repeat = !isInterruptingTimer; // ACT-1951: intermediate catching timer events shouldn't repeat according to spec if (executionEntity != null) { FlowElement currentElement = executionEntity.CurrentFlowElement; if (currentElement is IntermediateCatchEvent) { repeat = false; } } if (repeat) { string prepared = PrepareRepeat(dueDateString); timer.Repeat = prepared; } } if (timer != null && executionEntity != null) { timer.Execution = executionEntity; timer.ProcessDefinitionId = executionEntity.ProcessDefinitionId; // Inherit tenant identifier (if applicable) if (!string.IsNullOrWhiteSpace(executionEntity.TenantId)) { timer.TenantId = executionEntity.TenantId; } } return(timer); }
/// <summary> /// /// </summary> /// <param name="timerEventDefinition"></param> /// <param name="interrupting"></param> /// <param name="execution"></param> /// <param name="timerEventType"></param> /// <param name="jobHandlerConfiguration"></param> /// <returns></returns> public virtual ITimerJobEntity CreateTimerJob(TimerEventDefinition timerEventDefinition, bool interrupting, IExecutionEntity execution, string timerEventType, string jobHandlerConfiguration) { ITimerJobEntity timerEntity = TimerUtil.CreateTimerEntityForTimerEventDefinition(timerEventDefinition, interrupting, execution, timerEventType, jobHandlerConfiguration); return(timerEntity); }
/// <summary> /// /// </summary> /// <param name="job"></param> public virtual void AddJob(ITimerJobEntity job) { acquiredJobs.AddOrUpdate(job.Id, job, (key, old) => job); }