-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor service dependencies in BackgroundTaskDispatcher and LocalBa…
…ckgroundActivityScheduler (#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
1 parent
91b59a9
commit 480b726
Showing
3 changed files
with
17 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 7 additions & 16 deletions
23
src/modules/Elsa.Workflows.Runtime/Services/LocalBackgroundActivityScheduler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |