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

时间:2019-09-19 11:56:16

标签: asp.net-core 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");
}

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

但是,每当任何作业失败并尝试重试时,它就会进入默认队列。 enter image description here

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

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

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,我正在写此答案,以便它可以帮助其他人。

唯一的事情是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属性。