2022-01-18 19:28:48 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-02-01 19:45:11 +00:00
|
|
|
using Elsa.Modules.Scheduling.Activities;
|
|
|
|
|
using Elsa.Modules.Scheduling.Contracts;
|
|
|
|
|
using Elsa.Modules.Scheduling.Jobs;
|
2022-03-04 11:09:08 +00:00
|
|
|
using Elsa.Persistence.Entities;
|
2022-03-07 21:56:16 +00:00
|
|
|
using Elsa.Jobs.Contracts;
|
|
|
|
|
using Elsa.Jobs.Schedules;
|
2022-03-04 11:09:08 +00:00
|
|
|
using Elsa.Persistence.Extensions;
|
2022-01-18 19:28:48 +00:00
|
|
|
|
2022-02-01 19:45:11 +00:00
|
|
|
namespace Elsa.Modules.Scheduling.Services;
|
2022-01-18 19:28:48 +00:00
|
|
|
|
|
|
|
|
public class WorkflowBookmarkScheduler : IWorkflowBookmarkScheduler
|
|
|
|
|
{
|
|
|
|
|
private const string RootGroupKey = "WorkflowInstance";
|
|
|
|
|
private readonly IJobScheduler _jobScheduler;
|
|
|
|
|
|
|
|
|
|
public WorkflowBookmarkScheduler(IJobScheduler jobScheduler)
|
|
|
|
|
{
|
|
|
|
|
_jobScheduler = jobScheduler;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-04 11:09:08 +00:00
|
|
|
public async Task ScheduleBookmarksAsync(string workflowInstanceId, IEnumerable<WorkflowBookmark> bookmarks, CancellationToken cancellationToken = default)
|
2022-01-18 19:28:48 +00:00
|
|
|
{
|
|
|
|
|
var bookmarkList = bookmarks.ToList();
|
|
|
|
|
|
|
|
|
|
// Select all Delay bookmarks.
|
|
|
|
|
var delayBookmarks = bookmarkList.Filter<Delay>().ToList();
|
|
|
|
|
var groupKeys = new[] { RootGroupKey, workflowInstanceId };
|
|
|
|
|
|
|
|
|
|
// Schedule a trigger for each bookmark.
|
|
|
|
|
foreach (var bookmark in delayBookmarks)
|
|
|
|
|
{
|
2022-03-07 11:01:27 +00:00
|
|
|
var payload = JsonSerializer.Deserialize<DelayPayload>(bookmark.Data!)!;
|
2022-01-18 19:28:48 +00:00
|
|
|
var resumeAt = payload.ResumeAt;
|
2022-03-04 11:09:08 +00:00
|
|
|
var job = new ResumeWorkflowJob(workflowInstanceId, bookmark.ToBookmark());
|
|
|
|
|
var schedule = new SpecificInstantSchedule(resumeAt);
|
2022-03-15 12:01:10 +00:00
|
|
|
await _jobScheduler.ScheduleAsync(job, bookmark.Id, schedule, groupKeys, cancellationToken);
|
2022-03-04 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UnscheduleBookmarksAsync(string workflowInstanceId, IEnumerable<WorkflowBookmark> bookmarks, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var bookmarkList = bookmarks.ToList();
|
|
|
|
|
var delayBookmarks = bookmarkList.Filter<Delay>().ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var bookmark in delayBookmarks)
|
|
|
|
|
{
|
2022-03-15 12:01:10 +00:00
|
|
|
await _jobScheduler.UnscheduleAsync(bookmark.Id, cancellationToken);
|
2022-01-18 19:28:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|