control.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. from kombu.pidbox import Mailbox
  2. from celery.app import app_or_default
  3. from celery.utils import gen_unique_id
  4. def flatten_reply(reply):
  5. nodes = {}
  6. for item in reply:
  7. nodes.update(item)
  8. return nodes
  9. class Inspect(object):
  10. def __init__(self, control, destination=None, timeout=1, callback=None,):
  11. self.destination = destination
  12. self.timeout = timeout
  13. self.callback = callback
  14. self.control = control
  15. def _prepare(self, reply):
  16. if not reply:
  17. return
  18. by_node = flatten_reply(reply)
  19. if self.destination and \
  20. not isinstance(self.destination, (list, tuple)):
  21. return by_node.get(self.destination)
  22. return by_node
  23. def _request(self, command, **kwargs):
  24. return self._prepare(self.control.broadcast(command,
  25. arguments=kwargs,
  26. destination=self.destination,
  27. callback=self.callback,
  28. timeout=self.timeout, reply=True))
  29. def active(self, safe=False):
  30. return self._request("dump_active", safe=safe)
  31. def scheduled(self, safe=False):
  32. return self._request("dump_schedule", safe=safe)
  33. def reserved(self, safe=False):
  34. return self._request("dump_reserved", safe=safe)
  35. def stats(self):
  36. return self._request("stats")
  37. def revoked(self):
  38. return self._request("dump_revoked")
  39. def registered_tasks(self):
  40. return self._request("dump_tasks")
  41. def enable_events(self):
  42. return self._request("enable_events")
  43. def disable_events(self):
  44. return self._request("disable_events")
  45. def ping(self):
  46. return self._request("ping")
  47. def add_consumer(self, queue, exchange=None, exchange_type="direct",
  48. routing_key=None, **options):
  49. return self._request("add_consumer", queue=queue, exchange=exchange,
  50. exchange_type=exchange_type,
  51. routing_key=routing_key, **options)
  52. def cancel_consumer(self, queue, **kwargs):
  53. return self._request("cancel_consumer", queue=queue, **kwargs)
  54. class Control(object):
  55. Mailbox = Mailbox
  56. def __init__(self, app):
  57. self.app = app
  58. self.mailbox = self.Mailbox("celeryd", type="fanout")
  59. def inspect(self, destination=None, timeout=1, callback=None):
  60. return Inspect(self, destination=destination, timeout=timeout,
  61. callback=callback)
  62. def discard_all(self, connection=None, connect_timeout=None):
  63. """Discard all waiting tasks.
  64. This will ignore all tasks waiting for execution, and they will
  65. be deleted from the messaging server.
  66. :returns: the number of tasks discarded.
  67. """
  68. def _do_discard(connection=None, connect_timeout=None):
  69. consumer = self.app.amqp.get_task_consumer(connection=connection)
  70. try:
  71. return consumer.discard_all()
  72. finally:
  73. consumer.close()
  74. return self.app.with_default_connection(_do_discard)(
  75. connection=connection, connect_timeout=connect_timeout)
  76. def revoke(self, task_id, destination=None, **kwargs):
  77. """Revoke a task by id.
  78. If a task is revoked, the workers will ignore the task and
  79. not execute it after all.
  80. :param task_id: Id of the task to revoke.
  81. :keyword destination: If set, a list of the hosts to send the
  82. command to, when empty broadcast to all workers.
  83. :keyword connection: Custom broker connection to use, if not set,
  84. a connection will be established automatically.
  85. :keyword connect_timeout: Timeout for new connection if a custom
  86. connection is not provided.
  87. :keyword reply: Wait for and return the reply.
  88. :keyword timeout: Timeout in seconds to wait for the reply.
  89. :keyword limit: Limit number of replies.
  90. """
  91. return self.broadcast("revoke", destination=destination,
  92. arguments={"task_id": task_id}, **kwargs)
  93. def ping(self, destination=None, timeout=1, **kwargs):
  94. """Ping workers.
  95. Returns answer from alive workers.
  96. :keyword destination: If set, a list of the hosts to send the
  97. command to, when empty broadcast to all workers.
  98. :keyword connection: Custom broker connection to use, if not set,
  99. a connection will be established automatically.
  100. :keyword connect_timeout: Timeout for new connection if a custom
  101. connection is not provided.
  102. :keyword reply: Wait for and return the reply.
  103. :keyword timeout: Timeout in seconds to wait for the reply.
  104. :keyword limit: Limit number of replies.
  105. """
  106. return self.broadcast("ping", reply=True, destination=destination,
  107. timeout=timeout, **kwargs)
  108. def rate_limit(self, task_name, rate_limit, destination=None, **kwargs):
  109. """Set rate limit for task by type.
  110. :param task_name: Type of task to change rate limit for.
  111. :param rate_limit: The rate limit as tasks per second, or a rate limit
  112. string (``"100/m"``, etc.
  113. see :attr:`celery.task.base.Task.rate_limit` for
  114. more information).
  115. :keyword destination: If set, a list of the hosts to send the
  116. command to, when empty broadcast to all workers.
  117. :keyword connection: Custom broker connection to use, if not set,
  118. a connection will be established automatically.
  119. :keyword connect_timeout: Timeout for new connection if a custom
  120. connection is not provided.
  121. :keyword reply: Wait for and return the reply.
  122. :keyword timeout: Timeout in seconds to wait for the reply.
  123. :keyword limit: Limit number of replies.
  124. """
  125. return self.broadcast("rate_limit", destination=destination,
  126. arguments={"task_name": task_name,
  127. "rate_limit": rate_limit},
  128. **kwargs)
  129. def broadcast(self, command, arguments=None, destination=None,
  130. connection=None, connect_timeout=None, reply=False, timeout=1,
  131. limit=None, callback=None):
  132. """Broadcast a control command to the celery workers.
  133. :param command: Name of command to send.
  134. :param arguments: Keyword arguments for the command.
  135. :keyword destination: If set, a list of the hosts to send the
  136. command to, when empty broadcast to all workers.
  137. :keyword connection: Custom broker connection to use, if not set,
  138. a connection will be established automatically.
  139. :keyword connect_timeout: Timeout for new connection if a custom
  140. connection is not provided.
  141. :keyword reply: Wait for and return the reply.
  142. :keyword timeout: Timeout in seconds to wait for the reply.
  143. :keyword limit: Limit number of replies.
  144. :keyword callback: Callback called immediately for each reply
  145. received.
  146. """
  147. def _do_broadcast(connection=None, connect_timeout=None):
  148. return self.mailbox(connection)._broadcast(command, arguments,
  149. destination, reply,
  150. timeout, limit,
  151. callback)
  152. return self.app.with_default_connection(_do_broadcast)(
  153. connection=connection, connect_timeout=connect_timeout)
  154. _default_control = Control(app_or_default())
  155. broadcast = _default_control.broadcast
  156. rate_limit = _default_control.rate_limit
  157. ping = _default_control.ping
  158. revoke = _default_control.revoke
  159. discard_all = _default_control.discard_all
  160. inspect = _default_control.inspect