| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Scheduling.Bookmarks; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Runtime.Entities; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Scheduling.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A default implementation of <see cref="ITriggerScheduler"/> that schedules bookmarks using <see cref="IWorkflowSched |
| | | 10 | | /// </summary> |
| | | 11 | | public class DefaultBookmarkScheduler : IBookmarkScheduler |
| | | 12 | | { |
| | | 13 | | private readonly IWorkflowScheduler _workflowScheduler; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="DefaultBookmarkScheduler"/> class. |
| | | 17 | | /// </summary> |
| | 453 | 18 | | public DefaultBookmarkScheduler(IWorkflowScheduler workflowScheduler) |
| | | 19 | | { |
| | 453 | 20 | | _workflowScheduler = workflowScheduler; |
| | 453 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public async Task ScheduleAsync(IEnumerable<StoredBookmark> bookmarks, CancellationToken cancellationToken = default |
| | | 25 | | { |
| | 2 | 26 | | var bookmarkList = bookmarks.ToList(); |
| | 2 | 27 | | var delayBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Delay); |
| | 2 | 28 | | var startAtBookmarks = bookmarkList.Filter(SchedulingStimulusNames.StartAt); |
| | 2 | 29 | | var timerBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Timer); |
| | 2 | 30 | | var cronBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Cron); |
| | | 31 | | |
| | | 32 | | // Schedule each Delay bookmark. |
| | 4 | 33 | | foreach (var bookmark in delayBookmarks) |
| | | 34 | | { |
| | 0 | 35 | | var payload = bookmark.GetPayload<DelayPayload>(); |
| | 0 | 36 | | var resumeAt = payload.ResumeAt; |
| | 0 | 37 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 0 | 38 | | { |
| | 0 | 39 | | WorkflowInstanceId = bookmark.WorkflowInstanceId, |
| | 0 | 40 | | BookmarkId = bookmark.Id |
| | 0 | 41 | | }; |
| | 0 | 42 | | await _workflowScheduler.ScheduleAtAsync(bookmark.Id, request, resumeAt, cancellationToken); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // Schedule each StartAt bookmark. |
| | 4 | 46 | | foreach (var bookmark in startAtBookmarks) |
| | | 47 | | { |
| | 0 | 48 | | var payload = bookmark.GetPayload<StartAtPayload>(); |
| | 0 | 49 | | var executeAt = payload.ExecuteAt; |
| | 0 | 50 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 0 | 51 | | { |
| | 0 | 52 | | WorkflowInstanceId = bookmark.WorkflowInstanceId, |
| | 0 | 53 | | BookmarkId = bookmark.Id |
| | 0 | 54 | | }; |
| | 0 | 55 | | await _workflowScheduler.ScheduleAtAsync(bookmark.Id, request, executeAt, cancellationToken); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Schedule each Timer bookmark. |
| | 4 | 59 | | foreach (var bookmark in timerBookmarks) |
| | | 60 | | { |
| | 0 | 61 | | var payload = bookmark.GetPayload<TimerBookmarkPayload>(); |
| | 0 | 62 | | var resumeAt = payload.ResumeAt; |
| | 0 | 63 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 0 | 64 | | { |
| | 0 | 65 | | WorkflowInstanceId = bookmark.WorkflowInstanceId, |
| | 0 | 66 | | BookmarkId = bookmark.Id |
| | 0 | 67 | | }; |
| | 0 | 68 | | await _workflowScheduler.ScheduleAtAsync(bookmark.Id, request, resumeAt, cancellationToken); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Schedule each Cron bookmark. |
| | 4 | 72 | | foreach (var bookmark in cronBookmarks) |
| | | 73 | | { |
| | 0 | 74 | | var payload = bookmark.GetPayload<CronBookmarkPayload>(); |
| | 0 | 75 | | var cronExpression = payload.CronExpression; |
| | 0 | 76 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 0 | 77 | | { |
| | 0 | 78 | | WorkflowInstanceId = bookmark.WorkflowInstanceId, |
| | 0 | 79 | | BookmarkId = bookmark.Id |
| | 0 | 80 | | }; |
| | 0 | 81 | | await _workflowScheduler.ScheduleCronAsync(bookmark.Id, request, cronExpression, cancellationToken); |
| | | 82 | | } |
| | 2 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <inheritdoc /> |
| | | 86 | | public async Task ScheduleAsync(string workflowInstanceId, IEnumerable<Bookmark> bookmarks, CancellationToken cancel |
| | | 87 | | { |
| | 457 | 88 | | var bookmarkList = bookmarks.ToList(); |
| | 457 | 89 | | var delayBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Delay); |
| | 457 | 90 | | var startAtBookmarks = bookmarkList.Filter(SchedulingStimulusNames.StartAt); |
| | 457 | 91 | | var timerBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Timer); |
| | 457 | 92 | | var cronBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Cron); |
| | | 93 | | |
| | | 94 | | // Schedule each Delay bookmark. |
| | 944 | 95 | | foreach (var bookmark in delayBookmarks) |
| | | 96 | | { |
| | 15 | 97 | | var payload = bookmark.GetPayload<DelayPayload>(); |
| | 15 | 98 | | var resumeAt = payload.ResumeAt; |
| | 15 | 99 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 15 | 100 | | { |
| | 15 | 101 | | WorkflowInstanceId = workflowInstanceId, |
| | 15 | 102 | | BookmarkId = bookmark.Id |
| | 15 | 103 | | }; |
| | 15 | 104 | | await _workflowScheduler.ScheduleAtAsync(bookmark.Id, request, resumeAt, cancellationToken); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // Schedule each StartAt bookmark. |
| | 916 | 108 | | foreach (var bookmark in startAtBookmarks) |
| | | 109 | | { |
| | 1 | 110 | | var payload = bookmark.GetPayload<StartAtPayload>(); |
| | 1 | 111 | | var executeAt = payload.ExecuteAt; |
| | 1 | 112 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 1 | 113 | | { |
| | 1 | 114 | | WorkflowInstanceId = workflowInstanceId, |
| | 1 | 115 | | BookmarkId = bookmark.Id |
| | 1 | 116 | | }; |
| | 1 | 117 | | await _workflowScheduler.ScheduleAtAsync(bookmark.Id, request, executeAt, cancellationToken); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Schedule each Timer bookmark. |
| | 916 | 121 | | foreach (var bookmark in timerBookmarks) |
| | | 122 | | { |
| | 1 | 123 | | var payload = bookmark.GetPayload<TimerBookmarkPayload>(); |
| | 1 | 124 | | var resumeAt = payload.ResumeAt; |
| | 1 | 125 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 1 | 126 | | { |
| | 1 | 127 | | WorkflowInstanceId = workflowInstanceId, |
| | 1 | 128 | | BookmarkId = bookmark.Id |
| | 1 | 129 | | }; |
| | 1 | 130 | | await _workflowScheduler.ScheduleAtAsync(bookmark.Id, request, resumeAt, cancellationToken); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | // Schedule each Cron bookmark. |
| | 916 | 134 | | foreach (var bookmark in cronBookmarks) |
| | | 135 | | { |
| | 1 | 136 | | var payload = bookmark.GetPayload<CronBookmarkPayload>(); |
| | 1 | 137 | | var cronExpression = payload.CronExpression; |
| | 1 | 138 | | var request = new ScheduleExistingWorkflowInstanceRequest |
| | 1 | 139 | | { |
| | 1 | 140 | | WorkflowInstanceId = workflowInstanceId, |
| | 1 | 141 | | BookmarkId = bookmark.Id |
| | 1 | 142 | | }; |
| | 1 | 143 | | await _workflowScheduler.ScheduleCronAsync(bookmark.Id, request, cronExpression, cancellationToken); |
| | | 144 | | } |
| | 457 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <inheritdoc /> |
| | | 148 | | public async Task UnscheduleAsync(IEnumerable<Bookmark> bookmarks, CancellationToken cancellationToken = default) |
| | | 149 | | { |
| | 457 | 150 | | var bookmarkList = bookmarks.ToList(); |
| | 457 | 151 | | var delayBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Delay); |
| | 457 | 152 | | var startAtBookmarks = bookmarkList.Filter(SchedulingStimulusNames.StartAt); |
| | 457 | 153 | | var timerBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Timer); |
| | 457 | 154 | | var cronBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Cron); |
| | 457 | 155 | | var bookmarksToUnSchedule = delayBookmarks.Concat(startAtBookmarks).Concat(timerBookmarks).Concat(cronBookmarks) |
| | | 156 | | |
| | 942 | 157 | | foreach (var bookmark in bookmarksToUnSchedule) |
| | 14 | 158 | | await _workflowScheduler.UnscheduleAsync(bookmark.Id, cancellationToken); |
| | 457 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <inheritdoc /> |
| | | 162 | | public async Task UnscheduleAsync(IEnumerable<StoredBookmark> bookmarks, CancellationToken cancellationToken = defau |
| | | 163 | | { |
| | 52 | 164 | | var bookmarkList = bookmarks.ToList(); |
| | 52 | 165 | | var delayBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Delay); |
| | 52 | 166 | | var startAtBookmarks = bookmarkList.Filter(SchedulingStimulusNames.StartAt); |
| | 52 | 167 | | var timerBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Timer); |
| | 52 | 168 | | var cronBookmarks = bookmarkList.Filter(SchedulingStimulusNames.Cron); |
| | 52 | 169 | | var bookmarksToUnSchedule = delayBookmarks.Concat(startAtBookmarks).Concat(timerBookmarks).Concat(cronBookmarks) |
| | | 170 | | |
| | 140 | 171 | | foreach (var bookmark in bookmarksToUnSchedule) |
| | 18 | 172 | | await _workflowScheduler.UnscheduleAsync(bookmark.Id, cancellationToken); |
| | 52 | 173 | | } |
| | | 174 | | } |