Schedule activity until after Suspend action
This commit is contained in:
parent
38f883e103
commit
e00d41e0cf
|
|
@ -1,5 +1,4 @@
|
|||
using System.Threading.Tasks;
|
||||
using Elsa.Activities.Timers.Services;
|
||||
using Elsa.Activities.Timers.ActivityResults;
|
||||
using Elsa.ActivityResults;
|
||||
using Elsa.Attributes;
|
||||
using Elsa.Services;
|
||||
|
|
@ -12,14 +11,10 @@ namespace Elsa.Activities.Timers
|
|||
[Trigger(Category = "Timers", Description = "Triggers at a specified interval.")]
|
||||
public class Timer : Activity
|
||||
{
|
||||
private readonly IWorkflowInstanceManager _workflowInstanceManager;
|
||||
private readonly IWorkflowScheduler _workflowScheduler;
|
||||
private readonly IClock _clock;
|
||||
|
||||
public Timer(IWorkflowInstanceManager workflowInstanceManager, IWorkflowScheduler workflowScheduler, IClock clock)
|
||||
public Timer(IClock clock)
|
||||
{
|
||||
_workflowInstanceManager = workflowInstanceManager;
|
||||
_workflowScheduler = workflowScheduler;
|
||||
_clock = clock;
|
||||
}
|
||||
|
||||
|
|
@ -32,22 +27,14 @@ namespace Elsa.Activities.Timers
|
|||
set => SetState(value);
|
||||
}
|
||||
|
||||
protected override async ValueTask<IActivityExecutionResult> OnExecuteAsync(ActivityExecutionContext context)
|
||||
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
|
||||
{
|
||||
if (context.WorkflowExecutionContext.IsFirstPass)
|
||||
return Done();
|
||||
|
||||
var cancellationToken = context.CancellationToken;
|
||||
var workflowBlueprint = context.WorkflowExecutionContext.WorkflowBlueprint;
|
||||
var workflowInstance = context.WorkflowExecutionContext.WorkflowInstance;
|
||||
var executeAt = _clock.GetCurrentInstant().Plus(Timeout);
|
||||
|
||||
ExecuteAt = executeAt;
|
||||
ExecuteAt = _clock.GetCurrentInstant().Plus(Timeout);
|
||||
|
||||
await _workflowInstanceManager.SaveAsync(context.WorkflowExecutionContext.WorkflowInstance, cancellationToken);
|
||||
await _workflowScheduler.ScheduleWorkflowAsync(workflowBlueprint, workflowInstance.WorkflowInstanceId, Id, executeAt, cancellationToken);
|
||||
|
||||
return Suspend();
|
||||
return Combine(Suspend(), new ScheduleWorkflowResult(ExecuteAt.Value));
|
||||
}
|
||||
|
||||
protected override IActivityExecutionResult OnResume() => Done();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Elsa.Activities.Timers.Services;
|
||||
using Elsa.ActivityResults;
|
||||
using Elsa.Models;
|
||||
using Elsa.Services;
|
||||
using Elsa.Services.Models;
|
||||
using NodaTime;
|
||||
|
||||
namespace Elsa.Activities.Timers.ActivityResults
|
||||
{
|
||||
public class ScheduleWorkflowResult : ActivityExecutionResult
|
||||
{
|
||||
public ScheduleWorkflowResult(Instant executeAt)
|
||||
{
|
||||
ExecuteAt = executeAt;
|
||||
}
|
||||
|
||||
public Instant ExecuteAt { get; }
|
||||
|
||||
public override async ValueTask ExecuteAsync(ActivityExecutionContext activityExecutionContext, CancellationToken cancellationToken)
|
||||
{
|
||||
var workflowInstance = activityExecutionContext.WorkflowExecutionContext.WorkflowInstance;
|
||||
var workflowInstanceManager = activityExecutionContext.GetService<IWorkflowInstanceManager>();
|
||||
var scheduler = activityExecutionContext.GetService<IWorkflowScheduler>();
|
||||
await workflowInstanceManager.SaveAsync(workflowInstance, cancellationToken);
|
||||
await scheduler.ScheduleWorkflowAsync(activityExecutionContext.WorkflowExecutionContext.WorkflowBlueprint, workflowInstance.WorkflowInstanceId, activityExecutionContext.ActivityInstance.Id, ExecuteAt, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue