Hangfire 中具有不同工作人员数量的多个队列(服务器)

Multiple queues(Servers) with different worker counts in Hangfire

我试图创建多个队列,每个队列应该只有一个工作人员。 所以我做了下面的代码:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
            app.UseHangfireDashboard("/scheduler", new DashboardOptions
            {
                Authorization = new[] { new HangFireAuthorization() },
                AppPath = "/"
            });

            app.UseHangfireServer(new BackgroundJobServerOptions()
            {
                ServerName = string.Format("{0}:facebookqueue", Environment.MachineName),
                Queues = new[] { "facebookqueue" },
                WorkerCount = 1
            });

            app.UseHangfireServer(new BackgroundJobServerOptions()
            {
                ServerName = string.Format("{0}:googlequeue", Environment.MachineName),
                Queues = new[] { "googlequeue" },
                WorkerCount = 1
            });


            RecurringJob.AddOrUpdate<IScheduledJobs>(recurringJobId: 
            "FacebookExtractorJobYesterday",
            methodCall: services => services.RecurringJobYesterday(null, 
            JobCancellationToken.Null),
            cronExpression: cronExp, timeZone: TimeZoneInfo.Local, queue: "facebookqueue");

            RecurringJob.AddOrUpdate<IGoogleScheduledJobs>(recurringJobId: 
            "GoogleExtractorJobYesterday",
             methodCall: services => services.RecurringJobYesterday(null, 
             JobCancellationToken.Null),
             cronExpression: cronExp, timeZone: TimeZoneInfo.Local, queue: "googlequeue");
}

上面的代码解决了我的目的,它添加了两个具有不同队列的服务器,每个队列都有一个工作人员计数,这样每个作业都可以 运行 在它自己的队列中,导致每个队列(服务器)只有一个工人算,同一份工作永远不会 运行 同时出现两次。

但是每当任何作业失败并尝试重试时,它都会在默认队列中排队。

我希望每个作业都应该在它自己的队列(服务器)中排队。

非常感谢您提供的任何帮助。

我找到了解决方案,我写下这个答案是为了帮助其他人。

唯一缺少 IScheduledJobs 接口上的 Queue 属性。

[Queue("facebookqueue")]
[AutomaticRetry(Attempts = 2)]
public interface IScheduledJobs
{

     Task<bool> RecurringJobYesterday(PerformContext context, 
       IJobCancellationToken cancellationToken);
}

[Queue("googlequeue")]
[AutomaticRetry(Attempts = 2)]
 public interface IGoogleScheduledJobs
 {
     Task<bool> RecurringJobYesterday(PerformContext context,
       IJobCancellationToken cancellationToken);
 }

在Startup.cs文件中

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
            app.UseHangfireDashboard("/scheduler", new DashboardOptions
            {
                Authorization = new[] { new HangFireAuthorization() },
                AppPath = "/"
            });

            app.UseHangfireServer(new BackgroundJobServerOptions()
            {
                ServerName = string.Format("{0}:facebookqueue", Environment.MachineName),
                Queues = new[] { "facebookqueue" },
                WorkerCount = 1
            });

            app.UseHangfireServer(new BackgroundJobServerOptions()
            {
                ServerName = string.Format("{0}:googlequeue", Environment.MachineName),
                Queues = new[] { "googlequeue" },
                WorkerCount = 1
            });


            RecurringJob.AddOrUpdate<IScheduledJobs>(recurringJobId: 
            "FacebookExtractorJobYesterday",
            methodCall: services => services.RecurringJobYesterday(null, 
            JobCancellationToken.Null),
            cronExpression: cronExp, timeZone: TimeZoneInfo.Local, queue: "facebookqueue");

            RecurringJob.AddOrUpdate<IGoogleScheduledJobs>(recurringJobId: 
            "GoogleExtractorJobYesterday",
             methodCall: services => services.RecurringJobYesterday(null, 
             JobCancellationToken.Null),
             cronExpression: cronExp, timeZone: TimeZoneInfo.Local, queue: "googlequeue");
}

现在,每当作业失败并自行重试时,它只会启动自己的队列,因为我们在每个作业的接口上方定义了 Queue 属性。