amqp.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.backends.amqp
  4. ~~~~~~~~~~~~~~~~~~~~
  5. The AMQP result backend.
  6. This backend publishes results as messages.
  7. """
  8. from __future__ import absolute_import
  9. import socket
  10. import threading
  11. import time
  12. from collections import deque
  13. from operator import itemgetter
  14. from kombu import Exchange, Queue, Producer, Consumer
  15. from celery import states
  16. from celery.exceptions import TimeoutError
  17. from celery.five import range
  18. from celery.utils.log import get_logger
  19. from .base import BaseBackend
  20. logger = get_logger(__name__)
  21. class BacklogLimitExceeded(Exception):
  22. """Too much state history to fast-forward."""
  23. def repair_uuid(s):
  24. # Historically the dashes in UUIDS are removed from AMQ entity names,
  25. # but there is no known reason to. Hopefully we'll be able to fix
  26. # this in v4.0.
  27. return '%s-%s-%s-%s-%s' % (s[:8], s[8:12], s[12:16], s[16:20], s[20:])
  28. class AMQPBackend(BaseBackend):
  29. """Publishes results by sending messages."""
  30. Exchange = Exchange
  31. Queue = Queue
  32. Consumer = Consumer
  33. Producer = Producer
  34. BacklogLimitExceeded = BacklogLimitExceeded
  35. supports_autoexpire = True
  36. supports_native_join = True
  37. retry_policy = {
  38. 'max_retries': 20,
  39. 'interval_start': 0,
  40. 'interval_step': 1,
  41. 'interval_max': 1,
  42. }
  43. def __init__(self, app, connection=None, exchange=None, exchange_type=None,
  44. persistent=None, serializer=None, auto_delete=True,
  45. **kwargs):
  46. super(AMQPBackend, self).__init__(app, **kwargs)
  47. conf = self.app.conf
  48. self._connection = connection
  49. self.queue_arguments = {}
  50. self.persistent = (conf.CELERY_RESULT_PERSISTENT if persistent is None
  51. else persistent)
  52. exchange = exchange or conf.CELERY_RESULT_EXCHANGE
  53. exchange_type = exchange_type or conf.CELERY_RESULT_EXCHANGE_TYPE
  54. self.exchange = self._create_exchange(exchange, exchange_type,
  55. self.persistent)
  56. self.serializer = serializer or conf.CELERY_RESULT_SERIALIZER
  57. self.auto_delete = auto_delete
  58. self.expires = None
  59. if 'expires' not in kwargs or kwargs['expires'] is not None:
  60. self.expires = self.prepare_expires(kwargs.get('expires'))
  61. if self.expires:
  62. self.queue_arguments['x-expires'] = int(self.expires * 1000)
  63. self.mutex = threading.Lock()
  64. def _create_exchange(self, name, type='direct', persistent=True):
  65. delivery_mode = persistent and 'persistent' or 'transient'
  66. return self.Exchange(name=name,
  67. type=type,
  68. delivery_mode=delivery_mode,
  69. durable=self.persistent,
  70. auto_delete=False)
  71. def _create_binding(self, task_id):
  72. name = task_id.replace('-', '')
  73. return self.Queue(name=name,
  74. exchange=self.exchange,
  75. routing_key=name,
  76. durable=self.persistent,
  77. auto_delete=self.auto_delete,
  78. queue_arguments=self.queue_arguments)
  79. def revive(self, channel):
  80. pass
  81. def _routing_key(self, task_id):
  82. return task_id.replace('-', '')
  83. def _store_result(self, task_id, result, status, traceback=None):
  84. """Send task return value and status."""
  85. with self.mutex:
  86. with self.app.amqp.producer_pool.acquire(block=True) as pub:
  87. pub.publish({'task_id': task_id, 'status': status,
  88. 'result': self.encode_result(result, status),
  89. 'traceback': traceback,
  90. 'children': self.current_task_children()},
  91. exchange=self.exchange,
  92. routing_key=self._routing_key(task_id),
  93. serializer=self.serializer,
  94. retry=True, retry_policy=self.retry_policy,
  95. declare=self.on_reply_declare(task_id))
  96. return result
  97. def on_reply_declare(self, task_id):
  98. return [self._create_binding(task_id)]
  99. def wait_for(self, task_id, timeout=None, cache=True, propagate=True,
  100. READY_STATES=states.READY_STATES,
  101. PROPAGATE_STATES=states.PROPAGATE_STATES,
  102. **kwargs):
  103. cached_meta = self._cache.get(task_id)
  104. if cache and cached_meta and \
  105. cached_meta['status'] in READY_STATES:
  106. meta = cached_meta
  107. else:
  108. try:
  109. meta = self.consume(task_id, timeout=timeout)
  110. except socket.timeout:
  111. raise TimeoutError('The operation timed out.')
  112. if meta['status'] in PROPAGATE_STATES and propagate:
  113. raise self.exception_to_python(meta['result'])
  114. # consume() always returns READY_STATE.
  115. return meta['result']
  116. def get_task_meta(self, task_id, backlog_limit=1000):
  117. # Polling and using basic_get
  118. with self.app.pool.acquire_channel(block=True) as (_, channel):
  119. binding = self._create_binding(task_id)(channel)
  120. binding.declare()
  121. prev = latest = acc = None
  122. for i in range(backlog_limit): # spool ffwd
  123. prev, latest, acc = latest, acc, binding.get(no_ack=False)
  124. if not acc: # no more messages
  125. break
  126. if prev:
  127. # backends are not expected to keep history,
  128. # so we delete everything except the most recent state.
  129. prev.ack()
  130. else:
  131. raise self.BacklogLimitExceeded(task_id)
  132. if latest:
  133. payload = self._cache[task_id] = latest.payload
  134. latest.requeue()
  135. return payload
  136. else:
  137. # no new state, use previous
  138. try:
  139. return self._cache[task_id]
  140. except KeyError:
  141. # result probably pending.
  142. return {'status': states.PENDING, 'result': None}
  143. poll = get_task_meta # XXX compat
  144. def drain_events(self, connection, consumer,
  145. timeout=None, now=time.time, wait=None):
  146. wait = wait or connection.drain_events
  147. results = {}
  148. def callback(meta, message):
  149. if meta['status'] in states.READY_STATES:
  150. results[meta['task_id']] = meta
  151. consumer.callbacks[:] = [callback]
  152. time_start = now()
  153. while 1:
  154. # Total time spent may exceed a single call to wait()
  155. if timeout and now() - time_start >= timeout:
  156. raise socket.timeout()
  157. wait(timeout=timeout)
  158. if results: # got event on the wanted channel.
  159. break
  160. self._cache.update(results)
  161. return results
  162. def consume(self, task_id, timeout=None):
  163. wait = self.drain_events
  164. with self.app.pool.acquire_channel(block=True) as (conn, channel):
  165. binding = self._create_binding(task_id)
  166. with self.Consumer(channel, binding, no_ack=True) as consumer:
  167. while 1:
  168. try:
  169. return wait(conn, consumer, timeout)[task_id]
  170. except KeyError:
  171. continue
  172. def _many_bindings(self, ids):
  173. return [self._create_binding(task_id) for task_id in ids]
  174. def get_many(self, task_ids, timeout=None,
  175. now=time.time, getfields=itemgetter('status', 'task_id'),
  176. READY_STATES=states.READY_STATES, **kwargs):
  177. with self.app.pool.acquire_channel(block=True) as (conn, channel):
  178. ids = set(task_ids)
  179. cached_ids = set()
  180. mark_cached = cached_ids.add
  181. for task_id in ids:
  182. try:
  183. cached = self._cache[task_id]
  184. except KeyError:
  185. pass
  186. else:
  187. if cached['status'] in READY_STATES:
  188. yield task_id, cached
  189. mark_cached(task_id)
  190. ids.difference_update(cached_ids)
  191. results = deque()
  192. push_result = results.append
  193. push_cache = self._cache.__setitem__
  194. def on_message(message):
  195. body = message.decode()
  196. state, uid = getfields(body)
  197. if state in READY_STATES:
  198. push_result(body) \
  199. if uid in task_ids else push_cache(uid, body)
  200. bindings = self._many_bindings(task_ids)
  201. with self.Consumer(channel, bindings,
  202. on_message=on_message, no_ack=True):
  203. wait = conn.drain_events
  204. popleft = results.popleft
  205. while ids:
  206. wait(timeout=timeout)
  207. while results:
  208. state = popleft()
  209. task_id = state['task_id']
  210. ids.discard(task_id)
  211. push_cache(task_id, state)
  212. yield task_id, state
  213. def reload_task_result(self, task_id):
  214. raise NotImplementedError(
  215. 'reload_task_result is not supported by this backend.')
  216. def reload_group_result(self, task_id):
  217. """Reload group result, even if it has been previously fetched."""
  218. raise NotImplementedError(
  219. 'reload_group_result is not supported by this backend.')
  220. def save_group(self, group_id, result):
  221. raise NotImplementedError(
  222. 'save_group is not supported by this backend.')
  223. def restore_group(self, group_id, cache=True):
  224. raise NotImplementedError(
  225. 'restore_group is not supported by this backend.')
  226. def delete_group(self, group_id):
  227. raise NotImplementedError(
  228. 'delete_group is not supported by this backend.')
  229. def __reduce__(self, args=(), kwargs={}):
  230. kwargs.update(
  231. connection=self._connection,
  232. exchange=self.exchange.name,
  233. exchange_type=self.exchange.type,
  234. persistent=self.persistent,
  235. serializer=self.serializer,
  236. auto_delete=self.auto_delete,
  237. expires=self.expires,
  238. )
  239. return super(AMQPBackend, self).__reduce__(args, kwargs)