< Summary

Information
Class: Elsa.Workflows.Middleware.Activities.NotificationPublishingMiddlewareExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Activities/NotificationPublishingMiddleware.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 59
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseNotifications(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Activities/NotificationPublishingMiddleware.cs

#LineLine coverage
 1using Elsa.Mediator.Contracts;
 2using Elsa.Workflows.Notifications;
 3using Elsa.Workflows.Pipelines.ActivityExecution;
 4
 5namespace 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>
 15public class NotificationPublishingMiddleware : IActivityExecutionMiddleware
 16{
 17    private readonly ActivityMiddlewareDelegate _next;
 18    private readonly INotificationSender _notificationSender;
 19
 20    /// <summary>
 21    /// Constructor.
 22    /// </summary>
 23    public NotificationPublishingMiddleware(ActivityMiddlewareDelegate next, INotificationSender notificationSender)
 24    {
 25        _next = next;
 26        _notificationSender = notificationSender;
 27    }
 28
 29    /// <inheritdoc />
 30    public async ValueTask InvokeAsync(ActivityExecutionContext context)
 31    {
 32        // Store the status before execution
 33        var statusBeforeExecution = context.Status;
 34
 35        // Send notification that activity is executing
 36        await _notificationSender.SendAsync(new ActivityExecuting(context));
 37
 38        // Execute the activity
 39        await _next(context);
 40
 41        // Send notification that activity has executed
 42        await _notificationSender.SendAsync(new ActivityExecuted(context));
 43
 44        // Send notification that activity has completed if status transitioned from Running to Completed
 45        if (statusBeforeExecution == ActivityStatus.Running && context.Status == ActivityStatus.Completed)
 46            await _notificationSender.SendAsync(new Notifications.ActivityCompleted(context));
 47    }
 48}
 49
 50/// <summary>
 51/// Extends <see cref="IActivityExecutionPipelineBuilder"/> to install the <see cref="NotificationPublishingMiddleware"/
 52/// </summary>
 53public static class NotificationPublishingMiddlewareExtensions
 54{
 55    /// <summary>
 56    /// Installs the <see cref="NotificationPublishingMiddleware"/> component.
 57    /// </summary>
 42158    public static IActivityExecutionPipelineBuilder UseNotifications(this IActivityExecutionPipelineBuilder pipelineBuil
 59}