Search code examples
cronhangfire

Cron expression not correct for Hangfire Recurring Jobs


I have a few jobs configured to be executed on an ASP.NET MVC Site through Hangfire. Hangfire uses a cron expression to trigger said jobs. I thought I had the expression correct, but the triggering is happening too often. Image of cron schedule

As you can see, the top job just finished executing 6 minutes previously and its next execution is expected to happen in an hour. But the cron expression, as I understand it, says it should only trigger once a day at 2 AM. Even accounting for different time zones, it shouldn't be happening on an hourly basis.

Do I have the cron expression incorrect? I will admit to not being completely knowledgeable about that, though from my reading I thought I had it correct.

Thanks


Solution

  • Old question, but every time I forget this, I end up back here...

    Hangfire (at least the version pictured) uses NCrontab (https://github.com/atifaziz/NCrontab) which allows CRON expressions of 5 or 6 parts. If you use 6 parts, the first part is seconds, the second part is minutes. This is why the job in the pictured example was ran at 3:02.

    Most answers I have run across seem to only show the 5 part version of CRON expressions.

    From the above URL:

    * * * * * *
    - - - - - -
    | | | | | |
    | | | | | +--- day of week (0 - 6) (Sunday=0)
    | | | | +----- month (1 - 12)
    | | | +------- day of month (1 - 31)
    | | +--------- hour (0 - 23)
    | +----------- min (0 - 59)
    +------------- sec (0 - 59)
    

    To trigger once daily at 2am would be 0 0 2 * * ?