Multiple queues in Hangfire with different number of workers

For my dotnet 5 API I am using Hangfire to execute long running tasks. In my particular situation, I am trying to accomplish having 2 queues, 1 running serial (1 worker) and 1 with standard number of workers (number of CPU's * 5).

my config (ConfigureServices in startup):

    services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DbConnection")));

    services.AddHangfireServer(x => new BackgroundJobServerOptions
    {
        ServerName = string.Format("{0}:serial", Environment.MachineName),
        Queues = new[] { "serial" },
        WorkerCount = 1
    });

    services.AddHangfireServer(x => new BackgroundJobServerOptions
    {
        ServerName = string.Format("{0}:default", Environment.MachineName),
        Queues = new[] { "default" },
        WorkerCount = Environment.ProcessorCount * 5
    });

My problem is, when I start my API, that hangfire produces 2 servers (check), but they both have only the default queue. So my jobs being enqueued on the serial queue (yes, that works), never gets any worker assigned, thus never gets executed..

What am I missing? The documentation is not very clear on this.

Upvotes: 3

Views: 5290

Answers (1)

well.. a sudden inspiration gave me the answer... changed the configuration to this:

    services.AddHangfireServer(action =>
    {
        action.ServerName = $"{Environment.MachineName}:default";
        action.Queues = new[] {"default"};
        action.WorkerCount = 1;
    });

    services.AddHangfireServer(action =>
    {
        action.ServerName = $"{Environment.MachineName}:serial";
        action.Queues = new[] { "serial" };
        action.WorkerCount = Environment.ProcessorCount * 5;
    });

and that worked.

Upvotes: 8

Related Questions