4

Is it possible to schedule an airflow DAG to run at a specific time on the Monday directly before the 15th of each month? I think this cron string might do it but I'm not sure that I have understood correctly

0 10 8-14 * MON

So I think that this should run at 10:00 on a Monday only between the 8th and the 14th of each month. As there can only be one Monday between the 8th and the 14th, this should run only once a month and it will be the Monday preceding the 15th of the month.

Is that correct?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    You can check the schedule here: https://crontab.guru/ – tobi6 Sep 06 '18 at 12:23
  • 1
    @tobi6 thanks, I'm aware of that page. But it just writes out the cron string in words, it doesn't verify that those words match my intended logic. I'm just tyring to confirm that this will execute on the Monday before the 15th of every month. Is there some edge case I've missed where it accidentally runs twice in a month for example? – Dan Sep 06 '18 at 15:04

1 Answers1

15

The croniter module (which Airflow uses for the execution date/time calculations) supports the hash symbol for the day-of-week field which would allow you to schedule, what I believe will work, the second Monday of each month.

For example, "30 7 * * 1#2" says to run at 7:30AM, every month, on the second Monday. Using this code to test it:

from croniter import croniter 
from datetime import datetime 
cron = croniter("30 7 * * 1#2") 
for i in range(10):
    print(cron.get_next(datetime))

yields:

datetime.datetime(2018, 10, 8, 7, 30)
datetime.datetime(2018, 11, 12, 7, 30)
datetime.datetime(2018, 12, 10, 7, 30)
datetime.datetime(2019, 1, 14, 7, 30)
datetime.datetime(2019, 2, 11, 7, 30)
datetime.datetime(2019, 3, 11, 7, 30)
datetime.datetime(2019, 4, 8, 7, 30)
datetime.datetime(2019, 5, 13, 7, 30)
datetime.datetime(2019, 6, 10, 7, 30)
datetime.datetime(2019, 7, 8, 7, 30)
joebeeson
  • 4,159
  • 1
  • 22
  • 29
  • How did you generate the test dates? – Dan Sep 13 '18 at 19:58
  • 2
    from croniter import croniter from datetime import datetime cron = croniter("30 7 * * 1#2") print(cron.get_next(datetime)) # repeat – joebeeson Sep 13 '18 at 19:59
  • Thanks, using your `croniter` code I see that it takes my cron string as between the 8th and 14th **OR** a Monday as opposed to how I thought it would work as in **AND** a Monday. So I'll try out your tip using `#`. – Dan Sep 13 '18 at 22:37