Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

RecurringJob triggers on startup, outside cron expression?

Open rasmuseeg opened this issue 5 years ago • 9 comments

I've got the follow code, which is inside my startup.cs class It is registered as expected, but it also triggers the the method to run. Even though I've specified between 2-3 AM. The job has not yet been finished successfully, with the last 24 hours. If the job was successfully run, within the last 24 hours, it appears to not run during startup.

How to i stop it from triggering outside the specified hours?

RecurringJob.AddOrUpdate<HangfireMediator>("local-xml-files",
                m => m.Send(new ImportFeedFilesCommand() { RunInBackground = false }),
                "0 2-3 * * *",
                TimeZoneInfo.Local
            );

rasmuseeg avatar Jun 17 '19 07:06 rasmuseeg

Hi @rasmuseeg,

This is a strange behavior, since I've never seen that happening for years. The AddOrUpdate method just register a recurring job, without execute anything else. I see 2 possible reasons for this behavior:

1 - You have already a HangfireMediator.Send() task enqueued in the database, so when the service starts, it is executed.

2 - You have another piece of code that is expliciting doing a Enqueue() or Schedule() of your method.

If you are able to provide a repository with this problem ocurring, I can try to help you figure it out 😄

Kind regards

rsilvanet avatar Jun 18 '19 16:06 rsilvanet

Hi there,

I'm having the same problem on the latest version. Here's a more or less simple repro:

  1. Schedule a new job to execute, say, at 16:00.
  2. Wait for the job to execute - LastExecution gets ~ 16:00.
  3. Schedule the same job so that the next occurrence (since LastExecution) of its cron is in the past - e. g. at 16:07, schedule the job to execute each 5 minutes (0/5 * * * *).
  4. NextExecution gets 16:05 - job executes immediately.

The same outcome, if the job was created at 16:00, but never executed.

GlobalConfiguration.Configuration.UseSqlServerStorage("***");
var backgroundServerOptions = new BackgroundJobServerOptions
{
    ServerName = $"{Environment.MachineName}.{Guid.NewGuid().ToString()}"
};
 
var server = new BackgroundJobServer(backgroundServerOptions);
 

RecurringJob.AddOrUpdate("Test", () => Job(), "0 16 * * *", TimeZoneInfo.Local); // change the cron to "0/5 * * * *" at step 3.

Expected result: The job should be scheduled to the next occurrence of the new cron since now.

tukaef avatar Aug 09 '19 14:08 tukaef

Funny I just came to same conclusion yesterday, but you beat me to it here. Thanks! 😁

rasmuseeg avatar Aug 09 '19 14:08 rasmuseeg

@tukaef, what about previous versions?

Hulkstance avatar Aug 16 '19 07:08 Hulkstance

any updates on this? I am getting the same issue.

zhiyuanzhang-unai avatar Oct 07 '19 19:10 zhiyuanzhang-unai

@rsilvanet @odinserj
Any updates on this? Please let me know if you need some extra information.

tukaef avatar Oct 14 '19 08:10 tukaef

I just experienced this. Our cron job is scheduled to run Mon-Sat. Our hangfire server was rebooted on Sunday and a task ran which we do not want to run on Sundays.

mrchen911 avatar Nov 25 '19 13:11 mrchen911

No updates on this?

jpinto3488 avatar Jan 21 '22 12:01 jpinto3488

any updates?

Pant76 avatar May 17 '22 07:05 Pant76