Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpc.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.backends.rpc
  4. ~~~~~~~~~~~~~~~~~~~
  5. RPC-style result backend, using reply-to and one queue per client.
  6. """
  7. from __future__ import absolute_import
  8. from kombu import Consumer, Exchange
  9. from kombu.common import maybe_declare
  10. from kombu.utils import cached_property
  11. from celery import current_task
  12. from celery.backends import amqp
  13. __all__ = ['RPCBackend']
  14. class RPCBackend(amqp.AMQPBackend):
  15. persistent = False
  16. class Consumer(Consumer):
  17. auto_declare = False
  18. def _create_exchange(self, name, type='direct', delivery_mode=2):
  19. # uses direct to queue routing (anon exchange).
  20. return Exchange(None)
  21. def on_task_call(self, producer, task_id):
  22. maybe_declare(self.binding(producer.channel), retry=True)
  23. def _create_binding(self, task_id):
  24. return self.binding
  25. def _many_bindings(self, ids):
  26. return [self.binding]
  27. def rkey(self, task_id):
  28. return task_id
  29. def destination_for(self, task_id, request):
  30. # Request is a new argument for backends, so must still support
  31. # old code that rely on current_task
  32. try:
  33. request = request or current_task.request
  34. except AttributeError:
  35. raise RuntimeError(
  36. 'RPC backend missing task request for {0!r}'.format(task_id),
  37. )
  38. return request.reply_to, request.correlation_id or task_id
  39. def on_reply_declare(self, task_id):
  40. pass
  41. def as_uri(self, include_password=True):
  42. return 'rpc://'
  43. @property
  44. def binding(self):
  45. return self.Queue(self.oid, self.exchange, self.oid,
  46. durable=False, auto_delete=False)
  47. @cached_property
  48. def oid(self):
  49. return self.app.oid