diff --git a/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs b/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs index 5828def98..bfff39946 100644 --- a/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs +++ b/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs @@ -54,8 +54,14 @@ public static class DependencyInjectionExtensions .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() - .AddHostedService() + .AddSingleton() + .AddHostedService(sp => + { + using var scope = sp.CreateScope(); + + var options = scope.ServiceProvider.GetRequiredService>().Value; + return ActivatorUtilities.CreateInstance(scope.ServiceProvider, options.JobWorkerCount); + }) .AddHostedService(sp => { using var scope = sp.CreateScope(); diff --git a/src/common/Elsa.Mediator/HostedServices/JobRunnerHostedService.cs b/src/common/Elsa.Mediator/HostedServices/JobRunnerHostedService.cs index 223eb35f4..b759367b1 100644 --- a/src/common/Elsa.Mediator/HostedServices/JobRunnerHostedService.cs +++ b/src/common/Elsa.Mediator/HostedServices/JobRunnerHostedService.cs @@ -9,13 +9,14 @@ namespace Elsa.Mediator.HostedServices; /// public class JobRunnerHostedService : BackgroundService { - private const int WorkerCount = 4; + private readonly int _workerCount; private readonly IJobsChannel _jobsChannel; private readonly ILogger _logger; /// - public JobRunnerHostedService(IJobsChannel jobsChannel, ILogger logger) - { + public JobRunnerHostedService(int workerCount, IJobsChannel jobsChannel, ILogger logger) + { + _workerCount = workerCount; _jobsChannel = jobsChannel; _logger = logger; } @@ -23,9 +24,9 @@ public class JobRunnerHostedService : BackgroundService /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - var workers = new Task[WorkerCount]; - - for (var i = 0; i < WorkerCount; i++) + var workers = new Task[_workerCount]; + + for (var i = 0; i < _workerCount; i++) workers[i] = ProcessJobsAsync(stoppingToken); await Task.WhenAll(workers); diff --git a/src/common/Elsa.Mediator/Options/MediatorOptions.cs b/src/common/Elsa.Mediator/Options/MediatorOptions.cs index a8e646683..d42f2a4aa 100644 --- a/src/common/Elsa.Mediator/Options/MediatorOptions.cs +++ b/src/common/Elsa.Mediator/Options/MediatorOptions.cs @@ -10,12 +10,17 @@ public class MediatorOptions /// /// The max number of workers to process commands. /// - public int CommandWorkerCount { get; set; } = 4; - + public int CommandWorkerCount { get; set; } = 4; + /// /// The max number of workers to process notifications. /// - public int NotificationWorkerCount { get; set; } = 4; + public int NotificationWorkerCount { get; set; } = 4; + + /// + /// The max number of workers to process jobs. + /// + public int JobWorkerCount { get; set; } = 4; /// /// The default publishing strategy to use.