Hello, I am unable to get the DisableConcurrentExecution attribute to work as I expect. I have defined a simple job like so:
public class NotificationController
{
private readonly NotificationSender _notificationSender;
public NotificationController(NotificationSender notificationSender)
{
_notificationSender = notificationSender;
}
[DisableConcurrentExecution(600)]
public void SendNotifications()
{
_notificationSender.SendNotificationsDue();
}
}
My method is invoked both through the front-end like so:
BackgroundJob.Enqueue<NotificationController>(controller => controller.SendNotifications());
And also registered as a recurring task like so:
RecurringJob.AddOrUpdate<NotificationController>("NotificationSender", controller => controller.SendNotifications(), Cron.Minutely);
Unfortunately, if the process lasts longer than a minute (which sometimes happens), another instance of NotificationSender is automatically added to the queue and immediately processed. After several minutes, there are many SendNotification methods running concurrently which can lead to race conditions in which multiple notifications are sent.
I have also tried adding the attribute to the controller as well but this had no effect. Also, this isn’t just limited to the recurring jobs; I have added a button to send notifications manually, and if I click it several times, Hangfire will also run many concurrent instances of SendNotification.
For now I’m going to hack around it by using a static variable that will immediately quit the function if it’s already set; fortunately we’ve only got one Hangfire node to start with but we will soon have many and my hack won’t work in that case.
I’ve tried investigating the source code to determine if there is a bug; I can see a lock is being inserted but I can’t see what checks for this lock. Before I try to debug closer though I’d like to make sure that I haven’t done anything wrong.
My system specs:
- Hangfire 1.0.2
- Hangfire SQLServer 1.0.2
- Hangfire Ninject 1.0.0
- Hosted within a console application
Aside from that, everything has been great with Hangfire, I was able to convert a legacy WCF MSMQ queue service app in about a day and a bit. Thanks for the top framework!