schedules.py 12 KB

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