| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Scheduling; |
| | | 3 | | using Elsa.Scheduling.Bookmarks; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Extensions; |
| | | 9 | | |
| | | 10 | | public static class DelayActivityExecutionContextExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Resumes the activity after a specified delay. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="context">The activity execution context in which the workflow is running.</param> |
| | | 16 | | /// <param name="delay">The delay before the workflow resumes.</param> |
| | | 17 | | /// <param name="callback">The delegate to execute when the activity resumes.</param> |
| | | 18 | | public static void DelayFor(this ActivityExecutionContext context, TimeSpan delay, ExecuteActivityDelegate? callback |
| | | 19 | | { |
| | 27 | 20 | | var clock = context.ExpressionExecutionContext.GetRequiredService<ISystemClock>(); |
| | 27 | 21 | | var resumeAt = clock.UtcNow.Add(delay); |
| | | 22 | | |
| | 27 | 23 | | DelayUntil(context, resumeAt, callback); |
| | 27 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Resumes the activity at a specified point in time. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="context">The activity execution context in which the workflow is running.</param> |
| | | 30 | | /// <param name="resumeAt">The point in time at which the workflow should resume execution.</param> |
| | | 31 | | /// <param name="callback">The delegate to execute when the activity resumes.</param> |
| | | 32 | | public static void DelayUntil(this ActivityExecutionContext context, DateTimeOffset resumeAt, ExecuteActivityDelegat |
| | | 33 | | { |
| | 30 | 34 | | var payload = new DelayPayload(resumeAt); |
| | | 35 | | |
| | 30 | 36 | | var bookmarkOptions = new CreateBookmarkArgs |
| | 30 | 37 | | { |
| | 30 | 38 | | BookmarkName = SchedulingStimulusNames.Delay, |
| | 30 | 39 | | Stimulus = payload, |
| | 30 | 40 | | Callback = callback |
| | 30 | 41 | | }; |
| | | 42 | | |
| | 30 | 43 | | context.CreateBookmark(bookmarkOptions); |
| | 30 | 44 | | } |
| | | 45 | | } |