| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Runtime.Activities; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Faults the workflow. |
| | | 11 | | /// </summary> |
| | | 12 | | [Activity("Elsa", "Primitives", "Publishes an event.")] |
| | | 13 | | [UsedImplicitly] |
| | 39 | 14 | | public class PublishEvent([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : Activity(source |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The name of the event to publish. |
| | | 18 | | /// </summary> |
| | | 19 | | [Input(Description = "The name of the event to publish.")] |
| | 141 | 20 | | public Input<string> EventName { get; set; } = null!; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The correlation ID to scope the event to. |
| | | 24 | | /// </summary> |
| | | 25 | | [Input(Description = "The correlation ID to scope the event to.")] |
| | 120 | 26 | | public Input<string?> CorrelationId { get; set; } = null!; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Whether the event is local to the workflow. |
| | | 30 | | /// </summary> |
| | | 31 | | [Input(DisplayName = "Local event", Description = "Whether the event is local to the workflow. When checked, the eve |
| | 138 | 32 | | public Input<bool> IsLocalEvent { get; set; } = null!; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The input to send as the event body. |
| | | 36 | | /// </summary> |
| | | 37 | | [Input(Description = "The payload to send as the event body.")] |
| | 138 | 38 | | public Input<object> Payload { get; set; } = null!; |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 42 | | { |
| | 3 | 43 | | var eventName = EventName.Get(context); |
| | 3 | 44 | | var correlationId = CorrelationId.GetOrDefault(context).NullIfWhiteSpace(); |
| | 3 | 45 | | var isLocalEvent = IsLocalEvent.GetOrDefault(context); |
| | 3 | 46 | | var workflowInstanceId = isLocalEvent ? context.WorkflowExecutionContext.Id : null; |
| | 3 | 47 | | var payload = Payload.GetOrDefault(context); |
| | 3 | 48 | | var publisher = context.GetRequiredService<IEventPublisher>(); |
| | | 49 | | |
| | 3 | 50 | | await publisher.PublishAsync(eventName, correlationId, workflowInstanceId, null, payload, true, context.Cancella |
| | 3 | 51 | | await context.CompleteActivityAsync(); |
| | 3 | 52 | | } |
| | | 53 | | } |