Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecurringJob triggers on startup, outside cron expression? #1448

Open
rasmuseeg opened this issue Jun 17, 2019 · 9 comments
Open

RecurringJob triggers on startup, outside cron expression? #1448

rasmuseeg opened this issue Jun 17, 2019 · 9 comments

Comments

@rasmuseeg
Copy link

rasmuseeg commented Jun 17, 2019

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
            );
@rsilvanet
Copy link
Contributor

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

@tukaef
Copy link

tukaef commented Aug 9, 2019

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.

@rasmuseeg
Copy link
Author

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

@Hulkstance
Copy link

@tukaef, what about previous versions?

@zhiyuanzhang-unai
Copy link

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

@tukaef
Copy link

tukaef commented Oct 14, 2019

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

@mrchen911
Copy link

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.

@jpinto3488
Copy link

No updates on this?

@Pant76
Copy link

Pant76 commented May 17, 2022

any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

9 participants