schedules.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.schedules
  4. ~~~~~~~~~~~~~~~~
  5. Schedules define the intervals at which periodic tasks
  6. should run.
  7. :copyright: (c) 2009 - 2011 by Ask Solem.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. from __future__ import absolute_import
  11. import re
  12. from datetime import datetime, timedelta
  13. from dateutil.relativedelta import relativedelta
  14. from .utils import is_iterable
  15. from .utils.timeutils import (timedelta_seconds, weekday,
  16. remaining, humanize_seconds)
  17. class ParseException(Exception):
  18. """Raised by crontab_parser when the input can't be parsed."""
  19. class schedule(object):
  20. relative = False
  21. def __init__(self, run_every=None, relative=False):
  22. self.run_every = run_every
  23. self.relative = relative
  24. def remaining_estimate(self, last_run_at):
  25. """Returns when the periodic task should run next as a timedelta."""
  26. return remaining(last_run_at, self.run_every, relative=self.relative)
  27. def is_due(self, last_run_at):
  28. """Returns tuple of two items `(is_due, next_time_to_run)`,
  29. where next time to run is in seconds.
  30. e.g.
  31. * `(True, 20)`, means the task should be run now, and the next
  32. time to run is in 20 seconds.
  33. * `(False, 12)`, means the task should be run in 12 seconds.
  34. You can override this to decide the interval at runtime,
  35. but keep in mind the value of :setting:`CELERYBEAT_MAX_LOOP_INTERVAL`,
  36. which decides the maximum number of seconds celerybeat can sleep
  37. between re-checking the periodic task intervals. So if you
  38. dynamically change the next run at value, and the max interval is
  39. set to 5 minutes, it will take 5 minutes for the change to take
  40. effect, so you may consider lowering the value of
  41. :setting:`CELERYBEAT_MAX_LOOP_INTERVAL` if responsiveness is of
  42. importance to you.
  43. """
  44. rem_delta = self.remaining_estimate(last_run_at)
  45. rem = timedelta_seconds(rem_delta)
  46. if rem == 0:
  47. return True, timedelta_seconds(self.run_every)
  48. return False, rem
  49. def __repr__(self):
  50. return "<freq: %s>" % (
  51. humanize_seconds(timedelta_seconds(self.run_every)), )
  52. def __eq__(self, other):
  53. if isinstance(other, schedule):
  54. return self.run_every == other.run_every
  55. return self.run_every == other
  56. class crontab_parser(object):
  57. """Parser for crontab expressions. Any expression of the form 'groups'
  58. (see BNF grammar below) is accepted and expanded to a set of numbers.
  59. These numbers represent the units of time that the crontab needs to
  60. run on::
  61. digit :: '0'..'9'
  62. dow :: 'a'..'z'
  63. number :: digit+ | dow+
  64. steps :: number
  65. range :: number ( '-' number ) ?
  66. numspec :: '*' | range
  67. expr :: numspec ( '/' steps ) ?
  68. groups :: expr ( ',' expr ) *
  69. The parser is a general purpose one, useful for parsing hours, minutes and
  70. day_of_week expressions. Example usage::
  71. >>> minutes = crontab_parser(60).parse("*/15")
  72. [0, 15, 30, 45]
  73. >>> hours = crontab_parser(24).parse("*/4")
  74. [0, 4, 8, 12, 16, 20]
  75. >>> day_of_week = crontab_parser(7).parse("*")
  76. [0, 1, 2, 3, 4, 5, 6]
  77. """
  78. ParseException = ParseException
  79. _range = r'(\w+?)-(\w+)'
  80. _steps = r'/(\w+)?'
  81. _star = r'\*'
  82. def __init__(self, max_=60):
  83. self.max_ = max_
  84. self.pats = (
  85. (re.compile(self._range + self._steps), self._range_steps),
  86. (re.compile(self._range), self._expand_range),
  87. (re.compile(self._star + self._steps), self._star_steps),
  88. (re.compile('^' + self._star + '$'), self._expand_star))
  89. def parse(self, spec):
  90. acc = set()
  91. for part in spec.split(','):
  92. if not part:
  93. raise self.ParseException("empty part")
  94. acc |= set(self._parse_part(part))
  95. return acc
  96. def _parse_part(self, part):
  97. for regex, handler in self.pats:
  98. m = regex.match(part)
  99. if m:
  100. return handler(m.groups())
  101. return self._expand_range((part, ))
  102. def _expand_range(self, toks):
  103. fr = self._expand_number(toks[0])
  104. if len(toks) > 1:
  105. to = self._expand_number(toks[1])
  106. return range(fr, min(to + 1, self.max_ + 1))
  107. return [fr]
  108. def _range_steps(self, toks):
  109. if len(toks) != 3 or not toks[2]:
  110. raise self.ParseException("empty filter")
  111. return self._filter_steps(self._expand_range(toks[:2]), int(toks[2]))
  112. def _star_steps(self, toks):
  113. if not toks or not toks[0]:
  114. raise self.ParseException("empty filter")
  115. return self._filter_steps(self._expand_star(), int(toks[0]))
  116. def _filter_steps(self, numbers, steps):
  117. return [n for n in numbers if n % steps == 0]
  118. def _expand_star(self, *args):
  119. return range(self.max_)
  120. def _expand_number(self, s):
  121. if isinstance(s, basestring) and s[0] == '-':
  122. raise self.ParseException("negative numbers not supported")
  123. try:
  124. i = int(s)
  125. except ValueError:
  126. try:
  127. i = weekday(s)
  128. except KeyError:
  129. raise ValueError("Invalid weekday literal '%s'." % s)
  130. return i
  131. class crontab(schedule):
  132. """A crontab can be used as the `run_every` value of a
  133. :class:`PeriodicTask` to add cron-like scheduling.
  134. Like a :manpage:`cron` job, you can specify units of time of when
  135. you would like the task to execute. It is a reasonably complete
  136. implementation of cron's features, so it should provide a fair
  137. degree of scheduling needs.
  138. You can specify a minute, an hour, and/or a day of the week in any
  139. of the following formats:
  140. .. attribute:: minute
  141. - A (list of) integers from 0-59 that represent the minutes of
  142. an hour of when execution should occur; or
  143. - A string representing a crontab pattern. This may get pretty
  144. advanced, like `minute="*/15"` (for every quarter) or
  145. `minute="1,13,30-45,50-59/2"`.
  146. .. attribute:: hour
  147. - A (list of) integers from 0-23 that represent the hours of
  148. a day of when execution should occur; or
  149. - A string representing a crontab pattern. This may get pretty
  150. advanced, like `hour="*/3"` (for every three hours) or
  151. `hour="0,8-17/2"` (at midnight, and every two hours during
  152. office hours).
  153. .. attribute:: day_of_week
  154. - A (list of) integers from 0-6, where Sunday = 0 and Saturday =
  155. 6, that represent the days of a week that execution should
  156. occur.
  157. - A string representing a crontab pattern. This may get pretty
  158. advanced, like `day_of_week="mon-fri"` (for weekdays only).
  159. (Beware that `day_of_week="*/2"` does not literally mean
  160. "every two days", but "every day that is divisible by two"!)
  161. """
  162. @staticmethod
  163. def _expand_cronspec(cronspec, max_):
  164. """Takes the given cronspec argument in one of the forms::
  165. int (like 7)
  166. basestring (like '3-5,*/15', '*', or 'monday')
  167. set (like set([0,15,30,45]))
  168. list (like [8-17])
  169. And convert it to an (expanded) set representing all time unit
  170. values on which the crontab triggers. Only in case of the base
  171. type being 'basestring', parsing occurs. (It is fast and
  172. happens only once for each crontab instance, so there is no
  173. significant performance overhead involved.)
  174. For the other base types, merely Python type conversions happen.
  175. The argument `max_` is needed to determine the expansion of '*'.
  176. """
  177. if isinstance(cronspec, int):
  178. result = set([cronspec])
  179. elif isinstance(cronspec, basestring):
  180. result = crontab_parser(max_).parse(cronspec)
  181. elif isinstance(cronspec, set):
  182. result = cronspec
  183. elif is_iterable(cronspec):
  184. result = set(cronspec)
  185. else:
  186. raise TypeError(
  187. "Argument cronspec needs to be of any of the "
  188. "following types: int, basestring, or an iterable type. "
  189. "'%s' was given." % type(cronspec))
  190. # assure the result does not exceed the max
  191. for number in result:
  192. if number >= max_:
  193. raise ValueError(
  194. "Invalid crontab pattern. Valid "
  195. "range is 0-%d. '%d' was found." % (max_ - 1, number))
  196. return result
  197. def __init__(self, minute='*', hour='*', day_of_week='*',
  198. nowfun=datetime.now):
  199. self._orig_minute = minute
  200. self._orig_hour = hour
  201. self._orig_day_of_week = day_of_week
  202. self.hour = self._expand_cronspec(hour, 24)
  203. self.minute = self._expand_cronspec(minute, 60)
  204. self.day_of_week = self._expand_cronspec(day_of_week, 7)
  205. self.nowfun = nowfun
  206. def __repr__(self):
  207. return "<crontab: %s %s %s (m/h/d)>" % (self._orig_minute or "*",
  208. self._orig_hour or "*",
  209. self._orig_day_of_week or "*")
  210. def __reduce__(self):
  211. return (self.__class__, (self._orig_minute,
  212. self._orig_hour,
  213. self._orig_day_of_week), None)
  214. def remaining_estimate(self, last_run_at):
  215. """Returns when the periodic task should run next as a timedelta."""
  216. weekday = last_run_at.isoweekday()
  217. weekday = 0 if weekday == 7 else weekday # Sunday is day 0, not day 7.
  218. execute_this_hour = (weekday in self.day_of_week and
  219. last_run_at.hour in self.hour and
  220. last_run_at.minute < max(self.minute))
  221. if execute_this_hour:
  222. next_minute = min(minute for minute in self.minute
  223. if minute > last_run_at.minute)
  224. delta = relativedelta(minute=next_minute,
  225. second=0,
  226. microsecond=0)
  227. else:
  228. next_minute = min(self.minute)
  229. execute_today = (weekday in self.day_of_week and
  230. last_run_at.hour < max(self.hour))
  231. if execute_today:
  232. next_hour = min(hour for hour in self.hour
  233. if hour > last_run_at.hour)
  234. delta = relativedelta(hour=next_hour,
  235. minute=next_minute,
  236. second=0,
  237. microsecond=0)
  238. else:
  239. next_hour = min(self.hour)
  240. next_day = min([day for day in self.day_of_week
  241. if day > weekday] or
  242. self.day_of_week)
  243. add_week = next_day == weekday
  244. delta = relativedelta(weeks=add_week and 1 or 0,
  245. weekday=(next_day - 1) % 7,
  246. hour=next_hour,
  247. minute=next_minute,
  248. second=0,
  249. microsecond=0)
  250. return remaining(last_run_at, delta, now=self.nowfun())
  251. def is_due(self, last_run_at):
  252. """Returns tuple of two items `(is_due, next_time_to_run)`,
  253. where next time to run is in seconds.
  254. See :meth:`celery.schedules.schedule.is_due` for more information.
  255. """
  256. rem_delta = self.remaining_estimate(last_run_at)
  257. rem = timedelta_seconds(rem_delta)
  258. due = rem == 0
  259. if due:
  260. rem_delta = self.remaining_estimate(last_run_at=self.nowfun())
  261. rem = timedelta_seconds(rem_delta)
  262. return due, rem
  263. def __eq__(self, other):
  264. if isinstance(other, crontab):
  265. return (other.day_of_week == self.day_of_week and
  266. other.hour == self.hour and
  267. other.minute == self.minute)
  268. return other is self
  269. def maybe_schedule(s, relative=False):
  270. if isinstance(s, int):
  271. s = timedelta(seconds=s)
  272. if isinstance(s, timedelta):
  273. return schedule(s, relative)
  274. return s