Tuesday, January 25, 2011

Python APScheduler does not follow crontab conventions...

If you've ever used Python's APScheduler, you may try to specify the day of week for jobs to trigger and wonder why it doesn't fire on the scheduled date/time. Naturally you might look at the crontab format to decide how day of week is indexed.

http://adminschoice.com/crontab-quick-reference

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

If you look inside apscheduler/expressions.py, the days are indexed from 0-6 with Monday representing the first day. See http://packages.python.org/APScheduler/cronschedule.html

WEEKDAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']

The accompanying regex expressions are also supported:
value_re = re.compile(r'(?P[a-z]+)(?:-(?P[a-z]+))', re.IGNORECASE)

value_re = re.compile(r'(?P

They correspond to the following:
Expression types
The following table lists all the available expressions applicable in cron-style schedules.

Expression Field Description
* any Fire on every value
*/a any Fire every a values, starting from the minimum
a-b any Fire on any value within the a-b range (a must be smaller than b)
a-b/c any Fire every c values within the a-b range
xth y day Fire on the x -th occurrence of weekday y within the month
last x day Fire on the last occurrence of weekday x within the month
x,y,z any Fire on any matching expression; can combine any number of any of the above expressions

No comments:

Post a Comment