Reputation: 394
I'm new with Hangfire and I want to know if this can be achievable with the tool:
I have to send emails on a daily basis to a list of people that I have in my SQL DB. The thing is my that my web app (although it only uses one DB), is deployed through Amazon with load balancers that might add servers (if needed).
I want the hangfire task to execute only once per day.
My first idea was to use Hangfire to register the recurrent task on my Application_Start method, but this will not work, right? How can I register my recurrent task in a way that, even when I have multiple servers running, the task gets executed only once? I've read in other posts that I could do some tricks with the DB: having a "table" that registers if the task is already executed or not, but I think that kind of solution will not scale if I must have, let's say, 10 tasks to complete per day.
Another way I'm thinking is to have a separate "app" for the hangfire execution, that only deploys in one server. Since my web app is using a dll to access the DB, I could do the same for this new "app" and to grant access to the db with the same dll, but I'll have to update my app every time I change the dll.
Any thoughts will be appreciated. Thanks!
Upvotes: 2
Views: 1421
Reputation: 1677
There is no problem with having multiple "task" servers running Hangfire. It's designed for that.
Hangfire.RecurringJob.AddOrUpdate<YourJobClass>(YourEmailJobID, x => x.YourMethodCall(anyParameter), Cron.Daily);
Check out the Cron object for more options. Just make sure you use the same ID for YourEmailJobID and it will work as you intended. You could call that method a hundred times from a hundred different servers and as long as you pass in the same parameters, the job entry in the Hangfire DB won't change.
Also, if you have concurrent jobs running, but only want one of your task servers to execute the job at one time, use this attribute on your Hangfire method.
[Hangfire.DisableConcurrentExecution(60)]
public void YourMethodCall(object anyParameter)
{
....
}
Upvotes: 3