Use IOptions<MediatorOptions> to get workerCount for BackgroundCommandSenderHostedService, BackgroundEventPublisherHostedService and JobRunnerHostedService

This commit is contained in:
Thomas Trummer 2025-03-17 08:45:34 +01:00
parent 84be58fceb
commit 84191f149d
4 changed files with 17 additions and 30 deletions

View file

@ -14,7 +14,6 @@ using Elsa.Mediator.Options;
using Elsa.Mediator.Services;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;
@ -55,27 +54,9 @@ public static class DependencyInjectionExtensions
.AddSingleton<ICommandsChannel, CommandsChannel>()
.AddSingleton<IJobsChannel, JobsChannel>()
.AddSingleton<IJobQueue, JobQueue>()
.AddHostedService(sp =>
{
using var scope = sp.CreateScope();
var options = scope.ServiceProvider.GetRequiredService<IOptions<MediatorOptions>>().Value;
return ActivatorUtilities.CreateInstance<JobRunnerHostedService>(scope.ServiceProvider, options.JobWorkerCount);
})
.AddHostedService(sp =>
{
using var scope = sp.CreateScope();
var options = scope.ServiceProvider.GetRequiredService<IOptions<MediatorOptions>>().Value;
return ActivatorUtilities.CreateInstance<BackgroundCommandSenderHostedService>(scope.ServiceProvider, options.CommandWorkerCount);
})
.AddHostedService(sp =>
{
using var scope = sp.CreateScope();
var options = scope.ServiceProvider.GetRequiredService<IOptions<MediatorOptions>>().Value;
return ActivatorUtilities.CreateInstance<BackgroundEventPublisherHostedService>(scope.ServiceProvider, options.NotificationWorkerCount);
});
.AddHostedService<JobRunnerHostedService>()
.AddHostedService<BackgroundCommandSenderHostedService>()
.AddHostedService<BackgroundEventPublisherHostedService>();
}
/// <summary>

View file

@ -1,8 +1,10 @@
using System.Threading.Channels;
using Elsa.Mediator.Contracts;
using Elsa.Mediator.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Elsa.Mediator.HostedServices;
@ -18,13 +20,13 @@ public class BackgroundCommandSenderHostedService : BackgroundService
private readonly ILogger _logger;
/// <inheritdoc />
public BackgroundCommandSenderHostedService(int workerCount, ICommandsChannel commandsChannel, IServiceScopeFactory scopeFactory, ILogger<BackgroundCommandSenderHostedService> logger)
public BackgroundCommandSenderHostedService(IOptions<MediatorOptions> options, ICommandsChannel commandsChannel, IServiceScopeFactory scopeFactory, ILogger<BackgroundCommandSenderHostedService> logger)
{
_workerCount = workerCount;
_workerCount = options.Value.CommandWorkerCount;
_commandsChannel = commandsChannel;
_scopeFactory = scopeFactory;
_logger = logger;
_outputs = new List<Channel<ICommand>>(workerCount);
_outputs = new List<Channel<ICommand>>(_workerCount);
}
/// <inheritdoc />

View file

@ -1,8 +1,10 @@
using System.Threading.Channels;
using Elsa.Mediator.Contracts;
using Elsa.Mediator.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Elsa.Mediator.HostedServices;
@ -18,13 +20,13 @@ public class BackgroundEventPublisherHostedService : BackgroundService
private readonly ILogger _logger;
/// <inheritdoc />
public BackgroundEventPublisherHostedService(int workerCount, INotificationsChannel notificationsChannel, IServiceScopeFactory scopeFactory, ILogger<BackgroundEventPublisherHostedService> logger)
public BackgroundEventPublisherHostedService(IOptions<MediatorOptions> options, INotificationsChannel notificationsChannel, IServiceScopeFactory scopeFactory, ILogger<BackgroundEventPublisherHostedService> logger)
{
_workerCount = workerCount;
_workerCount = options.Value.NotificationWorkerCount;
_notificationsChannel = notificationsChannel;
_scopeFactory = scopeFactory;
_logger = logger;
_outputs = new List<Channel<INotification>>(workerCount);
_outputs = new List<Channel<INotification>>(_workerCount);
}
/// <inheritdoc />

View file

@ -1,6 +1,8 @@
using Elsa.Mediator.Contracts;
using Elsa.Mediator.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Elsa.Mediator.HostedServices;
@ -14,9 +16,9 @@ public class JobRunnerHostedService : BackgroundService
private readonly ILogger<JobRunnerHostedService> _logger;
/// <inheritdoc />
public JobRunnerHostedService(int workerCount, IJobsChannel jobsChannel, ILogger<JobRunnerHostedService> logger)
public JobRunnerHostedService(IOptions<MediatorOptions> options, IJobsChannel jobsChannel, ILogger<JobRunnerHostedService> logger)
{
_workerCount = workerCount;
_workerCount = options.Value.JobWorkerCount;
_jobsChannel = jobsChannel;
_logger = logger;
}