schedules.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.schedules
  4. ~~~~~~~~~~~~~~~~
  5. Schedules define the intervals at which periodic tasks
  6. should run.
  7. """
  8. from __future__ import absolute_import
  9. import re
  10. from datetime import datetime, timedelta
  11. from dateutil.relativedelta import relativedelta
  12. from . import current_app
  13. from .utils import is_iterable
  14. from .utils.timeutils import (
  15. timedelta_seconds, weekday, maybe_timedelta, remaining,
  16. humanize_seconds, timezone, maybe_make_aware
  17. )
  18. from .datastructures import AttributeDict
  19. class ParseException(Exception):
  20. """Raised by crontab_parser when the input can't be parsed."""
  21. class schedule(object):
  22. _app = None
  23. relative = False
  24. def __init__(self, run_every=None, relative=False, nowfun=None):
  25. self.run_every = maybe_timedelta(run_every)
  26. self.relative = relative
  27. self.nowfun = nowfun
  28. def now(self):
  29. return (self.nowfun or self.app.now)()
  30. def remaining_estimate(self, last_run_at):
  31. return remaining(last_run_at, self.run_every,
  32. self.maybe_make_aware(self.now()), self.relative)
  33. def is_due(self, last_run_at):
  34. """Returns tuple of two items `(is_due, next_time_to_run)`,
  35. where next time to run is in seconds.
  36. e.g.
  37. * `(True, 20)`, means the task should be run now, and the next
  38. time to run is in 20 seconds.
  39. * `(False, 12)`, means the task should be run in 12 seconds.
  40. You can override this to decide the interval at runtime,
  41. but keep in mind the value of :setting:`CELERYBEAT_MAX_LOOP_INTERVAL`,
  42. which decides the maximum number of seconds celerybeat can sleep
  43. between re-checking the periodic task intervals. So if you
  44. dynamically change the next run at value, and the max interval is
  45. set to 5 minutes, it will take 5 minutes for the change to take
  46. effect, so you may consider lowering the value of
  47. :setting:`CELERYBEAT_MAX_LOOP_INTERVAL` if responsiveness is of
  48. importance to you.
  49. .. admonition:: Scheduler max interval variance
  50. The default max loop interval may vary for different schedulers.
  51. For the default scheduler the value is 5 minutes, but for e.g.
  52. the django-celery database scheduler the value is 5 seconds.
  53. """
  54. last_run_at = self.maybe_make_aware(last_run_at)
  55. rem_delta = self.remaining_estimate(last_run_at)
  56. rem = timedelta_seconds(rem_delta)
  57. if rem == 0:
  58. return True, self.seconds
  59. return False, rem
  60. def maybe_make_aware(self, dt):
  61. if self.app.conf.CELERY_ENABLE_UTC:
  62. return maybe_make_aware(dt, self.tz)
  63. return dt
  64. def __repr__(self):
  65. return '<freq: %s>' % self.human_seconds
  66. def __eq__(self, other):
  67. if isinstance(other, schedule):
  68. return self.run_every == other.run_every
  69. return self.run_every == other
  70. @property
  71. def seconds(self):
  72. return timedelta_seconds(self.run_every)
  73. @property
  74. def human_seconds(self):
  75. return humanize_seconds(self.seconds)
  76. @property
  77. def app(self):
  78. if self._app is None:
  79. self._app = current_app._get_current_object()
  80. return self._app
  81. @property
  82. def tz(self):
  83. return self.app.conf.CELERY_TIMEZONE
  84. class crontab_parser(object):
  85. """Parser for crontab expressions. Any expression of the form 'groups'
  86. (see BNF grammar below) is accepted and expanded to a set of numbers.
  87. These numbers represent the units of time that the crontab needs to
  88. run on::
  89. digit :: '0'..'9'
  90. dow :: 'a'..'z'
  91. number :: digit+ | dow+
  92. steps :: number
  93. range :: number ( '-' number ) ?
  94. numspec :: '*' | range
  95. expr :: numspec ( '/' steps ) ?
  96. groups :: expr ( ',' expr ) *
  97. The parser is a general purpose one, useful for parsing hours, minutes and
  98. day_of_week expressions. Example usage::
  99. >>> minutes = crontab_parser(60).parse('*/15')
  100. [0, 15, 30, 45]
  101. >>> hours = crontab_parser(24).parse('*/4')
  102. [0, 4, 8, 12, 16, 20]
  103. >>> day_of_week = crontab_parser(7).parse('*')
  104. [0, 1, 2, 3, 4, 5, 6]
  105. It can also parse day_of_month and month_of_year expressions if initialized
  106. with an minimum of 1. Example usage::
  107. >>> days_of_month = crontab_parser(31, 1).parse('*/3')
  108. [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31]
  109. >>> months_of_year = crontab_parser(12, 1).parse('*/2')
  110. [1, 3, 5, 7, 9, 11]
  111. >>> months_of_year = crontab_parser(12, 1).parse('2-12/2')
  112. [2, 4, 6, 8, 10, 12]
  113. The maximum possible expanded value returned is found by the formula::
  114. max_ + min_ - 1
  115. """
  116. ParseException = ParseException
  117. _range = r'(\w+?)-(\w+)'
  118. _steps = r'/(\w+)?'
  119. _star = r'\*'
  120. def __init__(self, max_=60, min_=0):
  121. self.max_ = max_
  122. self.min_ = min_
  123. self.pats = (
  124. (re.compile(self._range + self._steps), self._range_steps),
  125. (re.compile(self._range), self._expand_range),
  126. (re.compile(self._star + self._steps), self._star_steps),
  127. (re.compile('^' + self._star + '$'), self._expand_star))
  128. def parse(self, spec):
  129. acc = set()
  130. for part in spec.split(','):
  131. if not part:
  132. raise self.ParseException('empty part')
  133. acc |= set(self._parse_part(part))
  134. return acc
  135. def _parse_part(self, part):
  136. for regex, handler in self.pats:
  137. m = regex.match(part)
  138. if m:
  139. return handler(m.groups())
  140. return self._expand_range((part, ))
  141. def _expand_range(self, toks):
  142. fr = self._expand_number(toks[0])
  143. if len(toks) > 1:
  144. to = self._expand_number(toks[1])
  145. return range(fr, min(to + 1, self.max_ + 1))
  146. return [fr]
  147. def _range_steps(self, toks):
  148. if len(toks) != 3 or not toks[2]:
  149. raise self.ParseException('empty filter')
  150. return self._expand_range(toks[:2])[::int(toks[2])]
  151. def _star_steps(self, toks):
  152. if not toks or not toks[0]:
  153. raise self.ParseException('empty filter')
  154. return self._expand_star()[::int(toks[0])]
  155. def _expand_star(self, *args):
  156. return range(self.min_, self.max_ + self.min_)
  157. def _expand_number(self, s):
  158. if isinstance(s, basestring) and s[0] == '-':
  159. raise self.ParseException('negative numbers not supported')
  160. try:
  161. i = int(s)
  162. except ValueError:
  163. try:
  164. i = weekday(s)
  165. except KeyError:
  166. raise ValueError("Invalid weekday literal '%s'." % s)
  167. if i < self.min_:
  168. raise ValueError('Invalid beginning range: %s < %s.' %
  169. (i, self.min_))
  170. return i
  171. class crontab(schedule):
  172. """A crontab can be used as the `run_every` value of a
  173. :class:`PeriodicTask` to add cron-like scheduling.
  174. Like a :manpage:`cron` job, you can specify units of time of when
  175. you would like the task to execute. It is a reasonably complete
  176. implementation of cron's features, so it should provide a fair
  177. degree of scheduling needs.
  178. You can specify a minute, an hour, a day of the week, a day of the
  179. month, and/or a month in the year in any of the following formats:
  180. .. attribute:: minute
  181. - A (list of) integers from 0-59 that represent the minutes of
  182. an hour of when execution should occur; or
  183. - A string representing a crontab pattern. This may get pretty
  184. advanced, like `minute='*/15'` (for every quarter) or
  185. `minute='1,13,30-45,50-59/2'`.
  186. .. attribute:: hour
  187. - A (list of) integers from 0-23 that represent the hours of
  188. a day of when execution should occur; or
  189. - A string representing a crontab pattern. This may get pretty
  190. advanced, like `hour='*/3'` (for every three hours) or
  191. `hour='0,8-17/2'` (at midnight, and every two hours during
  192. office hours).
  193. .. attribute:: day_of_week
  194. - A (list of) integers from 0-6, where Sunday = 0 and Saturday =
  195. 6, that represent the days of a week that execution should
  196. occur.
  197. - A string representing a crontab pattern. This may get pretty
  198. advanced, like `day_of_week='mon-fri'` (for weekdays only).
  199. (Beware that `day_of_week='*/2'` does not literally mean
  200. 'every two days', but 'every day that is divisible by two'!)
  201. .. attribute:: day_of_month
  202. - A (list of) integers from 1-31 that represents the days of the
  203. month that execution should occur.
  204. - A string representing a crontab pattern. This may get pretty
  205. advanced, such as `day_of_month='2-30/3'` (for every even
  206. numbered day) or `day_of_month='1-7,15-21'` (for the first and
  207. third weeks of the month).
  208. .. attribute:: month_of_year
  209. - A (list of) integers from 1-12 that represents the months of
  210. the year during which execution can occur.
  211. - A string representing a crontab pattern. This may get pretty
  212. advanced, such as `month_of_year='*/3'` (for the first month
  213. of every quarter) or `month_of_year='2-12/2'` (for every even
  214. numbered month).
  215. It is important to realize that any day on which execution should
  216. occur must be represented by entries in all three of the day and
  217. month attributes. For example, if `day_of_week` is 0 and `day_of_month`
  218. is every seventh day, only months that begin on Sunday and are also
  219. in the `month_of_year` attribute will have execution events. Or,
  220. `day_of_week` is 1 and `day_of_month` is '1-7,15-21' means every
  221. first and third monday of every month present in `month_of_year`.
  222. """
  223. @staticmethod
  224. def _expand_cronspec(cronspec, max_, min_=0):
  225. """Takes the given cronspec argument in one of the forms::
  226. int (like 7)
  227. basestring (like '3-5,*/15', '*', or 'monday')
  228. set (like set([0,15,30,45]))
  229. list (like [8-17])
  230. And convert it to an (expanded) set representing all time unit
  231. values on which the crontab triggers. Only in case of the base
  232. type being 'basestring', parsing occurs. (It is fast and
  233. happens only once for each crontab instance, so there is no
  234. significant performance overhead involved.)
  235. For the other base types, merely Python type conversions happen.
  236. The argument `max_` is needed to determine the expansion of '*'
  237. and ranges.
  238. The argument `min_` is needed to determine the expansion of '*'
  239. and ranges for 1-based cronspecs, such as day of month or month
  240. of year. The default is sufficient for minute, hour, and day of
  241. week.
  242. """
  243. if isinstance(cronspec, int):
  244. result = set([cronspec])
  245. elif isinstance(cronspec, basestring):
  246. result = crontab_parser(max_, min_).parse(cronspec)
  247. elif isinstance(cronspec, set):
  248. result = cronspec
  249. elif is_iterable(cronspec):
  250. result = set(cronspec)
  251. else:
  252. raise TypeError(
  253. 'Argument cronspec needs to be of any of the '
  254. 'following types: int, basestring, or an iterable type. '
  255. "'%s' was given." % type(cronspec))
  256. # assure the result does not preceed the min or exceed the max
  257. for number in result:
  258. if number >= max_ + min_ or number < min_:
  259. raise ValueError(
  260. 'Invalid crontab pattern. Valid '
  261. "range is %d-%d. '%d' was found." %
  262. (min_, max_ - 1 + min_, number))
  263. return result
  264. def _delta_to_next(self, last_run_at, next_hour, next_minute):
  265. """
  266. Takes a datetime of last run, next minute and hour, and
  267. returns a relativedelta for the next scheduled day and time.
  268. Only called when day_of_month and/or month_of_year cronspec
  269. is specified to further limit scheduled task execution.
  270. """
  271. from bisect import bisect, bisect_left
  272. datedata = AttributeDict(year=last_run_at.year)
  273. days_of_month = sorted(self.day_of_month)
  274. months_of_year = sorted(self.month_of_year)
  275. def day_out_of_range(year, month, day):
  276. try:
  277. datetime(year=year, month=month, day=day)
  278. except ValueError:
  279. return True
  280. return False
  281. def roll_over():
  282. while 1:
  283. flag = (datedata.dom == len(days_of_month) or
  284. day_out_of_range(datedata.year,
  285. months_of_year[datedata.moy],
  286. days_of_month[datedata.dom]))
  287. if flag:
  288. datedata.dom = 0
  289. datedata.moy += 1
  290. if datedata.moy == len(months_of_year):
  291. datedata.moy = 0
  292. datedata.year += 1
  293. else:
  294. break
  295. if last_run_at.month in self.month_of_year:
  296. datedata.dom = bisect(days_of_month, last_run_at.day)
  297. datedata.moy = bisect_left(months_of_year, last_run_at.month)
  298. else:
  299. datedata.dom = 0
  300. datedata.moy = bisect(months_of_year, last_run_at.month)
  301. roll_over()
  302. while not (datetime(year=datedata.year,
  303. month=months_of_year[datedata.moy],
  304. day=days_of_month[datedata.dom]
  305. ).isoweekday() % 7
  306. ) in self.day_of_week:
  307. datedata.dom += 1
  308. roll_over()
  309. return relativedelta(year=datedata.year,
  310. month=months_of_year[datedata.moy],
  311. day=days_of_month[datedata.dom],
  312. hour=next_hour,
  313. minute=next_minute,
  314. second=0,
  315. microsecond=0)
  316. def __init__(self, minute='*', hour='*', day_of_week='*',
  317. day_of_month='*', month_of_year='*', nowfun=None):
  318. self._orig_minute = minute
  319. self._orig_hour = hour
  320. self._orig_day_of_week = day_of_week
  321. self._orig_day_of_month = day_of_month
  322. self._orig_month_of_year = month_of_year
  323. self.hour = self._expand_cronspec(hour, 24)
  324. self.minute = self._expand_cronspec(minute, 60)
  325. self.day_of_week = self._expand_cronspec(day_of_week, 7)
  326. self.day_of_month = self._expand_cronspec(day_of_month, 31, 1)
  327. self.month_of_year = self._expand_cronspec(month_of_year, 12, 1)
  328. self.nowfun = nowfun
  329. def now(self):
  330. return (self.nowfun or self.app.now)()
  331. def __repr__(self):
  332. return ('<crontab: %s %s %s %s %s (m/h/d/dM/MY)>' %
  333. (self._orig_minute or '*',
  334. self._orig_hour or '*',
  335. self._orig_day_of_week or '*',
  336. self._orig_day_of_month or '*',
  337. self._orig_month_of_year or '*'))
  338. def __reduce__(self):
  339. return (self.__class__, (self._orig_minute,
  340. self._orig_hour,
  341. self._orig_day_of_week,
  342. self._orig_day_of_month,
  343. self._orig_month_of_year), None)
  344. def remaining_estimate(self, last_run_at, tz=None):
  345. """Returns when the periodic task should run next as a timedelta."""
  346. tz = tz or self.tz
  347. last_run_at = self.maybe_make_aware(last_run_at)
  348. dow_num = last_run_at.isoweekday() % 7 # Sunday is day 0, not day 7
  349. execute_this_date = (last_run_at.month in self.month_of_year and
  350. last_run_at.day in self.day_of_month and
  351. dow_num in self.day_of_week)
  352. execute_this_hour = (execute_this_date and
  353. last_run_at.hour in self.hour and
  354. last_run_at.minute < max(self.minute))
  355. if execute_this_hour:
  356. next_minute = min(minute for minute in self.minute
  357. if minute > last_run_at.minute)
  358. delta = relativedelta(minute=next_minute,
  359. second=0,
  360. microsecond=0)
  361. else:
  362. next_minute = min(self.minute)
  363. execute_today = (execute_this_date and
  364. last_run_at.hour < max(self.hour))
  365. if execute_today:
  366. next_hour = min(hour for hour in self.hour
  367. if hour > last_run_at.hour)
  368. delta = relativedelta(hour=next_hour,
  369. minute=next_minute,
  370. second=0,
  371. microsecond=0)
  372. else:
  373. next_hour = min(self.hour)
  374. all_dom_moy = (self._orig_day_of_month == '*' and
  375. self._orig_month_of_year == '*')
  376. if all_dom_moy:
  377. next_day = min([day for day in self.day_of_week
  378. if day > dow_num] or
  379. self.day_of_week)
  380. add_week = next_day == dow_num
  381. delta = relativedelta(weeks=add_week and 1 or 0,
  382. weekday=(next_day - 1) % 7,
  383. hour=next_hour,
  384. minute=next_minute,
  385. second=0,
  386. microsecond=0)
  387. else:
  388. delta = self._delta_to_next(last_run_at,
  389. next_hour, next_minute)
  390. return remaining(timezone.to_local(last_run_at, tz),
  391. delta, timezone.to_local(self.now(), tz))
  392. def is_due(self, last_run_at):
  393. """Returns tuple of two items `(is_due, next_time_to_run)`,
  394. where next time to run is in seconds.
  395. See :meth:`celery.schedules.schedule.is_due` for more information.
  396. """
  397. rem_delta = self.remaining_estimate(last_run_at)
  398. rem = timedelta_seconds(rem_delta)
  399. due = rem == 0
  400. if due:
  401. rem_delta = self.remaining_estimate(self.now())
  402. rem = timedelta_seconds(rem_delta)
  403. return due, rem
  404. def __eq__(self, other):
  405. if isinstance(other, crontab):
  406. return (other.month_of_year == self.month_of_year and
  407. other.day_of_month == self.day_of_month and
  408. other.day_of_week == self.day_of_week and
  409. other.hour == self.hour and
  410. other.minute == self.minute)
  411. return other is self
  412. def maybe_schedule(s, relative=False):
  413. if isinstance(s, int):
  414. s = timedelta(seconds=s)
  415. if isinstance(s, timedelta):
  416. return schedule(s, relative)
  417. return s