【问题标题】:Multiple queues(Servers) with different worker counts in HangfireHangfire中具有不同工作人员数量的多个队列(服务器)
【发布时间】:2020-05-13 18:07:27
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: asp.net-core hangfire


    【解决方案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 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      相关资源
      最近更新 更多