builtins.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 itertools import imap, izip, starmap
  11. from celery._state import get_current_worker_task
  12. from celery.utils import uuid
  13. #: global list of functions defining tasks that should be
  14. #: added to all apps.
  15. _shared_tasks = []
  16. def shared_task(constructor):
  17. """Decorator that specifies that the decorated function is a function
  18. that generates a built-in task.
  19. The function will then be called for every new app instance created
  20. (lazily, so more exactly when the task registry for that app is needed).
  21. """
  22. _shared_tasks.append(constructor)
  23. return constructor
  24. def load_shared_tasks(app):
  25. """Loads the built-in tasks for an app instance."""
  26. for constructor in _shared_tasks:
  27. constructor(app)
  28. @shared_task
  29. def add_backend_cleanup_task(app):
  30. """The backend cleanup task can be used to clean up the default result
  31. backend.
  32. This task is also added do the periodic task schedule so that it is
  33. run every day at midnight, but :program:`celerybeat` must be running
  34. for this to be effective.
  35. Note that not all backends do anything for this, what needs to be
  36. done at cleanup is up to each backend, and some backends
  37. may even clean up in realtime so that a periodic cleanup is not necessary.
  38. """
  39. @app.task(name='celery.backend_cleanup')
  40. def backend_cleanup():
  41. app.backend.cleanup()
  42. return backend_cleanup
  43. @shared_task
  44. def add_unlock_chord_task(app):
  45. """The unlock chord task is used by result backends that doesn't
  46. have native chord support.
  47. It creates a task chain polling the header for completion.
  48. """
  49. from celery.canvas import subtask
  50. from celery import result as _res
  51. @app.task(name='celery.chord_unlock', max_retries=None,
  52. default_retry_delay=1, ignore_result=True)
  53. def unlock_chord(group_id, callback, interval=None, propagate=False,
  54. max_retries=None, result=None, Result=_res.AsyncResult):
  55. if interval is None:
  56. interval = unlock_chord.default_retry_delay
  57. result = _res.GroupResult(group_id, [Result(r) for r in result])
  58. j = result.join_native if result.supports_native_join else result.join
  59. if result.ready():
  60. subtask(callback).delay(j(propagate=propagate))
  61. else:
  62. unlock_chord.retry(countdown=interval, max_retries=max_retries)
  63. return unlock_chord
  64. @shared_task
  65. def add_map_task(app):
  66. from celery.canvas import subtask
  67. @app.task(name='celery.map')
  68. def xmap(task, it):
  69. task = subtask(task).type
  70. return list(imap(task, it))
  71. return xmap
  72. @shared_task
  73. def add_starmap_task(app):
  74. from celery.canvas import subtask
  75. @app.task(name='celery.starmap')
  76. def xstarmap(task, it):
  77. task = subtask(task).type
  78. return list(starmap(task, it))
  79. return xstarmap
  80. @shared_task
  81. def add_chunk_task(app):
  82. from celery.canvas import chunks as _chunks
  83. @app.task(name='celery.chunks')
  84. def chunks(task, it, n):
  85. return _chunks.apply_chunks(task, it, n)
  86. return chunks
  87. @shared_task
  88. def add_group_task(app):
  89. _app = app
  90. from celery.canvas import maybe_subtask, subtask
  91. from celery.result import from_serializable
  92. class Group(app.Task):
  93. app = _app
  94. name = 'celery.group'
  95. accept_magic_kwargs = False
  96. def run(self, tasks, result, group_id, partial_args):
  97. app = self.app
  98. result = from_serializable(result)
  99. # any partial args are added to all tasks in the group
  100. taskit = (subtask(task).clone(partial_args)
  101. for i, task in enumerate(tasks))
  102. if self.request.is_eager or app.conf.CELERY_ALWAYS_EAGER:
  103. return app.GroupResult(result.id,
  104. [task.apply(group_id=group_id) for task in taskit])
  105. with app.producer_or_acquire() as pub:
  106. [task.apply_async(group_id=group_id, publisher=pub,
  107. add_to_parent=False) for task in taskit]
  108. parent = get_current_worker_task()
  109. if parent:
  110. parent.request.children.append(result)
  111. return result
  112. def prepare(self, options, tasks, args, **kwargs):
  113. AsyncResult = self.AsyncResult
  114. options['group_id'] = group_id = \
  115. options.setdefault('task_id', uuid())
  116. def prepare_member(task):
  117. task = maybe_subtask(task)
  118. opts = task.options
  119. opts['group_id'] = group_id
  120. try:
  121. tid = opts['task_id']
  122. except KeyError:
  123. tid = opts['task_id'] = uuid()
  124. return task, AsyncResult(tid)
  125. try:
  126. tasks, res = list(izip(*[prepare_member(task)
  127. for task in tasks]))
  128. except ValueError: # tasks empty
  129. tasks, res = [], []
  130. return (tasks, self.app.GroupResult(group_id, res), group_id, args)
  131. def apply_async(self, partial_args=(), kwargs={}, **options):
  132. if self.app.conf.CELERY_ALWAYS_EAGER:
  133. return self.apply(partial_args, kwargs, **options)
  134. tasks, result, gid, args = self.prepare(options,
  135. args=partial_args, **kwargs)
  136. super(Group, self).apply_async((list(tasks),
  137. result.serializable(), gid, args), **options)
  138. return result
  139. def apply(self, args=(), kwargs={}, **options):
  140. return super(Group, self).apply(
  141. self.prepare(options, args=args, **kwargs),
  142. **options).get()
  143. return Group
  144. @shared_task
  145. def add_chain_task(app):
  146. from celery.canvas import chord, group, maybe_subtask
  147. _app = app
  148. class Chain(app.Task):
  149. app = _app
  150. name = 'celery.chain'
  151. accept_magic_kwargs = False
  152. def prepare_steps(self, args, tasks):
  153. steps = deque(tasks)
  154. next_step = prev_task = prev_res = None
  155. tasks, results = [], []
  156. i = 0
  157. while steps:
  158. # First task get partial args from chain.
  159. task = maybe_subtask(steps.popleft())
  160. task = task.clone() if i else task.clone(args)
  161. i += 1
  162. tid = task.options.get('task_id')
  163. if tid is None:
  164. tid = task.options['task_id'] = uuid()
  165. res = task.type.AsyncResult(tid)
  166. # automatically upgrade group(..) | s to chord(group, s)
  167. if isinstance(task, group):
  168. try:
  169. next_step = steps.popleft()
  170. except IndexError:
  171. next_step = None
  172. if next_step is not None:
  173. task = chord(task, body=next_step, task_id=tid)
  174. if prev_task:
  175. # link previous task to this task.
  176. prev_task.link(task)
  177. # set the results parent attribute.
  178. res.parent = prev_res
  179. if not isinstance(prev_task, chord):
  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. tasks = header.tasks if isinstance(header, group) else header
  227. header = group([maybe_subtask(s).clone() for s in tasks])
  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. return header(*partial_args, task_id=group_id)
  241. def _prepare_member(self, task, body, group_id):
  242. opts = task.options
  243. # d.setdefault would work but generating uuid's are expensive
  244. try:
  245. task_id = opts['task_id']
  246. except KeyError:
  247. task_id = opts['task_id'] = uuid()
  248. opts.update(chord=body, group_id=group_id)
  249. return task_id
  250. def apply_async(self, args=(), kwargs={}, task_id=None, **options):
  251. if self.app.conf.CELERY_ALWAYS_EAGER:
  252. return self.apply(args, kwargs, **options)
  253. group_id = options.pop('group_id', None)
  254. chord = options.pop('chord', None)
  255. header = kwargs.pop('header')
  256. body = kwargs.pop('body')
  257. header, body = (list(maybe_subtask(header)),
  258. maybe_subtask(body))
  259. if group_id:
  260. body.set(group_id=group_id)
  261. if chord:
  262. body.set(chord=chord)
  263. callback_id = body.options.setdefault('task_id', task_id or uuid())
  264. parent = super(Chord, self).apply_async((header, body, args),
  265. kwargs, **options)
  266. body_result = self.AsyncResult(callback_id)
  267. body_result.parent = parent
  268. return body_result
  269. def apply(self, args=(), kwargs={}, propagate=True, **options):
  270. body = kwargs['body']
  271. res = super(Chord, self).apply(args, dict(kwargs, eager=True),
  272. **options)
  273. return maybe_subtask(body).apply(
  274. args=(res.get(propagate=propagate).get(), ))
  275. return Chord