Hangfire 中具有不同数量工作人员的多个队列

Han*_*ler 2 .net c# hangfire .net-core

对于我的 dotnet 5 API,我使用 Hangfire 来执行长时间运行的任务。在我的特定情况下,我试图实现 2 个队列,1 个正在运行的串行队列(1 个工作线程)和 1 个具有标准数量的工作线程(CPU 数量 * 5)的队列。

我的配置(启动时配置服务):

    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
    });
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我启动 API 时,hangfire 会生成 2 个服务器(检查),但它们都只有默认队列。因此,我的作业被排入串行队列(是的,有效),永远不会分配任何工作人员,因此永远不会被执行。

我缺少什么?文档对此不是很清楚。

Han*_*ler 6

好吧..突然的灵感给了我答案...将配置更改为:

    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;
    });
Run Code Online (Sandbox Code Playgroud)

这奏效了。