rpc.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # -*- coding: utf-8 -*-
  2. """The ``RPC`` result backend for AMQP brokers.
  3. RPC-style result backend, using reply-to and one queue per client.
  4. """
  5. import kombu
  6. import time
  7. from kombu.common import maybe_declare
  8. from kombu.utils.compat import register_after_fork
  9. from kombu.utils.objects import cached_property
  10. from celery import states
  11. from celery._state import current_task, task_join_will_block
  12. from . import base
  13. from .async import AsyncBackendMixin, BaseResultConsumer
  14. __all__ = ['BacklogLimitExceeded', 'RPCBackend']
  15. E_NO_CHORD_SUPPORT = """
  16. The "rpc" result backend does not support chords!
  17. Note that a group chained with a task is also upgraded to be a chord,
  18. as this pattern requires synchronization.
  19. Result backends that supports chords: Redis, Database, Memcached, and more.
  20. """
  21. class BacklogLimitExceeded(Exception):
  22. """Too much state history to fast-forward."""
  23. def _on_after_fork_cleanup_backend(backend):
  24. backend._after_fork()
  25. class ResultConsumer(BaseResultConsumer):
  26. Consumer = kombu.Consumer
  27. _connection = None
  28. _consumer = None
  29. def __init__(self, *args, **kwargs):
  30. super().__init__(*args, **kwargs)
  31. self._create_binding = self.backend._create_binding
  32. def start(self, initial_task_id, no_ack=True, **kwargs):
  33. self._connection = self.app.connection()
  34. initial_queue = self._create_binding(initial_task_id)
  35. self._consumer = self.Consumer(
  36. self._connection.default_channel, [initial_queue],
  37. callbacks=[self.on_state_change], no_ack=no_ack,
  38. accept=self.accept)
  39. self._consumer.consume()
  40. def drain_events(self, timeout=None):
  41. if self._connection:
  42. return self._connection.drain_events(timeout=timeout)
  43. elif timeout:
  44. time.sleep(timeout)
  45. def stop(self):
  46. try:
  47. self._consumer.cancel()
  48. finally:
  49. self._connection.close()
  50. def on_after_fork(self):
  51. self._consumer = None
  52. if self._connection is not None:
  53. self._connection.collect()
  54. self._connection = None
  55. def consume_from(self, task_id):
  56. if self._consumer is None:
  57. return self.start(task_id)
  58. queue = self._create_binding(task_id)
  59. if not self._consumer.consuming_from(queue):
  60. self._consumer.add_queue(queue)
  61. self._consumer.consume()
  62. def cancel_for(self, task_id):
  63. if self._consumer:
  64. self._consumer.cancel_by_queue(self._create_binding(task_id).name)
  65. class RPCBackend(base.Backend, AsyncBackendMixin):
  66. """Base class for the RPC result backend."""
  67. Exchange = kombu.Exchange
  68. Producer = kombu.Producer
  69. ResultConsumer = ResultConsumer
  70. #: Exception raised when there are too many messages for a task id.
  71. BacklogLimitExceeded = BacklogLimitExceeded
  72. persistent = False
  73. supports_autoexpire = True
  74. supports_native_join = True
  75. retry_policy = {
  76. 'max_retries': 20,
  77. 'interval_start': 0,
  78. 'interval_step': 1,
  79. 'interval_max': 1,
  80. }
  81. class Consumer(kombu.Consumer):
  82. """Consumer that requires manual declaration of queues."""
  83. auto_declare = False
  84. class Queue(kombu.Queue):
  85. """Queue that never caches declaration."""
  86. can_cache_declaration = False
  87. def __init__(self, app, connection=None, exchange=None, exchange_type=None,
  88. persistent=None, serializer=None, auto_delete=True, **kwargs):
  89. super().__init__(app, **kwargs)
  90. conf = self.app.conf
  91. self._connection = connection
  92. self._out_of_band = {}
  93. self.persistent = self.prepare_persistent(persistent)
  94. self.delivery_mode = 2 if self.persistent else 1
  95. exchange = exchange or conf.result_exchange
  96. exchange_type = exchange_type or conf.result_exchange_type
  97. self.exchange = self._create_exchange(
  98. exchange, exchange_type, self.delivery_mode,
  99. )
  100. self.serializer = serializer or conf.result_serializer
  101. self.auto_delete = auto_delete
  102. self.result_consumer = self.ResultConsumer(
  103. self, self.app, self.accept,
  104. self._pending_results, self._pending_messages,
  105. )
  106. if register_after_fork is not None:
  107. register_after_fork(self, _on_after_fork_cleanup_backend)
  108. def _after_fork(self):
  109. # clear state for child processes.
  110. self._pending_results.clear()
  111. self.result_consumer._after_fork()
  112. def _create_exchange(self, name, type='direct', delivery_mode=2):
  113. # uses direct to queue routing (anon exchange).
  114. return self.Exchange(None)
  115. def _create_binding(self, task_id):
  116. """Create new binding for task with id."""
  117. # RPC backend caches the binding, as one queue is used for all tasks.
  118. return self.binding
  119. def ensure_chords_allowed(self):
  120. raise NotImplementedError(E_NO_CHORD_SUPPORT.strip())
  121. def on_task_call(self, producer, task_id):
  122. # Called every time a task is sent when using this backend.
  123. # We declare the queue we receive replies on in advance of sending
  124. # the message, but we skip this if running in the prefork pool
  125. # (task_join_will_block), as we know the queue is already declared.
  126. if not task_join_will_block():
  127. maybe_declare(self.binding(producer.channel), retry=True)
  128. def destination_for(self, task_id, request):
  129. """Get the destination for result by task id.
  130. Returns:
  131. Tuple[str, str]: tuple of ``(reply_to, correlation_id)``.
  132. """
  133. # Backends didn't always receive the `request`, so we must still
  134. # support old code that relies on current_task.
  135. try:
  136. request = request or current_task.request
  137. except AttributeError:
  138. raise RuntimeError(
  139. 'RPC backend missing task request for {0!r}'.format(task_id))
  140. return request.reply_to, request.correlation_id or task_id
  141. def on_reply_declare(self, task_id):
  142. # Return value here is used as the `declare=` argument
  143. # for Producer.publish.
  144. # By default we don't have to declare anything when sending a result.
  145. pass
  146. def on_result_fulfilled(self, result):
  147. # This usually cancels the queue after the result is received,
  148. # but we don't have to cancel since we have one queue per process.
  149. pass
  150. def as_uri(self, include_password=True):
  151. return 'rpc://'
  152. def store_result(self, task_id, result, state,
  153. traceback=None, request=None, **kwargs):
  154. """Send task return value and state."""
  155. routing_key, correlation_id = self.destination_for(task_id, request)
  156. if not routing_key:
  157. return
  158. with self.app.amqp.producer_pool.acquire(block=True) as producer:
  159. producer.publish(
  160. self._to_result(task_id, state, result, traceback, request),
  161. exchange=self.exchange,
  162. routing_key=routing_key,
  163. correlation_id=correlation_id,
  164. serializer=self.serializer,
  165. retry=True, retry_policy=self.retry_policy,
  166. declare=self.on_reply_declare(task_id),
  167. delivery_mode=self.delivery_mode,
  168. )
  169. return result
  170. def _to_result(self, task_id, state, result, traceback, request):
  171. return {
  172. 'task_id': task_id,
  173. 'status': state,
  174. 'result': self.encode_result(result, state),
  175. 'traceback': traceback,
  176. 'children': self.current_task_children(request),
  177. }
  178. def on_out_of_band_result(self, task_id, message):
  179. # Callback called when a reply for a task is received,
  180. # but we have no idea what do do with it.
  181. # Since the result is not pending, we put it in a separate
  182. # buffer: probably it will become pending later.
  183. if self.result_consumer:
  184. self.result_consumer.on_out_of_band_result(message)
  185. self._out_of_band[task_id] = message
  186. def get_task_meta(self, task_id, backlog_limit=1000):
  187. buffered = self._out_of_band.pop(task_id, None)
  188. if buffered:
  189. return self._set_cache_by_message(task_id, buffered)
  190. # Polling and using basic_get
  191. latest_by_id = {}
  192. prev = None
  193. for acc in self._slurp_from_queue(task_id, self.accept, backlog_limit):
  194. tid = self._get_message_task_id(acc)
  195. prev, latest_by_id[tid] = latest_by_id.get(tid), acc
  196. if prev:
  197. # backends aren't expected to keep history,
  198. # so we delete everything except the most recent state.
  199. prev.ack()
  200. prev = None
  201. latest = latest_by_id.pop(task_id, None)
  202. for tid, msg in latest_by_id.items():
  203. self.on_out_of_band_result(tid, msg)
  204. if latest:
  205. latest.requeue()
  206. return self._set_cache_by_message(task_id, latest)
  207. else:
  208. # no new state, use previous
  209. try:
  210. return self._cache[task_id]
  211. except KeyError:
  212. # result probably pending.
  213. return {'status': states.PENDING, 'result': None}
  214. def _set_cache_by_message(self, task_id, message):
  215. payload = self._cache[task_id] = self.meta_from_decoded(
  216. message.payload)
  217. return payload
  218. def _slurp_from_queue(self, task_id, accept,
  219. limit=1000, no_ack=False):
  220. with self.app.pool.acquire_channel(block=True) as (_, channel):
  221. binding = self._create_binding(task_id)(channel)
  222. binding.declare()
  223. for _ in range(limit):
  224. msg = binding.get(accept=accept, no_ack=no_ack)
  225. if not msg:
  226. break
  227. yield msg
  228. else:
  229. raise self.BacklogLimitExceeded(task_id)
  230. def _get_message_task_id(self, message):
  231. try:
  232. # try property first so we don't have to deserialize
  233. # the payload.
  234. return message.properties['correlation_id']
  235. except (AttributeError, KeyError):
  236. # message sent by old Celery version, need to deserialize.
  237. return message.payload['task_id']
  238. def revive(self, channel):
  239. pass
  240. def reload_task_result(self, task_id):
  241. raise NotImplementedError(
  242. 'reload_task_result is not supported by this backend.')
  243. def reload_group_result(self, task_id):
  244. """Reload group result, even if it has been previously fetched."""
  245. raise NotImplementedError(
  246. 'reload_group_result is not supported by this backend.')
  247. def save_group(self, group_id, result):
  248. raise NotImplementedError(
  249. 'save_group is not supported by this backend.')
  250. def restore_group(self, group_id, cache=True):
  251. raise NotImplementedError(
  252. 'restore_group is not supported by this backend.')
  253. def delete_group(self, group_id):
  254. raise NotImplementedError(
  255. 'delete_group is not supported by this backend.')
  256. def __reduce__(self, args=(), kwargs={}):
  257. return super().__reduce__(args, dict(
  258. kwargs,
  259. connection=self._connection,
  260. exchange=self.exchange.name,
  261. exchange_type=self.exchange.type,
  262. persistent=self.persistent,
  263. serializer=self.serializer,
  264. auto_delete=self.auto_delete,
  265. expires=self.expires,
  266. ))
  267. @property
  268. def binding(self):
  269. return self.Queue(
  270. self.oid, self.exchange, self.oid,
  271. durable=False,
  272. auto_delete=True,
  273. expires=self.expires,
  274. )
  275. @cached_property
  276. def oid(self):
  277. # cached here is the app OID: name of queue we receive results on.
  278. return self.app.oid