| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Workflows.Api.RealTime.Contracts; |
| | | 3 | | using Elsa.Workflows.Api.RealTime.Hubs; |
| | | 4 | | using Elsa.Workflows.Api.RealTime.Messages; |
| | | 5 | | using Elsa.Workflows.Management.Notifications; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 8 | | using Microsoft.AspNetCore.SignalR; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Api.RealTime.Handlers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Broadcasts workflow progress to clients. |
| | | 14 | | /// </summary> |
| | | 15 | | public class BroadcastWorkflowProgress : |
| | | 16 | | INotificationHandler<ActivityExecutionLogUpdated>, |
| | | 17 | | INotificationHandler<ActivityExecutionRecordDeleted>, |
| | | 18 | | INotificationHandler<ActivityExecutionRecordUpdated>, |
| | | 19 | | INotificationHandler<WorkflowExecutionLogUpdated>, |
| | | 20 | | INotificationHandler<WorkflowInstanceSaved> |
| | | 21 | | { |
| | | 22 | | private readonly IHubContext<WorkflowInstanceHub, IWorkflowInstanceClient> _hubContext; |
| | | 23 | | private readonly IActivityExecutionStatsService _activityExecutionStatsService; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="BroadcastWorkflowProgress"/> class. |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public BroadcastWorkflowProgress(IHubContext<WorkflowInstanceHub, IWorkflowInstanceClient> hubContext, IActivityExec |
| | | 29 | | { |
| | 0 | 30 | | _hubContext = hubContext; |
| | 0 | 31 | | _activityExecutionStatsService = activityExecutionStatsService; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async Task HandleAsync(ActivityExecutionLogUpdated notification, CancellationToken cancellationToken) |
| | | 36 | | { |
| | 0 | 37 | | var workflowInstanceId = notification.WorkflowExecutionContext.Id; |
| | 0 | 38 | | var activityIds = notification.Records.Select(x => x.ActivityNodeId).Distinct().ToList(); |
| | 0 | 39 | | var stats = (await _activityExecutionStatsService.GetStatsAsync(workflowInstanceId, activityIds, cancellationTok |
| | 0 | 40 | | var clients = _hubContext.Clients.Group(workflowInstanceId); |
| | 0 | 41 | | var message = new ActivityExecutionLogUpdatedMessage(stats); |
| | | 42 | | |
| | 0 | 43 | | await clients.ActivityExecutionLogUpdatedAsync(message, cancellationToken); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async Task HandleAsync(ActivityExecutionRecordDeleted notification, CancellationToken cancellationToken) |
| | | 48 | | { |
| | 0 | 49 | | var record = notification.Record; |
| | 0 | 50 | | var workflowInstanceId = record.WorkflowInstanceId; |
| | 0 | 51 | | var activityId = record.ActivityId; |
| | 0 | 52 | | await BroadcastActivityExecutionLogUpdatedAsync(workflowInstanceId, activityId, cancellationToken); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public async Task HandleAsync(ActivityExecutionRecordUpdated notification, CancellationToken cancellationToken) |
| | | 57 | | { |
| | 0 | 58 | | var record = notification.Record; |
| | 0 | 59 | | var workflowInstanceId = record.WorkflowInstanceId; |
| | 0 | 60 | | var activityId = record.ActivityId; |
| | 0 | 61 | | await BroadcastActivityExecutionLogUpdatedAsync(workflowInstanceId, activityId, cancellationToken); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public async Task HandleAsync(WorkflowExecutionLogUpdated notification, CancellationToken cancellationToken) |
| | | 66 | | { |
| | 0 | 67 | | var workflowInstanceId = notification.WorkflowExecutionContext.Id; |
| | 0 | 68 | | var clients = _hubContext.Clients.Group(workflowInstanceId); |
| | 0 | 69 | | var message = new WorkflowExecutionLogUpdatedMessage(); |
| | | 70 | | |
| | 0 | 71 | | await clients.WorkflowExecutionLogUpdatedAsync(message, cancellationToken); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | public async Task HandleAsync(WorkflowInstanceSaved notification, CancellationToken cancellationToken) |
| | | 76 | | { |
| | 0 | 77 | | var workflowInstanceId = notification.WorkflowInstance.Id; |
| | 0 | 78 | | var clients = _hubContext.Clients.Group(workflowInstanceId); |
| | 0 | 79 | | var message = new WorkflowInstanceUpdatedMessage(workflowInstanceId); |
| | | 80 | | |
| | 0 | 81 | | await clients.WorkflowInstanceUpdatedAsync(message, cancellationToken); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private async Task BroadcastActivityExecutionLogUpdatedAsync(string workflowInstanceId, string activityId, Cancellat |
| | | 85 | | { |
| | 0 | 86 | | var stats = await _activityExecutionStatsService.GetStatsAsync(workflowInstanceId, activityId, cancellationToken |
| | 0 | 87 | | var clients = _hubContext.Clients.Group(workflowInstanceId); |
| | 0 | 88 | | var message = new ActivityExecutionLogUpdatedMessage(new[] { stats }); |
| | | 89 | | |
| | 0 | 90 | | await clients.ActivityExecutionLogUpdatedAsync(message, cancellationToken); |
| | 0 | 91 | | } |
| | | 92 | | } |