builtins.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.app.builtins
  4. ~~~~~~~~~~~~~~~~~~~
  5. Built-in tasks that are always available in all
  6. app instances. E.g. chord, group and xmap.
  7. """
  8. from __future__ import absolute_import
  9. from __future__ import with_statement
  10. from collections import deque
  11. from itertools import starmap
  12. from celery._state import get_current_worker_task
  13. from celery.utils import uuid
  14. #: global list of functions defining tasks that should be
  15. #: added to all apps.
  16. _shared_tasks = []
  17. def shared_task(constructor):
  18. """Decorator that specifies that the decorated function is a function
  19. that generates a built-in task.
  20. The function will then be called for every new app instance created
  21. (lazily, so more exactly when the task registry for that app is needed).
  22. """
  23. _shared_tasks.append(constructor)
  24. return constructor
  25. def load_shared_tasks(app):
  26. """Loads the built-in tasks for an app instance."""
  27. for constructor in _shared_tasks:
  28. constructor(app)
  29. @shared_task
  30. def add_backend_cleanup_task(app):
  31. """The backend cleanup task can be used to clean up the default result
  32. backend.
  33. This task is also added do the periodic task schedule so that it is
  34. run every day at midnight, but :program:`celerybeat` must be running
  35. for this to be effective.
  36. Note that not all backends do anything for this, what needs to be
  37. done at cleanup is up to each backend, and some backends
  38. may even clean up in realtime so that a periodic cleanup is not necessary.
  39. """
  40. @app.task(name='celery.backend_cleanup')
  41. def backend_cleanup():
  42. app.backend.cleanup()
  43. return backend_cleanup
  44. @shared_task
  45. def add_unlock_chord_task(app):
  46. """The unlock chord task is used by result backends that doesn't
  47. have native chord support.
  48. It creates a task chain polling the header for completion.
  49. """
  50. from celery.canvas import subtask
  51. from celery import result as _res
  52. @app.task(name='celery.chord_unlock', max_retries=None,
  53. default_retry_delay=1, ignore_result=True)
  54. def unlock_chord(group_id, callback, interval=None, propagate=False,
  55. max_retries=None, result=None):
  56. if interval is None:
  57. interval = unlock_chord.default_retry_delay
  58. result = _res.GroupResult(group_id, map(_res.AsyncResult, result))
  59. j = result.join_native if result.supports_native_join else result.join
  60. if result.ready():
  61. subtask(callback).delay(j(propagate=propagate))
  62. else:
  63. unlock_chord.retry(countdown=interval, max_retries=max_retries)
  64. return unlock_chord
  65. @shared_task
  66. def add_map_task(app):
  67. from celery.canvas import subtask
  68. @app.task(name='celery.map')
  69. def xmap(task, it):
  70. task = subtask(task).type
  71. return list(map(task, it))
  72. return xmap
  73. @shared_task
  74. def add_starmap_task(app):
  75. from celery.canvas import subtask
  76. @app.task(name='celery.starmap')
  77. def xstarmap(task, it):
  78. task = subtask(task).type
  79. return list(starmap(task, it))
  80. return xstarmap
  81. @shared_task
  82. def add_chunk_task(app):
  83. from celery.canvas import chunks as _chunks
  84. @app.task(name='celery.chunks')
  85. def chunks(task, it, n):
  86. return _chunks.apply_chunks(task, it, n)
  87. return chunks
  88. @shared_task
  89. def add_group_task(app):
  90. _app = app
  91. from celery.canvas import maybe_subtask, subtask
  92. from celery.result import from_serializable
  93. class Group(app.Task):
  94. app = _app
  95. name = 'celery.group'
  96. accept_magic_kwargs = False
  97. def run(self, tasks, result, group_id, partial_args):
  98. app = self.app
  99. result = from_serializable(result)
  100. # any partial args are added to all tasks in the group
  101. taskit = (subtask(task).clone(partial_args)
  102. for i, task in enumerate(tasks))
  103. if self.request.is_eager or app.conf.CELERY_ALWAYS_EAGER:
  104. return app.GroupResult(result.id,
  105. [task.apply(group_id=group_id) for task in taskit])
  106. with app.producer_or_acquire() as pub:
  107. [task.apply_async(group_id=group_id, publisher=pub,
  108. add_to_parent=False) for task in taskit]
  109. parent = get_current_worker_task()
  110. if parent:
  111. parent.request.children.append(result)
  112. return result
  113. def prepare(self, options, tasks, args, **kwargs):
  114. AsyncResult = self.AsyncResult
  115. options['group_id'] = group_id = \
  116. options.setdefault('task_id', uuid())
  117. def prepare_member(task):
  118. task = maybe_subtask(task)
  119. opts = task.options
  120. opts['group_id'] = group_id
  121. try:
  122. tid = opts['task_id']
  123. except KeyError:
  124. tid = opts['task_id'] = uuid()
  125. return task, AsyncResult(tid)
  126. try:
  127. tasks, results = zip(*[prepare_member(task) for task in tasks])
  128. except ValueError: # tasks empty
  129. tasks, results = [], []
  130. return (tasks, self.app.GroupResult(group_id, results),
  131. group_id, args)
  132. def apply_async(self, partial_args=(), kwargs={}, **options):
  133. if self.app.conf.CELERY_ALWAYS_EAGER:
  134. return self.apply(partial_args, kwargs, **options)
  135. tasks, result, gid, args = self.prepare(options,
  136. args=partial_args, **kwargs)
  137. super(Group, self).apply_async((list(tasks),
  138. result.serializable(), gid, args), **options)
  139. return result
  140. def apply(self, args=(), kwargs={}, **options):
  141. return super(Group, self).apply(
  142. self.prepare(options, args=args, **kwargs),
  143. **options).get()
  144. return Group
  145. @shared_task
  146. def add_chain_task(app):
  147. from celery.canvas import chord, group, maybe_subtask
  148. _app = app
  149. class Chain(app.Task):
  150. app = _app
  151. name = 'celery.chain'
  152. accept_magic_kwargs = False
  153. def prepare_steps(self, args, tasks):
  154. steps = deque(tasks)
  155. next_step = prev_task = prev_res = None
  156. tasks, results = [], []
  157. i = 0
  158. while steps:
  159. # First task get partial args from chain.
  160. task = maybe_subtask(steps.popleft())
  161. task = task.clone() if i else task.clone(args)
  162. i += 1
  163. tid = task.options.get('task_id')
  164. if tid is None:
  165. tid = task.options['task_id'] = uuid()
  166. res = task.type.AsyncResult(tid)
  167. # automatically upgrade group(..) | s to chord(group, s)
  168. if isinstance(task, group):
  169. try:
  170. next_step = steps.popleft()
  171. except IndexError:
  172. next_step = None
  173. if next_step is not None:
  174. task = chord(task, body=next_step, task_id=tid)
  175. if prev_task:
  176. # link previous task to this task.
  177. prev_task.link(task)
  178. # set the results parent attribute.
  179. res.parent = prev_res
  180. results.append(res)
  181. tasks.append(task)
  182. prev_task, prev_res = task, res
  183. return tasks, results
  184. def apply_async(self, args=(), kwargs={}, group_id=None, chord=None,
  185. task_id=None, **options):
  186. if self.app.conf.CELERY_ALWAYS_EAGER:
  187. return self.apply(args, kwargs, **options)
  188. options.pop('publisher', None)
  189. tasks, results = self.prepare_steps(args, kwargs['tasks'])
  190. result = results[-1]
  191. if group_id:
  192. tasks[-1].set(group_id=group_id)
  193. if chord:
  194. tasks[-1].set(chord=chord)
  195. if task_id:
  196. tasks[-1].set(task_id=task_id)
  197. result = tasks[-1].type.AsyncResult(task_id)
  198. tasks[0].apply_async()
  199. return result
  200. def apply(self, args=(), kwargs={}, subtask=maybe_subtask, **options):
  201. last, fargs = None, args # fargs passed to first task only
  202. for task in kwargs['tasks']:
  203. res = subtask(task).clone(fargs).apply(last and (last.get(), ))
  204. res.parent, last, fargs = last, res, None
  205. return last
  206. return Chain
  207. @shared_task
  208. def add_chord_task(app):
  209. """Every chord is executed in a dedicated task, so that the chord
  210. can be used as a subtask, and this generates the task
  211. responsible for that."""
  212. from celery import group
  213. from celery.canvas import maybe_subtask
  214. _app = app
  215. class Chord(app.Task):
  216. app = _app
  217. name = 'celery.chord'
  218. accept_magic_kwargs = False
  219. ignore_result = False
  220. def run(self, header, body, partial_args=(), interval=1,
  221. max_retries=None, propagate=False, eager=False, **kwargs):
  222. group_id = uuid()
  223. AsyncResult = self.app.AsyncResult
  224. prepare_member = self._prepare_member
  225. # - convert back to group if serialized
  226. if not isinstance(header, group):
  227. header = group(map(maybe_subtask, header))
  228. # - eager applies the group inline
  229. if eager:
  230. return header.apply(args=partial_args, task_id=group_id)
  231. results = [AsyncResult(prepare_member(task, body, group_id))
  232. for task in header.tasks]
  233. # - fallback implementations schedules the chord_unlock task here
  234. app.backend.on_chord_apply(group_id, body,
  235. interval=interval,
  236. max_retries=max_retries,
  237. propagate=propagate,
  238. result=results)
  239. # - call the header group, returning the GroupResult.
  240. # XXX Python 2.5 doesn't allow kwargs after star-args.
  241. return header(*partial_args, **{'task_id': group_id})
  242. def _prepare_member(self, task, body, group_id):
  243. opts = task.options
  244. # d.setdefault would work but generating uuid's are expensive
  245. try:
  246. task_id = opts['task_id']
  247. except KeyError:
  248. task_id = opts['task_id'] = uuid()
  249. opts.update(chord=body, group_id=group_id)
  250. return task_id
  251. def apply_async(self, args=(), kwargs={}, task_id=None, **options):
  252. if self.app.conf.CELERY_ALWAYS_EAGER:
  253. return self.apply(args, kwargs, **options)
  254. group_id = options.pop('group_id', None)
  255. chord = options.pop('chord', None)
  256. header = kwargs.pop('header')
  257. body = kwargs.pop('body')
  258. header, body = (list(maybe_subtask(header)),
  259. maybe_subtask(body))
  260. if group_id:
  261. body.set(group_id=group_id)
  262. if chord:
  263. body.set(chord=chord)
  264. callback_id = body.options.setdefault('task_id', task_id or uuid())
  265. parent = super(Chord, self).apply_async((header, body, args),
  266. kwargs, **options)
  267. body_result = self.AsyncResult(callback_id)
  268. body_result.parent = parent
  269. return body_result
  270. def apply(self, args=(), kwargs={}, propagate=True, **options):
  271. body = kwargs['body']
  272. res = super(Chord, self).apply(args, dict(kwargs, eager=True),
  273. **options)
  274. return maybe_subtask(body).apply(
  275. args=(res.get(propagate=propagate).get(), ))
  276. return Chord