| | | 1 | | using Elsa.Workflows.Management.Entities; |
| | | 2 | | using Elsa.Workflows.Runtime.Entities; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Runtime; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Helpers that record <c>WorkflowInterrupted</c> entries in the per-instance workflow execution log. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class InterruptedLogExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Writes a <c>WorkflowInterrupted</c> entry for the supplied instance with the typed payload. The record's |
| | | 13 | | /// <c>Id</c> is generated via <paramref name="identityGenerator"/> so the format stays consistent with every |
| | | 14 | | /// other log record in the store. Activity-related fields are populated from <paramref name="payload"/>'s |
| | | 15 | | /// <see cref="WorkflowInterruptedPayload.LastActivityId"/> when available; otherwise they are left empty |
| | | 16 | | /// (the forensic record still uniquely identifies the interrupted instance via the workflow instance id). |
| | | 17 | | /// </summary> |
| | | 18 | | public static async Task LogWorkflowInterruptedAsync( |
| | | 19 | | this IWorkflowExecutionLogStore store, |
| | | 20 | | IIdentityGenerator identityGenerator, |
| | | 21 | | WorkflowInstance instance, |
| | | 22 | | WorkflowInterruptedPayload payload, |
| | | 23 | | CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 3 | 25 | | var record = new WorkflowExecutionLogRecord |
| | 3 | 26 | | { |
| | 3 | 27 | | Id = identityGenerator.GenerateId(), |
| | 3 | 28 | | WorkflowInstanceId = instance.Id, |
| | 3 | 29 | | WorkflowDefinitionId = instance.DefinitionId, |
| | 3 | 30 | | WorkflowDefinitionVersionId = instance.DefinitionVersionId, |
| | 3 | 31 | | WorkflowVersion = instance.Version, |
| | 3 | 32 | | ActivityInstanceId = payload.LastActivityId ?? string.Empty, |
| | 3 | 33 | | ActivityId = payload.LastActivityId ?? string.Empty, |
| | 3 | 34 | | ActivityType = string.Empty, |
| | 3 | 35 | | ActivityNodeId = payload.LastActivityNodeId ?? string.Empty, |
| | 3 | 36 | | Timestamp = payload.InterruptedAt, |
| | 3 | 37 | | EventName = WorkflowInterruptedPayload.WorkflowInterruptedEventName, |
| | 3 | 38 | | Source = "Elsa.Workflows.Runtime.GracefulShutdown", |
| | 3 | 39 | | Message = $"Workflow execution cycle was force-cancelled by the runtime ({payload.Reason}).", |
| | 3 | 40 | | Payload = payload, |
| | 3 | 41 | | }; |
| | | 42 | | |
| | 3 | 43 | | await store.AddAsync(record, cancellationToken); |
| | 3 | 44 | | } |
| | | 45 | | } |