Hangfire how to write crontab if there is a Start Date and End Date?

1.2k Views Asked by At

I understand that crontab expression doesn't have the 'Year' expression. But I would like to know, if there is a workaround or something which can enable the Hangfire to start/stop scheduling at a specified Year.

For example, given a case:

  • Assume current date is 2016, September 15

  • Scheduler Starts on 2016, December, 1

  • Repeat every hour from 8.30am till 5.30pm daily (office hour)

  • Happens on weekday Monday-Friday

  • End on 2018, Februrary, 1

Understood from other post that, it may require more than 1 crontab expression in some cases. But for this one, i have no idea how to write. Reason:

if i write:

  • "30 8 1 12 0-4": it only specifies December 1
  • "30 8 * * 0-4": it doesn't tell which month to start or end

How to write this crontab?

1

There are 1 best solutions below

6
On BEST ANSWER

Not sure of a clean way. But it could be handled in this manner- 5 jobs:

2 Delayed Jobs to run on Start Date and End Date(StartDateDelayedJob, EndDateDelayedJob)

3 Recurring Jobs -

  1. 1st with cron expression (* */1 * * 1-5) to run the expected task every hour from monday to friday,
  2. 2nd with cron expression (30 8 * * 1-5) to add the 1st recurrence job every day at 8 30 if it is monday to friday,
  3. 3rd with cron expression of (30 17 * * 1-5) to remove the 1st recurring job every day at 5 30 if it is monday to friday.

StartDateDelayedJob will add 2nd Recurrence Job and 3rd Recurrence Job.

EndDateDelayedJob will remove the two Recurrence Jobs.

Hope this helps