Skip to content

Commit

Permalink
Refactor service dependencies in BackgroundTaskDispatcher and LocalBa…
Browse files Browse the repository at this point in the history
…ckgroundActivityScheduler (elsa-workflows#4753)

Removed explicit dependency resolution from the constructors of BackgroundTaskDispatcher and LocalBackgroundActivityScheduler classes, and used IServiceScopeFactory to resolve dependencies inside methods instead. Updated their lifetimes from Scoped to Singleton in the DI container in WorkflowRuntimeFeature. The DI container now manages the lifetime scope of these services, promoting better separation of responsibilities and improved testability.
  • Loading branch information
sfmskywalker committed Jan 5, 2024
1 parent 91b59a9 commit 480b726
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public override void Apply()
.AddScoped(WorkflowExecutionLogStore)
.AddScoped(ActivityExecutionLogStore)
.AddScoped(WorkflowInboxStore)
.AddScoped(RunTaskDispatcher)
.AddScoped(BackgroundActivityScheduler)
.AddSingleton(RunTaskDispatcher)
.AddSingleton(BackgroundActivityScheduler)
.AddScoped<IBookmarkManager, DefaultBookmarkManager>()
.AddScoped<IActivityExecutionManager, DefaultActivityExecutionManager>()
.AddScoped<IActivityExecutionStatsService, ActivityExecutionStatsService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
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 : ITaskDispatcher
public class BackgroundTaskDispatcher(IServiceScopeFactory scopeFactory) : ITaskDispatcher
{
private readonly INotificationSender _eventPublisher;

/// <summary>
/// Constructor.
/// </summary>
public BackgroundTaskDispatcher(INotificationSender eventPublisher) => _eventPublisher = eventPublisher;

/// <inheritdoc />
public async Task DispatchAsync(RunTaskRequest request, CancellationToken cancellationToken = default) => await _eventPublisher.SendAsync(request, NotificationStrategy.Background, cancellationToken);
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
using Elsa.Mediator.Contracts;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Models;
using Microsoft.Extensions.DependencyInjection;

namespace Elsa.Workflows.Runtime.Services;

/// <summary>
/// Invokes activities from a background worker within the context of its workflow instance using a local background worker.
/// </summary>
public class LocalBackgroundActivityScheduler : IBackgroundActivityScheduler
public class LocalBackgroundActivityScheduler(IJobQueue jobQueue, IServiceScopeFactory scopeFactory) : IBackgroundActivityScheduler
{
private readonly IJobQueue _jobQueue;
private readonly IBackgroundActivityInvoker _backgroundActivityInvoker;

/// <summary>
/// Initializes a new instance of the <see cref="LocalBackgroundActivityScheduler"/> class.
/// </summary>
public LocalBackgroundActivityScheduler(IJobQueue jobQueue, IBackgroundActivityInvoker backgroundActivityInvoker)
{
_jobQueue = jobQueue;
_backgroundActivityInvoker = backgroundActivityInvoker;
}

/// <inheritdoc />
public Task<string> ScheduleAsync(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancellationToken = default)
{
var jobId = _jobQueue.Enqueue(async ct => await InvokeBackgroundActivity(scheduledBackgroundActivity, ct));
var jobId = jobQueue.Enqueue(async ct => await InvokeBackgroundActivity(scheduledBackgroundActivity, ct));
return Task.FromResult(jobId);
}

/// <inheritdoc />
public Task CancelAsync(string jobId, CancellationToken cancellationToken = default)
{
_jobQueue.Cancel(jobId);
jobQueue.Cancel(jobId);
return Task.CompletedTask;
}

private async Task InvokeBackgroundActivity(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancellationToken)
{
await _backgroundActivityInvoker.ExecuteAsync(scheduledBackgroundActivity, cancellationToken);
using var scope = scopeFactory.CreateScope();
var backgroundActivityInvoker = scope.ServiceProvider.GetRequiredService<IBackgroundActivityInvoker>();
await backgroundActivityInvoker.ExecuteAsync(scheduledBackgroundActivity, cancellationToken);
}
}

0 comments on commit 480b726

Please sign in to comment.