builtins.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 collections import deque
  10. from future_builtins import map, zip
  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. def unlock_chord(group_id, callback, interval=1, propagate=False,
  54. max_retries=None, result=None):
  55. AR = _res.AsyncResult
  56. result = _res.GroupResult(group_id, [AR(r) for r in result])
  57. j = result.join_native if result.supports_native_join else result.join
  58. if result.ready():
  59. subtask(callback).delay(j(propagate=propagate))
  60. else:
  61. unlock_chord.retry(countdown=interval, max_retries=max_retries)
  62. return unlock_chord
  63. @shared_task
  64. def add_map_task(app):
  65. from celery.canvas import subtask
  66. @app.task(name='celery.map')
  67. def xmap(task, it):
  68. task = subtask(task).type
  69. return list(map(task, it))
  70. return xmap
  71. @shared_task
  72. def add_starmap_task(app):
  73. from celery.canvas import subtask
  74. @app.task(name='celery.starmap')
  75. def xstarmap(task, it):
  76. task = subtask(task).type
  77. return list(starmap(task, it))
  78. return xstarmap
  79. @shared_task
  80. def add_chunk_task(app):
  81. from celery.canvas import chunks as _chunks
  82. @app.task(name='celery.chunks')
  83. def chunks(task, it, n):
  84. return _chunks.apply_chunks(task, it, n)
  85. return chunks
  86. @shared_task
  87. def add_group_task(app):
  88. _app = app
  89. from celery.canvas import maybe_subtask, subtask
  90. from celery.result import from_serializable
  91. class Group(app.Task):
  92. app = _app
  93. name = 'celery.group'
  94. accept_magic_kwargs = False
  95. def run(self, tasks, result, group_id, partial_args):
  96. app = self.app
  97. result = from_serializable(result)
  98. # any partial args are added to all tasks in the group
  99. taskit = (subtask(task).clone(partial_args)
  100. for i, task in enumerate(tasks))
  101. if self.request.is_eager or app.conf.CELERY_ALWAYS_EAGER:
  102. return app.GroupResult(result.id,
  103. [task.apply(group_id=group_id) for task in taskit])
  104. with app.producer_or_acquire() as pub:
  105. [task.apply_async(group_id=group_id, publisher=pub,
  106. add_to_parent=False) for task in taskit]
  107. parent = get_current_worker_task()
  108. if parent:
  109. parent.request.children.append(result)
  110. return result
  111. def prepare(self, options, tasks, args, **kwargs):
  112. options['group_id'] = group_id = \
  113. options.setdefault('task_id', uuid())
  114. def prepare_member(task):
  115. task = maybe_subtask(task)
  116. opts = task.options
  117. opts['group_id'] = group_id
  118. try:
  119. tid = opts['task_id']
  120. except KeyError:
  121. tid = opts['task_id'] = uuid()
  122. return task, self.AsyncResult(tid)
  123. tasks, res = list(zip(*[prepare_member(task) for task in tasks]))
  124. return (tasks, self.app.GroupResult(group_id, res), group_id, args)
  125. def apply_async(self, partial_args=(), kwargs={}, **options):
  126. if self.app.conf.CELERY_ALWAYS_EAGER:
  127. return self.apply(partial_args, kwargs, **options)
  128. tasks, result, gid, args = self.prepare(options,
  129. args=partial_args, **kwargs)
  130. super(Group, self).apply_async((list(tasks),
  131. result.serializable(), gid, args), **options)
  132. return result
  133. def apply(self, args=(), kwargs={}, **options):
  134. return super(Group, self).apply(
  135. self.prepare(options, args=args, **kwargs),
  136. **options).get()
  137. return Group
  138. @shared_task
  139. def add_chain_task(app):
  140. from celery.canvas import chord, group, maybe_subtask
  141. _app = app
  142. class Chain(app.Task):
  143. app = _app
  144. name = 'celery.chain'
  145. accept_magic_kwargs = False
  146. def prepare_steps(self, args, tasks):
  147. steps = deque(tasks)
  148. next_step = prev_task = prev_res = None
  149. tasks, results = [], []
  150. i = 0
  151. while steps:
  152. # First task get partial args from chain.
  153. task = maybe_subtask(steps.popleft())
  154. task = task.clone() if i else task.clone(args)
  155. i += 1
  156. tid = task.options.get('task_id')
  157. if tid is None:
  158. tid = task.options['task_id'] = uuid()
  159. res = task.type.AsyncResult(tid)
  160. # automatically upgrade group(..) | s to chord(group, s)
  161. if isinstance(task, group):
  162. try:
  163. next_step = steps.popleft()
  164. except IndexError:
  165. next_step = None
  166. if next_step is not None:
  167. task = chord(task, body=next_step, task_id=tid)
  168. if prev_task:
  169. # link previous task to this task.
  170. prev_task.link(task)
  171. # set the results parent attribute.
  172. res.parent = prev_res
  173. results.append(res)
  174. tasks.append(task)
  175. prev_task, prev_res = task, res
  176. return tasks, results
  177. def apply_async(self, args=(), kwargs={}, group_id=None, chord=None,
  178. task_id=None, **options):
  179. if self.app.conf.CELERY_ALWAYS_EAGER:
  180. return self.apply(args, kwargs, **options)
  181. options.pop('publisher', None)
  182. tasks, results = self.prepare_steps(args, kwargs['tasks'])
  183. result = results[-1]
  184. if group_id:
  185. tasks[-1].set(group_id=group_id)
  186. if chord:
  187. tasks[-1].set(chord=chord)
  188. if task_id:
  189. tasks[-1].set(task_id=task_id)
  190. result = tasks[-1].type.AsyncResult(task_id)
  191. tasks[0].apply_async()
  192. return result
  193. def apply(self, args=(), kwargs={}, **options):
  194. tasks = [maybe_subtask(task).clone() for task in kwargs['tasks']]
  195. res = prev = None
  196. for task in tasks:
  197. res = task.apply((prev.get(), ) if prev else ())
  198. res.parent, prev = prev, res
  199. return res
  200. return Chain
  201. @shared_task
  202. def add_chord_task(app):
  203. """Every chord is executed in a dedicated task, so that the chord
  204. can be used as a subtask, and this generates the task
  205. responsible for that."""
  206. from celery import group
  207. from celery.canvas import maybe_subtask
  208. _app = app
  209. class Chord(app.Task):
  210. app = _app
  211. name = 'celery.chord'
  212. accept_magic_kwargs = False
  213. ignore_result = False
  214. def run(self, header, body, partial_args=(), interval=1,
  215. max_retries=None, propagate=False, eager=False, **kwargs):
  216. group_id = uuid()
  217. AsyncResult = self.app.AsyncResult
  218. prepare_member = self._prepare_member
  219. # - convert back to group if serialized
  220. if not isinstance(header, group):
  221. header = group([maybe_subtask(t) for t in header])
  222. # - eager applies the group inline
  223. if eager:
  224. return header.apply(args=partial_args, task_id=group_id)
  225. results = [AsyncResult(prepare_member(task, body, group_id))
  226. for task in header.tasks]
  227. # - fallback implementations schedules the chord_unlock task here
  228. app.backend.on_chord_apply(group_id, body,
  229. interval=interval,
  230. max_retries=max_retries,
  231. propagate=propagate,
  232. result=results)
  233. # - call the header group, returning the GroupResult.
  234. return header(*partial_args, task_id=group_id)
  235. def _prepare_member(self, task, body, group_id):
  236. opts = task.options
  237. # d.setdefault would work but generating uuid's are expensive
  238. try:
  239. task_id = opts['task_id']
  240. except KeyError:
  241. task_id = opts['task_id'] = uuid()
  242. opts.update(chord=body, group_id=group_id)
  243. return task_id
  244. def apply_async(self, args=(), kwargs={}, task_id=None, **options):
  245. if self.app.conf.CELERY_ALWAYS_EAGER:
  246. return self.apply(args, kwargs, **options)
  247. group_id = options.pop('group_id', None)
  248. chord = options.pop('chord', None)
  249. header, body = (list(maybe_subtask(kwargs['header'])),
  250. maybe_subtask(kwargs['body']))
  251. if group_id:
  252. body.set(group_id=group_id)
  253. if chord:
  254. body.set(chord=chord)
  255. callback_id = body.options.setdefault('task_id', task_id or uuid())
  256. parent = super(Chord, self).apply_async((header, body, args),
  257. **options)
  258. body_result = self.AsyncResult(callback_id)
  259. body_result.parent = parent
  260. return body_result
  261. def apply(self, args=(), kwargs={}, propagate=True, **options):
  262. body = kwargs['body']
  263. res = super(Chord, self).apply(args, dict(kwargs, eager=True),
  264. **options)
  265. return maybe_subtask(body).apply(
  266. args=(res.get(propagate=propagate).get(), ))
  267. return Chord