Repetitive Cutting Tasks

Is there a way to set temporary hangfire jobs every few seconds? I am not looking for a solution where the task of fire and forgetting creates another fire and forgets the task, and if not, what alternatives are proposed?

+5
source share
4 answers

Hangfire did not support less than a minute for repetitive jobs. What for? Imagine if they allow less than a minute, say 1 second. How often should I delay checking for duplicate jobs in db? The bottom line is that it hurts your database. See .

+2
source

I think someone who does not allow a repeated trigger of less than 1 minute is shortsighted. After all, 55 seconds are less effective than 1 min? It seems so arbitrary! As much as I love Hangfire, I came across situations where I had to manage the Quartz.net client simply because I needed a business task to work every 55 seconds or the like.

Anyone who makes the counter argument that if it is configured to run every 1 second can seriously affect performance, as it closes the view of things again. Of course, a trigger with an interval of 1 second is probably not a good idea, but can we not save 55 seconds or 45 seconds for the unlikely situation when someone chooses 1 second?

In any case, the performance is both subjective and dependent on the host platform and equipment. It's really not up to the API to enforce opinions when it comes to performance. Just adjust the polling interval and try starting again. Thus, the user can determine the best result for himself.

Although a background process that organizes a task to run every 55 seconds may be an option, this is not very satisfactory. In this case, the process is not displayed through the Hangfire interface, so it is hidden from the administrator. I feel this approach bypasses one of the main 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 of less than 1 minute, than Hangfire cannot!

+3
source

I ran into the same problem, and here is my solution:

private void TimingExecuteWrapper(Action action, int sleepSeconds, int intervalSeconds) { DateTime beginTime = DateTime.UtcNow, endTime; var interval = TimeSpan.FromSeconds(intervalSeconds); while (true) { action(); Thread.Sleep(TimeSpan.FromSeconds(sleepSeconds)); endTime = DateTime.UtcNow; if (endTime - beginTime >= interval) break; } } 

intervalSeconds - The minimum NCRON interval. This is 1 minute. action is our work code. I also suggest using DisableConcurrentExecution to avoid some concurrency collisions.

0
source

Despite the fact that Hangfire does not allow you to schedule tasks for less than a minute, you can really achieve this if the function plans itself recursively; for example, you want a method to hit every second, you can schedule a background task that calls the method at startup;

 BackgroundJob.Schedule(() => PublishMessage(), TimeSpan.FromMilliseconds(2000)); 

And then, in your PublishMessage method, do your stuff and then pay the job to call the same method;

 public void PublishMessage() { /* do your stuff */ //then schedule a job to exec the same method BackgroundJob.Schedule(() => PublishMessage(), TimeSpan.FromMilliseconds(2000)); } 

Another thing you need to override is the default SchedulePollingInterval by default is 15, otherwise your method will only hit every 15 seconds. To do this, simply pass an instance of BackgroundJobServerOptions for UseHangfireServer at your startup, for example:

 var options = new BackgroundJobServerOptions { SchedulePollingInterval = TimeSpan.FromMilliseconds(2000) }; app.UseHangfireServer(options); 

I do not know how “faultlessly” my decision is, but I managed to achieve my goal, and everything is “happily” in production.

0
source

All Articles