Update ScheduleWorkflowResult to schedule job AFTER workflow is suspended

This commit is contained in:
Sipke Schoorstra 2021-01-03 19:21:50 +01:00
parent 4a3ac3d3aa
commit 96464c988c
2 changed files with 29 additions and 9 deletions

View file

@ -2,8 +2,8 @@ using System.Threading;
using System.Threading.Tasks;
using Elsa.Activities.Timers.Services;
using Elsa.ActivityResults;
using Elsa.Persistence;
using Elsa.Services.Models;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
namespace Elsa.Activities.Timers.ActivityResults
@ -17,16 +17,20 @@ namespace Elsa.Activities.Timers.ActivityResults
public Instant ExecuteAt { get; }
public override async ValueTask ExecuteAsync(ActivityExecutionContext activityExecutionContext, CancellationToken cancellationToken)
protected override void Execute(ActivityExecutionContext activityExecutionContext)
{
var workflowInstance = activityExecutionContext.WorkflowExecutionContext.WorkflowInstance;
var workflowInstanceStore = activityExecutionContext.GetService<IWorkflowInstanceStore>();
var scheduler = activityExecutionContext.GetService<IWorkflowScheduler>();
var workflowInstanceId = activityExecutionContext.WorkflowExecutionContext.WorkflowInstance.Id;
var activityId = activityExecutionContext.ActivityBlueprint.Id;
var workflowBlueprint = activityExecutionContext.WorkflowExecutionContext.WorkflowBlueprint;
var executeAt = ExecuteAt;
if(workflowInstance.LastSavedAt == null)
await workflowInstanceStore.SaveAsync(workflowInstance, cancellationToken);
await scheduler.ScheduleWorkflowAsync(activityExecutionContext.WorkflowExecutionContext.WorkflowBlueprint, workflowInstance.Id, activityExecutionContext.ActivityInstance.Id, ExecuteAt, cancellationToken);
async ValueTask ScheduleWorkflowAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken)
{
var scheduler = workflowExecutionContext.ServiceScope.ServiceProvider.GetRequiredService<IWorkflowScheduler>();
await scheduler.ScheduleWorkflowAsync(workflowBlueprint, workflowInstanceId, activityId, executeAt, cancellationToken);
}
activityExecutionContext.WorkflowExecutionContext.RegisterTask(nameof(ScheduleWorkflowResult), ScheduleWorkflowAsync);
}
}
}

View file

@ -0,0 +1,16 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Activities.Timers.ActivityResults;
using Elsa.Events;
using MediatR;
namespace Elsa.Activities.Timers.Handlers
{
public class ScheduleWorkflows : INotificationHandler<WorkflowSuspended>
{
public async Task Handle(WorkflowSuspended notification, CancellationToken cancellationToken)
{
await notification.WorkflowExecutionContext.ExecuteRegisteredTasksAsync(nameof(ScheduleWorkflowResult), cancellationToken);
}
}
}