【问题标题】:Multiple queues in Hangfire with different number of workersHangfire中的多个队列具有不同数量的工人
【发布时间】:2021-09-18 02:46:15
【问题描述】:

对于我的 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
    });

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

我错过了什么?文档对此不是很清楚。

【问题讨论】:

    标签: c# .net .net-core hangfire


    【解决方案1】:

    嗯..突然的灵感给了我答案...把配置改成这样:

        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;
        });
    

    而且成功了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 2017-12-31
      • 2012-06-30
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多