forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PublishEvent.cs
59 lines (51 loc) · 2.18 KB
/
PublishEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Elsa.Extensions;
using Elsa.Workflows.Runtime.Contracts;
using JetBrains.Annotations;
using System.Runtime.CompilerServices;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Models;
namespace Elsa.Workflows.Runtime.Activities;
/// <summary>
/// Faults the workflow.
/// </summary>
[Activity("Elsa", "Primitives", "Publishes an event.")]
[UsedImplicitly]
public class PublishEvent : Activity
{
/// <inheritdoc />
public PublishEvent([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
{
}
/// <summary>
/// The name of the event to publish.
/// </summary>
[Input(Description = "The name of the event to publish.")]
public Input<string> EventName { get; set; } = default!;
/// <summary>
/// The correlation ID to scope the event to.
/// </summary>
[Input(Description = "The correlation ID to scope the event to.")]
public Input<string?> CorrelationId { get; set; } = default!;
/// <summary>
/// Whether the event is local to the workflow.
/// </summary>
[Input(DisplayName = "Local event", Description = "Whether the event is local to the workflow. When checked, the event will be delivered to this workflow instance only.")]
public Input<bool> IsLocalEvent { get; set; } = default!;
/// <summary>
/// The input to send as the event body.
/// </summary>
[Input(Description = "The payload to send as the event body.")]
public Input<object> Payload { get; set; } = default!;
/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var eventName = EventName.Get(context);
var correlationId = CorrelationId.GetOrDefault(context);
var isLocalEvent = IsLocalEvent.GetOrDefault(context);
var workflowInstanceId = isLocalEvent ? context.WorkflowExecutionContext.Id : default;
var payload = Payload.GetOrDefault(context);
var publisher = context.GetRequiredService<IEventPublisher>();
await publisher.DispatchAsync(eventName, correlationId, workflowInstanceId, null, payload, context.CancellationToken);
await context.CompleteActivityAsync();
}
}