Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scoping issues #4753

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}