2023-08-06 15:03:36 +00:00
|
|
|
using Elsa.Mediator;
|
|
|
|
|
using Elsa.Mediator.Contracts;
|
2023-08-12 19:09:07 +00:00
|
|
|
using Elsa.Mediator.PublishingStrategies;
|
2023-01-04 14:28:28 +00:00
|
|
|
using Elsa.Workflows.Runtime.Activities;
|
2023-07-19 22:05:43 +00:00
|
|
|
using Elsa.Workflows.Runtime.Bookmarks;
|
2023-03-03 21:47:23 +00:00
|
|
|
using Elsa.Workflows.Runtime.Contracts;
|
2023-08-06 15:03:36 +00:00
|
|
|
using Elsa.Workflows.Runtime.Models;
|
|
|
|
|
using Elsa.Workflows.Runtime.Options;
|
2023-01-04 14:28:28 +00:00
|
|
|
|
2023-03-03 21:47:23 +00:00
|
|
|
namespace Elsa.Workflows.Runtime.Services;
|
2023-01-04 14:28:28 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public class EventPublisher : IEventPublisher
|
|
|
|
|
{
|
2023-08-06 15:03:36 +00:00
|
|
|
private readonly IWorkflowInbox _workflowInbox;
|
2023-01-04 14:28:28 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
2023-08-06 15:03:36 +00:00
|
|
|
public EventPublisher(IWorkflowInbox workflowInbox)
|
2023-01-04 14:28:28 +00:00
|
|
|
{
|
2023-08-06 15:03:36 +00:00
|
|
|
_workflowInbox = workflowInbox;
|
2023-01-04 14:28:28 +00:00
|
|
|
}
|
2023-08-06 15:03:36 +00:00
|
|
|
|
2023-01-04 14:28:28 +00:00
|
|
|
/// <inheritdoc />
|
2023-08-09 11:02:20 +00:00
|
|
|
public async Task PublishAsync(
|
|
|
|
|
string eventName,
|
|
|
|
|
string? correlationId = default,
|
|
|
|
|
string? workflowInstanceId = default,
|
|
|
|
|
string? activityInstanceId = default,
|
|
|
|
|
IDictionary<string, object>? input = default,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2023-01-04 14:28:28 +00:00
|
|
|
{
|
2023-08-09 11:02:20 +00:00
|
|
|
await PublishInternalAsync(eventName, NotificationStrategy.Sequential, correlationId, workflowInstanceId, activityInstanceId, input, cancellationToken);
|
2023-01-04 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-09 11:02:20 +00:00
|
|
|
public async Task DispatchAsync(
|
|
|
|
|
string eventName,
|
|
|
|
|
string? correlationId = default,
|
|
|
|
|
string? workflowInstanceId = default,
|
|
|
|
|
string? activityInstanceId = default,
|
|
|
|
|
IDictionary<string, object>? input = default,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2023-08-06 15:03:36 +00:00
|
|
|
{
|
2023-08-12 19:09:07 +00:00
|
|
|
await PublishInternalAsync(eventName, NotificationStrategy.FireAndForget, correlationId, workflowInstanceId, activityInstanceId, input, cancellationToken);
|
2023-08-06 15:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:02:20 +00:00
|
|
|
private async Task PublishInternalAsync(
|
|
|
|
|
string eventName,
|
|
|
|
|
IEventPublishingStrategy publishingStrategy,
|
|
|
|
|
string? correlationId = default,
|
|
|
|
|
string? workflowInstanceId = default,
|
|
|
|
|
string? activityInstanceId = default,
|
|
|
|
|
IDictionary<string, object>? input = default,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2023-01-04 14:28:28 +00:00
|
|
|
{
|
|
|
|
|
var eventBookmark = new EventBookmarkPayload(eventName);
|
2023-08-09 11:02:20 +00:00
|
|
|
var message = NewWorkflowInboxMessage.For<Event>(eventBookmark, workflowInstanceId, correlationId, activityInstanceId, input);
|
2023-08-06 15:03:36 +00:00
|
|
|
var options = new WorkflowInboxMessageDeliveryOptions
|
|
|
|
|
{
|
2023-08-12 19:09:07 +00:00
|
|
|
EventPublishingStrategy = publishingStrategy,
|
2023-08-06 15:03:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await _workflowInbox.SubmitAsync(message, options, cancellationToken);
|
2023-01-04 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
}
|