beat.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.beat
  4. ~~~~~~~~~~~
  5. The periodic task scheduler.
  6. """
  7. from __future__ import absolute_import
  8. import errno
  9. import os
  10. import time
  11. import shelve
  12. import sys
  13. import traceback
  14. from threading import Event, Thread
  15. from billiard import Process, ensure_multiprocessing
  16. from kombu.utils import reprcall
  17. from kombu.utils.functional import maybe_promise
  18. from . import __version__
  19. from . import platforms
  20. from . import signals
  21. from . import current_app
  22. from .app import app_or_default
  23. from .schedules import maybe_schedule, crontab
  24. from .utils import cached_property
  25. from .utils.imports import instantiate
  26. from .utils.timeutils import humanize_seconds
  27. from .utils.log import get_logger
  28. logger = get_logger(__name__)
  29. debug, info, error = logger.debug, logger.info, logger.error
  30. DEFAULT_MAX_INTERVAL = 300 # 5 minutes
  31. class SchedulingError(Exception):
  32. """An error occured while scheduling a task."""
  33. class ScheduleEntry(object):
  34. """An entry in the scheduler.
  35. :keyword name: see :attr:`name`.
  36. :keyword schedule: see :attr:`schedule`.
  37. :keyword args: see :attr:`args`.
  38. :keyword kwargs: see :attr:`kwargs`.
  39. :keyword options: see :attr:`options`.
  40. :keyword last_run_at: see :attr:`last_run_at`.
  41. :keyword total_run_count: see :attr:`total_run_count`.
  42. :keyword relative: Is the time relative to when the server starts?
  43. """
  44. #: The task name
  45. name = None
  46. #: The schedule (run_every/crontab)
  47. schedule = None
  48. #: Positional arguments to apply.
  49. args = None
  50. #: Keyword arguments to apply.
  51. kwargs = None
  52. #: Task execution options.
  53. options = None
  54. #: The time and date of when this task was last scheduled.
  55. last_run_at = None
  56. #: Total number of times this task has been scheduled.
  57. total_run_count = 0
  58. def __init__(self, name=None, task=None, last_run_at=None,
  59. total_run_count=None, schedule=None, args=(), kwargs={},
  60. options={}, relative=False):
  61. self.name = name
  62. self.task = task
  63. self.args = args
  64. self.kwargs = kwargs
  65. self.options = options
  66. self.schedule = maybe_schedule(schedule, relative)
  67. self.last_run_at = last_run_at or self._default_now()
  68. self.total_run_count = total_run_count or 0
  69. def _default_now(self):
  70. return current_app.now()
  71. def _next_instance(self, last_run_at=None):
  72. """Returns a new instance of the same class, but with
  73. its date and count fields updated."""
  74. return self.__class__(**dict(self,
  75. last_run_at=last_run_at or self._default_now(),
  76. total_run_count=self.total_run_count + 1))
  77. __next__ = next = _next_instance # for 2to3
  78. def update(self, other):
  79. """Update values from another entry.
  80. Does only update "editable" fields (task, schedule, args, kwargs,
  81. options).
  82. """
  83. self.__dict__.update({'task': other.task, 'schedule': other.schedule,
  84. 'args': other.args, 'kwargs': other.kwargs,
  85. 'options': other.options})
  86. def is_due(self):
  87. """See :meth:`~celery.schedule.schedule.is_due`."""
  88. return self.schedule.is_due(self.last_run_at)
  89. def __iter__(self):
  90. return vars(self).iteritems()
  91. def __repr__(self):
  92. return '<Entry: {0.name} {call} {0.schedule}'.format(self,
  93. call=reprcall(self.task, self.args or (), self.kwargs or {}))
  94. class Scheduler(object):
  95. """Scheduler for periodic tasks.
  96. :keyword schedule: see :attr:`schedule`.
  97. :keyword max_interval: see :attr:`max_interval`.
  98. """
  99. Entry = ScheduleEntry
  100. #: The schedule dict/shelve.
  101. schedule = None
  102. #: Maximum time to sleep between re-checking the schedule.
  103. max_interval = DEFAULT_MAX_INTERVAL
  104. #: How often to sync the schedule (3 minutes by default)
  105. sync_every = 3 * 60
  106. _last_sync = None
  107. logger = logger # compat
  108. def __init__(self, schedule=None, max_interval=None,
  109. app=None, Publisher=None, lazy=False, **kwargs):
  110. app = self.app = app_or_default(app)
  111. self.data = maybe_promise({} if schedule is None else schedule)
  112. self.max_interval = (max_interval
  113. or app.conf.CELERYBEAT_MAX_LOOP_INTERVAL
  114. or self.max_interval)
  115. self.Publisher = Publisher or app.amqp.TaskProducer
  116. if not lazy:
  117. self.setup_schedule()
  118. def install_default_entries(self, data):
  119. entries = {}
  120. if self.app.conf.CELERY_TASK_RESULT_EXPIRES:
  121. if 'celery.backend_cleanup' not in data:
  122. entries['celery.backend_cleanup'] = {
  123. 'task': 'celery.backend_cleanup',
  124. 'schedule': crontab('0', '4', '*'),
  125. 'options': {'expires': 12 * 3600}}
  126. self.update_from_dict(entries)
  127. def maybe_due(self, entry, publisher=None):
  128. is_due, next_time_to_run = entry.is_due()
  129. if is_due:
  130. info('Scheduler: Sending due task %s', entry.task)
  131. try:
  132. result = self.apply_async(entry, publisher=publisher)
  133. except Exception as exc:
  134. error('Message Error: %s\n%s',
  135. exc, traceback.format_stack(), exc_info=True)
  136. else:
  137. debug('%s sent. id->%s', entry.task, result.id)
  138. return next_time_to_run
  139. def tick(self):
  140. """Run a tick, that is one iteration of the scheduler.
  141. Executes all due tasks.
  142. """
  143. remaining_times = []
  144. try:
  145. for entry in self.schedule.itervalues():
  146. next_time_to_run = self.maybe_due(entry, self.publisher)
  147. if next_time_to_run:
  148. remaining_times.append(next_time_to_run)
  149. except RuntimeError:
  150. pass
  151. return min(remaining_times + [self.max_interval])
  152. def should_sync(self):
  153. return (not self._last_sync or
  154. (time.time() - self._last_sync) > self.sync_every)
  155. def reserve(self, entry):
  156. new_entry = self.schedule[entry.name] = next(entry)
  157. return new_entry
  158. def apply_async(self, entry, publisher=None, **kwargs):
  159. # Update timestamps and run counts before we actually execute,
  160. # so we have that done if an exception is raised (doesn't schedule
  161. # forever.)
  162. entry = self.reserve(entry)
  163. task = self.app.tasks.get(entry.task)
  164. try:
  165. if task:
  166. result = task.apply_async(entry.args, entry.kwargs,
  167. publisher=publisher,
  168. **entry.options)
  169. else:
  170. result = self.send_task(entry.task, entry.args, entry.kwargs,
  171. publisher=publisher,
  172. **entry.options)
  173. except Exception as exc:
  174. raise SchedulingError, SchedulingError(
  175. "Couldn't apply scheduled task {0.name}: {exc}".format(
  176. entry, exc)), sys.exc_info()[2]
  177. finally:
  178. if self.should_sync():
  179. self._do_sync()
  180. return result
  181. def send_task(self, *args, **kwargs):
  182. return self.app.send_task(*args, **kwargs)
  183. def setup_schedule(self):
  184. self.install_default_entries(self.data)
  185. def _do_sync(self):
  186. try:
  187. debug('Celerybeat: Synchronizing schedule...')
  188. self.sync()
  189. finally:
  190. self._last_sync = time.time()
  191. def sync(self):
  192. pass
  193. def close(self):
  194. self.sync()
  195. def add(self, **kwargs):
  196. entry = self.Entry(**kwargs)
  197. self.schedule[entry.name] = entry
  198. return entry
  199. def _maybe_entry(self, name, entry):
  200. if isinstance(entry, self.Entry):
  201. return entry
  202. return self.Entry(**dict(entry, name=name))
  203. def update_from_dict(self, dict_):
  204. self.schedule.update(dict((name, self._maybe_entry(name, entry))
  205. for name, entry in dict_.items()))
  206. def merge_inplace(self, b):
  207. schedule = self.schedule
  208. A, B = set(schedule), set(b)
  209. # Remove items from disk not in the schedule anymore.
  210. for key in A ^ B:
  211. schedule.pop(key, None)
  212. # Update and add new items in the schedule
  213. for key in B:
  214. entry = self.Entry(**dict(b[key], name=key))
  215. if schedule.get(key):
  216. schedule[key].update(entry)
  217. else:
  218. schedule[key] = entry
  219. def _ensure_connected(self):
  220. # callback called for each retry while the connection
  221. # can't be established.
  222. def _error_handler(exc, interval):
  223. error('Celerybeat: Connection error: %s. '
  224. 'Trying again in %s seconds...', exc, interval)
  225. return self.connection.ensure_connection(_error_handler,
  226. self.app.conf.BROKER_CONNECTION_MAX_RETRIES)
  227. def get_schedule(self):
  228. return self.data
  229. def set_schedule(self, schedule):
  230. self.data = schedule
  231. schedule = property(get_schedule, set_schedule)
  232. @cached_property
  233. def connection(self):
  234. return self.app.connection()
  235. @cached_property
  236. def publisher(self):
  237. return self.Publisher(self._ensure_connected())
  238. @property
  239. def info(self):
  240. return ''
  241. class PersistentScheduler(Scheduler):
  242. persistence = shelve
  243. known_suffixes = ('', '.db', '.dat', '.bak', '.dir')
  244. _store = None
  245. def __init__(self, *args, **kwargs):
  246. self.schedule_filename = kwargs.get('schedule_filename')
  247. Scheduler.__init__(self, *args, **kwargs)
  248. def _remove_db(self):
  249. for suffix in self.known_suffixes:
  250. try:
  251. os.remove(self.schedule_filename + suffix)
  252. except OSError as exc:
  253. if exc.errno != errno.ENOENT:
  254. raise
  255. def setup_schedule(self):
  256. try:
  257. self._store = self.persistence.open(self.schedule_filename,
  258. writeback=True)
  259. entries = self._store.setdefault('entries', {})
  260. except Exception as exc:
  261. error('Removing corrupted schedule file %r: %r',
  262. self.schedule_filename, exc, exc_info=True)
  263. self._remove_db()
  264. self._store = self.persistence.open(self.schedule_filename,
  265. writeback=True)
  266. else:
  267. if '__version__' not in self._store:
  268. self._store.clear() # remove schedule at 2.2.2 upgrade.
  269. if 'utc' not in self._store:
  270. self._store.clear() # remove schedule at 3.0.1 upgrade.
  271. entries = self._store.setdefault('entries', {})
  272. self.merge_inplace(self.app.conf.CELERYBEAT_SCHEDULE)
  273. self.install_default_entries(self.schedule)
  274. self._store.update(__version__=__version__, utc=True)
  275. self.sync()
  276. debug('Current schedule:\n' + '\n'.join(repr(entry)
  277. for entry in entries.itervalues()))
  278. def get_schedule(self):
  279. return self._store['entries']
  280. def set_schedule(self, schedule):
  281. self._store['entries'] = schedule
  282. schedule = property(get_schedule, set_schedule)
  283. def sync(self):
  284. if self._store is not None:
  285. self._store.sync()
  286. def close(self):
  287. self.sync()
  288. self._store.close()
  289. @property
  290. def info(self):
  291. return ' . db -> {self.schedule_filename}'.format(self=self)
  292. class Service(object):
  293. scheduler_cls = PersistentScheduler
  294. def __init__(self, max_interval=None, schedule_filename=None,
  295. scheduler_cls=None, app=None):
  296. app = self.app = app_or_default(app)
  297. self.max_interval = (max_interval
  298. or app.conf.CELERYBEAT_MAX_LOOP_INTERVAL)
  299. self.scheduler_cls = scheduler_cls or self.scheduler_cls
  300. self.schedule_filename = schedule_filename or \
  301. app.conf.CELERYBEAT_SCHEDULE_FILENAME
  302. self._is_shutdown = Event()
  303. self._is_stopped = Event()
  304. def start(self, embedded_process=False):
  305. info('Celerybeat: Starting...')
  306. debug('Celerybeat: Ticking with max interval->%s',
  307. humanize_seconds(self.scheduler.max_interval))
  308. signals.beat_init.send(sender=self)
  309. if embedded_process:
  310. signals.beat_embedded_init.send(sender=self)
  311. platforms.set_process_title('celerybeat')
  312. try:
  313. while not self._is_shutdown.is_set():
  314. interval = self.scheduler.tick()
  315. debug('Celerybeat: Waking up %s.',
  316. humanize_seconds(interval, prefix='in '))
  317. time.sleep(interval)
  318. except (KeyboardInterrupt, SystemExit):
  319. self._is_shutdown.set()
  320. finally:
  321. self.sync()
  322. def sync(self):
  323. self.scheduler.close()
  324. self._is_stopped.set()
  325. def stop(self, wait=False):
  326. info('Celerybeat: Shutting down...')
  327. self._is_shutdown.set()
  328. wait and self._is_stopped.wait() # block until shutdown done.
  329. def get_scheduler(self, lazy=False):
  330. filename = self.schedule_filename
  331. scheduler = instantiate(self.scheduler_cls,
  332. app=self.app,
  333. schedule_filename=filename,
  334. max_interval=self.max_interval,
  335. lazy=lazy)
  336. return scheduler
  337. @cached_property
  338. def scheduler(self):
  339. return self.get_scheduler()
  340. class _Threaded(Thread):
  341. """Embedded task scheduler using threading."""
  342. def __init__(self, *args, **kwargs):
  343. super(_Threaded, self).__init__()
  344. self.service = Service(*args, **kwargs)
  345. self.daemon = True
  346. self.name = 'Beat'
  347. def run(self):
  348. self.service.start()
  349. def stop(self):
  350. self.service.stop(wait=True)
  351. try:
  352. ensure_multiprocessing()
  353. except NotImplementedError: # pragma: no cover
  354. _Process = None
  355. else:
  356. class _Process(Process): # noqa
  357. def __init__(self, *args, **kwargs):
  358. super(_Process, self).__init__()
  359. self.service = Service(*args, **kwargs)
  360. self.name = 'Beat'
  361. def run(self):
  362. platforms.signals.reset('SIGTERM')
  363. self.service.start(embedded_process=True)
  364. def stop(self):
  365. self.service.stop()
  366. self.terminate()
  367. def EmbeddedService(*args, **kwargs):
  368. """Return embedded clock service.
  369. :keyword thread: Run threaded instead of as a separate process.
  370. Uses :mod:`multiprocessing` by default, if available.
  371. """
  372. if kwargs.pop('thread', False) or _Process is None:
  373. # Need short max interval to be able to stop thread
  374. # in reasonable time.
  375. kwargs.setdefault('max_interval', 1)
  376. return _Threaded(*args, **kwargs)
  377. return _Process(*args, **kwargs)