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

Dur*_*dha 8 hangfire asp.net-core

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

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

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

但是,每当任何作业失败并尝试重试时,它都会在默认队列中排队。 在此输入图像描述

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

我将非常感谢任何形式的帮助。

Dur*_*dha 11

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

唯一的问题是 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);
 }
Run Code Online (Sandbox Code Playgroud)

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

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