Refactor BackgroundTaskDispatcher and update lifecycle

Refactored the BackgroundTaskDispatcher class to accept the INotificationSender service directly rather than the IServiceScopeFactory. This change simplifies the dispatch process. Also, adjusted the service lifecycle of RunTaskDispatcher, changing it from singleton to scoped.
This commit is contained in:
Sipke Schoorstra 2024-03-28 16:05:28 +01:00
parent 673f5dfdbc
commit b7074e45d2
2 changed files with 2 additions and 5 deletions

View file

@ -209,7 +209,7 @@ public class WorkflowRuntimeFeature : FeatureBase
.AddScoped(ActivityExecutionLogStore)
.AddScoped(WorkflowInboxStore)
.AddScoped(WorkflowExecutionContextStore)
.AddSingleton(RunTaskDispatcher)
.AddScoped(RunTaskDispatcher)
.AddSingleton(BackgroundActivityScheduler)
.AddSingleton<RandomLongIdentityGenerator>()
.AddScoped<IBookmarkManager, DefaultBookmarkManager>()

View file

@ -2,20 +2,17 @@ using Elsa.Mediator;
using Elsa.Mediator.Contracts;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Notifications;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Workflows.Runtime.Services;
/// <summary>
/// Relies on the <see cref="INotificationSender"/> to publish the received request as a domain event from a background worker.
/// </summary>
public class BackgroundTaskDispatcher(IServiceScopeFactory scopeFactory) : ITaskDispatcher
public class BackgroundTaskDispatcher(INotificationSender notificationSender) : ITaskDispatcher
{
/// <inheritdoc />
public async Task DispatchAsync(RunTaskRequest request, CancellationToken cancellationToken = default)
{
using var scope = scopeFactory.CreateScope();
var notificationSender = scope.ServiceProvider.GetRequiredService<INotificationSender>();
await notificationSender.SendAsync(request, NotificationStrategy.Background, cancellationToken);
}
}