Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hangfire recurring tasks under minute

Tags:

c#

.net

hangfire

Is there a way to set hangfire recurring jobs every few seconds? I do not seek a solution where fire and forget task creates another fire and forget task, and if not, what are suggested alternatives?

like image 984
Robert Avatar asked Jul 14 '16 06:07

Robert


People also ask

How do I create a recurring job on hangfire?

Recurring job registration is almost as simple as background job registration – you need to write a single line of code, but you also need to specify an identifier you can use to refer to your job later. The call to AddOrUpdate method will create a new recurring job or update existing job with the same identifier.

How do I stop a recurring job on hangfire?

Each iteration of your job would check that value, and if it indicates a Stop Request, then exit the loop. You can achieve this by using IMonitorinApi and fetch jobs that are processing. Then you can delete them by using IBackgroundJobClient. You need to pass in a cancellationtoken though.

What is a recurring job?

Recurring jobs are used for: Jobs that have multiple visits with a repeating schedule. Eg. You visit the client every Tuesday. Jobs where you invoice your client periodically Eg.

What can be used to handle hangfire events?

Logging. Hangfire uses the Common. Logging library to log all its events. It is a generic library and you can plug it to your logging framework using adapters.


2 Answers

Not sure when this became supported but tried this in ASP.NET Core 2.0 with Hangfire 1.7.0. The following code schedules a job every 20 seconds:

RecurringJob.AddOrUpdate<SomeJob>(     x => x.DoWork(),     "*/20 * * * * *"); 

If I am not mistaken 6 tokens (as opposed to standard 5 tokens) is supported due to Hangfire use of NCrontab which allows cron expressions with 6 tokens (second granularity instead of minute granularity).

Hangfire dashboard also nicely shows the small time interval between runs:

Hangfire dashboard

like image 52
Alexei - check Codidact Avatar answered Sep 22 '22 01:09

Alexei - check Codidact


I think anyone who is against allowing a recurring trigger of less than 1 min is short sighted. After all, is 55 secs any less efficient than 1 min ? It seems so arbitrary! As much as I love Hangfire, I've encountered situations where I've had to steer a client to Quartz.net simply because there was a business requirement for a job to run every 55 secs or alike.

Anyone who makes the counter argument that if it was configured to run every 1sec it would have a serious impact on performance is again taking a closed view on things. Of course a trigger with a 1 sec interval is probably not a good idea, but do we dissalow 55 sec or 45 sec for the unlikely situation where someone will choose 1 sec ?

In any case, performance is both subjective and dependent on the host platform and hardware. It really isn't up to the API to enforce opinion when it comes to performance. Just make the polling interval and trigger recurrence configurable. That way the user can determine the best result for themselves.

Although a background process which is orchestrating a job to run every 55 sec may be an option, it isn't very satisfactory. In this case, the process isn't visible via the Hangfire UI so it's hidden from the administrator. I feel this approach is circumventing one of the major benefits of Hangfire.

If Hangfire was a serious competitor to the likes of Quartz.net it would at least match their basic functionality. If Quartz can support triggers with an interval below 1 min than why can't Hangfire!

like image 41
PatrickNolan Avatar answered Sep 20 '22 01:09

PatrickNolan