-
Notifications
You must be signed in to change notification settings - Fork 30
/
WorkerHostedService.cs
30 lines (26 loc) · 1.03 KB
/
WorkerHostedService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Camunda.Worker.Execution;
public sealed class WorkerHostedService : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly int _numberOfWorkers;
public WorkerHostedService(IServiceProvider serviceProvider, int numberOfWorkers)
{
_serviceProvider = Guard.NotNull(serviceProvider, nameof(serviceProvider));
_numberOfWorkers =
Guard.GreaterThanOrEqual(numberOfWorkers, Constants.MinimumParallelExecutors, nameof(numberOfWorkers));
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var activeTasks = Enumerable.Range(0, _numberOfWorkers)
.Select(_ => _serviceProvider.GetRequiredService<ICamundaWorker>())
.Select(worker => worker.RunAsync(stoppingToken))
.ToList();
return Task.WhenAll(activeTasks);
}
}