Reputation: 339
I was trying to create multiple queues and each queue should have only one worker count. So I did the bellow code:
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");
}
The above code solved my purpose and it added two servers with different queues and every queue has one worker count, So that every job can run in it's own queue and cause every queue (server) has only one worker count, the same job will never run twice at same time.
But whenever any of the job fails and attempts to retry, it en-queued in default queue.
I want that every job should en-queue in it's own queue (server).
I'll be really thankful for any kind of help.
Upvotes: 11
Views: 3276
Reputation: 339
I found the solution, I'm writing this answer so it can help others.
The only thing was missing Queue attribute on IScheduledJobs interface.
[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);
}
In Startup.cs file
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");
}
Now, whenever job fails and retries itself, it will only start it its own queue because we defined Queue attribute above every job's interface.
Upvotes: 15