Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

schedules.py 21KB

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