Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Workers in .net core worker services?

Tags:

.net-core

The dot net core 3.0 worker services template shown as follow:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices(services =>
            {
                services.AddHostedService<Worker>();
            });
}

The "Worker" class is derived from BackgroundService. It loops to write log to console every 1000 ms.

My questions:

Can I run multiple "Worker"s simultaneously? (I know I can create another class "Worker2". But can I run two copies of same class "Worker"?)

If yes, how I can configure two "Worker" with different configuration or parameters, say, two Workers with different looping intervals? (Because instance of "Worker" class is created by DI framework. I don't know how I can pass different config/parameters to two different instance of "Worker")

like image 642
Raymond Wong Avatar asked Oct 04 '19 03:10

Raymond Wong


1 Answers

You can have a "parent" worker that launches the "real" workers like this...

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var workers = new List<Task>();
                foreach(var delay in _config.LoopIntervals)
                    workers.Add(DoRealWork(delay, stoppingToken));

                await Task.WhenAll(workers.ToArray());
            }
        }

Then...

        private async Task DoRealWork(int delay, CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("worker {delay} checking in at {time}", delay, DateTimeOffset.Now);
                await Task.Delay(delay, stoppingToken);
            }
        }

_config gets populated from appSettings.json and passed in to the constructor of the Worker like this...

var cfg = hostContext.Configuration.GetSection(nameof(WorkerConfig)).Get<WorkerConfig>();
services.AddSingleton(cfg);
services.AddHostedService<Worker>();

and the appSettings...

{
  "WorkerConfig": {
    "LoopIntervals": [ 1000, 2000, 3000 ]
  }
}
like image 129
Ed MacDonald Avatar answered Sep 20 '22 13:09

Ed MacDonald