Optimize timer activities to always execute when target time stamp lies in past

This commit is contained in:
Sipke Schoorstra 2020-12-20 20:22:03 +01:00
parent 8078e7bccc
commit f5f9c070ca
3 changed files with 10 additions and 2 deletions

View file

@ -49,6 +49,9 @@ namespace Elsa.Activities.Timers
ExecuteAt = executeAt;
if (executeAt < _clock.GetCurrentInstant())
return Done();
await _workflowInstanceStore.SaveAsync(context.WorkflowExecutionContext.WorkflowInstance, cancellationToken);
await _workflowScheduler.ScheduleWorkflowAsync(workflowBlueprint, workflowInstance.Id, Id, executeAt, cancellationToken);

View file

@ -21,11 +21,13 @@ namespace Elsa.Activities.Timers
{
private readonly IWorkflowInstanceStore _workflowInstanceStore;
private readonly IWorkflowScheduler _workflowScheduler;
private readonly IClock _clock;
public StartAt(IWorkflowInstanceStore workflowInstanceStore, IWorkflowScheduler workflowScheduler)
public StartAt(IWorkflowInstanceStore workflowInstanceStore, IWorkflowScheduler workflowScheduler, IClock clock)
{
_workflowInstanceStore = workflowInstanceStore;
_workflowScheduler = workflowScheduler;
_clock = clock;
}
[ActivityProperty(Hint = "An instant in the future at which this activity should execute.")]
@ -49,6 +51,9 @@ namespace Elsa.Activities.Timers
ExecuteAt = executeAt;
if (executeAt <= _clock.GetCurrentInstant())
return Done();
await _workflowInstanceStore.SaveAsync(context.WorkflowExecutionContext.WorkflowInstance, cancellationToken);
await _workflowScheduler.ScheduleWorkflowAsync(workflowBlueprint, workflowInstance.Id, Id, executeAt, cancellationToken);

View file

@ -32,7 +32,7 @@ namespace Elsa.Activities.Timers
if (context.WorkflowExecutionContext.IsFirstPass)
return Done();
if (Timeout == Duration.Zero)
if (Timeout <= Duration.Zero)
return Done();
ExecuteAt = _clock.GetCurrentInstant().Plus(Timeout);