control.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.app.control
  4. ~~~~~~~~~~~~~~~~~~~
  5. Client for worker remote control commands.
  6. Server implementation is in :mod:`celery.worker.control`.
  7. :copyright: (c) 2009 - 2012 by Ask Solem.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. from __future__ import absolute_import
  11. from __future__ import with_statement
  12. from kombu.pidbox import Mailbox
  13. from kombu.utils import cached_property
  14. from . import app_or_default
  15. def flatten_reply(reply):
  16. nodes = {}
  17. for item in reply:
  18. nodes.update(item)
  19. return nodes
  20. class Inspect(object):
  21. app = None
  22. def __init__(self, destination=None, timeout=1, callback=None,
  23. connection=None, app=None):
  24. self.app = app or self.app
  25. self.destination = destination
  26. self.timeout = timeout
  27. self.callback = callback
  28. self.connection = connection
  29. def _prepare(self, reply):
  30. if not reply:
  31. return
  32. by_node = flatten_reply(reply)
  33. if self.destination and \
  34. not isinstance(self.destination, (list, tuple)):
  35. return by_node.get(self.destination)
  36. return by_node
  37. def _request(self, command, **kwargs):
  38. return self._prepare(self.app.control.broadcast(command,
  39. arguments=kwargs,
  40. destination=self.destination,
  41. callback=self.callback,
  42. connection=self.connection,
  43. timeout=self.timeout, reply=True))
  44. def report(self):
  45. return self._request("report")
  46. def active(self, safe=False):
  47. return self._request("dump_active", safe=safe)
  48. def scheduled(self, safe=False):
  49. return self._request("dump_schedule", safe=safe)
  50. def reserved(self, safe=False):
  51. return self._request("dump_reserved", safe=safe)
  52. def stats(self):
  53. return self._request("stats")
  54. def revoked(self):
  55. return self._request("dump_revoked")
  56. def registered(self):
  57. return self._request("dump_tasks")
  58. registered_tasks = registered
  59. def ping(self):
  60. return self._request("ping")
  61. def active_queues(self):
  62. return self._request("active_queues")
  63. class Control(object):
  64. Mailbox = Mailbox
  65. def __init__(self, app=None):
  66. self.app = app_or_default(app)
  67. self.mailbox = self.Mailbox("celeryd", type="fanout")
  68. @cached_property
  69. def inspect(self):
  70. return self.app.subclass_with_self(Inspect, reverse="control.inspect")
  71. def purge(self, connection=None):
  72. """Discard all waiting tasks.
  73. This will ignore all tasks waiting for execution, and they will
  74. be deleted from the messaging server.
  75. :returns: the number of tasks discarded.
  76. """
  77. with self.app.default_connection(connection) as conn:
  78. return self.app.amqp.TaskConsumer(conn).purge()
  79. discard_all = purge
  80. def revoke(self, task_id, destination=None, terminate=False,
  81. signal="SIGTERM", **kwargs):
  82. """Tell all (or specific) workers to revoke a task by id.
  83. If a task is revoked, the workers will ignore the task and
  84. not execute it after all.
  85. :param task_id: Id of the task to revoke.
  86. :keyword terminate: Also terminate the process currently working
  87. on the task (if any).
  88. :keyword signal: Name of signal to send to process if terminate.
  89. Default is TERM.
  90. See :meth:`broadcast` for supported keyword arguments.
  91. """
  92. return self.broadcast("revoke", destination=destination,
  93. arguments={"task_id": task_id,
  94. "terminate": terminate,
  95. "signal": signal}, **kwargs)
  96. def ping(self, destination=None, timeout=1, **kwargs):
  97. """Ping all (or specific) workers.
  98. Returns answer from alive workers.
  99. See :meth:`broadcast` for supported keyword arguments.
  100. """
  101. return self.broadcast("ping", reply=True, destination=destination,
  102. timeout=timeout, **kwargs)
  103. def rate_limit(self, task_name, rate_limit, destination=None, **kwargs):
  104. """Tell all (or specific) workers to set a new rate limit
  105. for task by type.
  106. :param task_name: Name of task to change rate limit for.
  107. :param rate_limit: The rate limit as tasks per second, or a rate limit
  108. string (`"100/m"`, etc.
  109. see :attr:`celery.task.base.Task.rate_limit` for
  110. more information).
  111. See :meth:`broadcast` for supported keyword arguments.
  112. """
  113. return self.broadcast("rate_limit", destination=destination,
  114. arguments={"task_name": task_name,
  115. "rate_limit": rate_limit},
  116. **kwargs)
  117. def add_consumer(self, queue, exchange=None, exchange_type="direct",
  118. routing_key=None, **options):
  119. """Tell all (or specific) workers to start consuming from a new queue.
  120. Only the queue name is required as if only the queue is specified
  121. then the exchange/routing key will be set to the same name (
  122. like automatic queues do).
  123. .. note::
  124. This command does not respect the default queue/exchange
  125. options in the configuration.
  126. :param queue: Name of queue to start consuming from.
  127. :keyword exchange: Optional name of exchange.
  128. :keyword exchange_type: Type of exchange (defaults to "direct")
  129. command to, when empty broadcast to all workers.
  130. :keyword routing_key: Optional routing key.
  131. See :meth:`broadcast` for supported keyword arguments.
  132. """
  133. return self.broadcast("add_consumer",
  134. arguments={"queue": queue, "exchange": exchange,
  135. "exchange_type": exchange_type,
  136. "routing_key": routing_key}, **options)
  137. def cancel_consumer(self, queue, **kwargs):
  138. """Tell all (or specific) workers to stop consuming from ``queue``.
  139. Supports the same keyword arguments as :meth:`broadcast`.
  140. """
  141. return self.broadcast("cancel_consumer",
  142. arguments={"queue": queue}, **kwargs)
  143. def time_limit(self, task_name, soft=None, hard=None, **kwargs):
  144. """Tell all (or specific) workers to set time limits for
  145. a task by type.
  146. :param task_name: Name of task to change time limits for.
  147. :keyword soft: New soft time limit (in seconds).
  148. :keyword hard: New hard time limit (in seconds).
  149. Any additional keyword arguments are passed on to :meth:`broadcast`.
  150. """
  151. return self.broadcast("time_limit",
  152. arguments={"task_name": task_name,
  153. "hard": hard, "soft": soft}, **kwargs)
  154. def enable_events(self, destination=None, **kwargs):
  155. """Tell all (or specific) workers to enable events."""
  156. return self.broadcast("enable_events", {}, destination, **kwargs)
  157. def disable_events(self, destination=None, **kwargs):
  158. """Tell all (or specific) workers to enable events."""
  159. return self.broadcast("disable_events", {}, destination, **kwargs)
  160. def pool_grow(self, n=1, destination=None, **kwargs):
  161. """Tell all (or specific) workers to grow the pool by ``n``.
  162. Supports the same arguments as :meth:`broadcast`.
  163. """
  164. return self.broadcast("pool_grow", {}, destination, **kwargs)
  165. def pool_shrink(self, n=1, destination=None, **kwargs):
  166. """Tell all (or specific) workers to shrink the pool by ``n``.
  167. Supports the same arguments as :meth:`broadcast`.
  168. """
  169. return self.broadcast("pool_shrink", {}, destination, **kwargs)
  170. def broadcast(self, command, arguments=None, destination=None,
  171. connection=None, reply=False, timeout=1, limit=None,
  172. callback=None, channel=None, **extra_kwargs):
  173. """Broadcast a control command to the celery workers.
  174. :param command: Name of command to send.
  175. :param arguments: Keyword arguments for the command.
  176. :keyword destination: If set, a list of the hosts to send the
  177. command to, when empty broadcast to all workers.
  178. :keyword connection: Custom broker connection to use, if not set,
  179. a connection will be established automatically.
  180. :keyword reply: Wait for and return the reply.
  181. :keyword timeout: Timeout in seconds to wait for the reply.
  182. :keyword limit: Limit number of replies.
  183. :keyword callback: Callback called immediately for each reply
  184. received.
  185. """
  186. with self.app.default_connection(connection) as conn:
  187. if channel is None:
  188. channel = conn.default_channel
  189. arguments = dict(arguments, **extra_kwargs)
  190. return self.mailbox(conn)._broadcast(command, arguments,
  191. destination, reply, timeout,
  192. limit, callback,
  193. channel=channel)