| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Workflows.Notifications; |
| | | 3 | | using Elsa.Workflows.Pipelines.ActivityExecution; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Middleware.Activities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// An activity execution middleware component that publishes activity notifications: |
| | | 9 | | /// <list type="bullet"> |
| | | 10 | | /// <item><see cref="ActivityExecuting"/> when an activity starts executing</item> |
| | | 11 | | /// <item><see cref="ActivityExecuted"/> when an activity has executed</item> |
| | | 12 | | /// <item><see cref="Notifications.ActivityCompleted"/> when an activity transitions from Running to Completed status</i |
| | | 13 | | /// </list> |
| | | 14 | | /// </summary> |
| | | 15 | | public class NotificationPublishingMiddleware : IActivityExecutionMiddleware |
| | | 16 | | { |
| | | 17 | | private readonly ActivityMiddlewareDelegate _next; |
| | | 18 | | private readonly INotificationSender _notificationSender; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Constructor. |
| | | 22 | | /// </summary> |
| | 421 | 23 | | public NotificationPublishingMiddleware(ActivityMiddlewareDelegate next, INotificationSender notificationSender) |
| | | 24 | | { |
| | 421 | 25 | | _next = next; |
| | 421 | 26 | | _notificationSender = notificationSender; |
| | 421 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public async ValueTask InvokeAsync(ActivityExecutionContext context) |
| | | 31 | | { |
| | | 32 | | // Store the status before execution |
| | 3019 | 33 | | var statusBeforeExecution = context.Status; |
| | | 34 | | |
| | | 35 | | // Send notification that activity is executing |
| | 3019 | 36 | | await _notificationSender.SendAsync(new ActivityExecuting(context)); |
| | | 37 | | |
| | | 38 | | // Execute the activity |
| | 3019 | 39 | | await _next(context); |
| | | 40 | | |
| | | 41 | | // Send notification that activity has executed |
| | 3006 | 42 | | await _notificationSender.SendAsync(new ActivityExecuted(context)); |
| | | 43 | | |
| | | 44 | | // Send notification that activity has completed if status transitioned from Running to Completed |
| | 3006 | 45 | | if (statusBeforeExecution == ActivityStatus.Running && context.Status == ActivityStatus.Completed) |
| | 27 | 46 | | await _notificationSender.SendAsync(new Notifications.ActivityCompleted(context)); |
| | 3006 | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Extends <see cref="IActivityExecutionPipelineBuilder"/> to install the <see cref="NotificationPublishingMiddleware"/ |
| | | 52 | | /// </summary> |
| | | 53 | | public static class NotificationPublishingMiddlewareExtensions |
| | | 54 | | { |
| | | 55 | | /// <summary> |
| | | 56 | | /// Installs the <see cref="NotificationPublishingMiddleware"/> component. |
| | | 57 | | /// </summary> |
| | | 58 | | public static IActivityExecutionPipelineBuilder UseNotifications(this IActivityExecutionPipelineBuilder pipelineBuil |
| | | 59 | | } |