control.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. """
  8. from __future__ import absolute_import
  9. import warnings
  10. from kombu.pidbox import Mailbox
  11. from kombu.utils import cached_property
  12. from celery.exceptions import DuplicateNodenameWarning
  13. __all__ = ['Inspect', 'Control', 'flatten_reply']
  14. W_DUPNODE = """\
  15. Received multiple replies from node name {0!r}.
  16. Please make sure you give each node a unique nodename using the `-n` option.\
  17. """
  18. def flatten_reply(reply):
  19. nodes = {}
  20. seen = set()
  21. for item in reply:
  22. dup = next((nodename in seen for nodename in item), None)
  23. if dup:
  24. warnings.warn(DuplicateNodenameWarning(
  25. W_DUPNODE.format(dup),
  26. ))
  27. seen.update(item)
  28. nodes.update(item)
  29. return nodes
  30. class Inspect(object):
  31. app = None
  32. def __init__(self, destination=None, timeout=1, callback=None,
  33. connection=None, app=None, limit=None):
  34. self.app = app or self.app
  35. self.destination = destination
  36. self.timeout = timeout
  37. self.callback = callback
  38. self.connection = connection
  39. self.limit = limit
  40. def _prepare(self, reply):
  41. if not reply:
  42. return
  43. by_node = flatten_reply(reply)
  44. if self.destination and \
  45. not isinstance(self.destination, (list, tuple)):
  46. return by_node.get(self.destination)
  47. return by_node
  48. def _request(self, command, **kwargs):
  49. return self._prepare(self.app.control.broadcast(
  50. command,
  51. arguments=kwargs,
  52. destination=self.destination,
  53. callback=self.callback,
  54. connection=self.connection,
  55. limit=self.limit,
  56. timeout=self.timeout, reply=True,
  57. ))
  58. def report(self):
  59. return self._request('report')
  60. def clock(self):
  61. return self._request('clock')
  62. def active(self, safe=False):
  63. return self._request('dump_active', safe=safe)
  64. def scheduled(self, safe=False):
  65. return self._request('dump_schedule', safe=safe)
  66. def reserved(self, safe=False):
  67. return self._request('dump_reserved', safe=safe)
  68. def stats(self):
  69. return self._request('stats')
  70. def revoked(self):
  71. return self._request('dump_revoked')
  72. def registered(self, *taskinfoitems):
  73. return self._request('dump_tasks', taskinfoitems=taskinfoitems)
  74. registered_tasks = registered
  75. def ping(self):
  76. return self._request('ping')
  77. def active_queues(self):
  78. return self._request('active_queues')
  79. def query_task(self, ids):
  80. return self._request('query_task', ids=ids)
  81. def conf(self):
  82. return self._request('dump_conf')
  83. def hello(self, from_node, revoked=None):
  84. return self._request('hello', from_node=from_node, revoked=revoked)
  85. def memsample(self):
  86. return self._request('memsample')
  87. def memdump(self, samples=10):
  88. return self._request('memdump', samples=samples)
  89. def objgraph(self, type='Request', n=200, max_depth=10):
  90. return self._request('objgraph', num=n, max_depth=max_depth, type=type)
  91. class Control(object):
  92. Mailbox = Mailbox
  93. def __init__(self, app=None):
  94. self.app = app
  95. self.mailbox = self.Mailbox('celery', type='fanout',
  96. accept=self.app.conf.CELERY_ACCEPT_CONTENT)
  97. @cached_property
  98. def inspect(self):
  99. return self.app.subclass_with_self(Inspect, reverse='control.inspect')
  100. def purge(self, connection=None):
  101. """Discard all waiting tasks.
  102. This will ignore all tasks waiting for execution, and they will
  103. be deleted from the messaging server.
  104. :returns: the number of tasks discarded.
  105. """
  106. with self.app.connection_or_acquire(connection) as conn:
  107. return self.app.amqp.TaskConsumer(conn).purge()
  108. discard_all = purge
  109. def election(self, id, topic, action=None, connection=None):
  110. self.broadcast('election', connection=connection, arguments={
  111. 'id': id, 'topic': topic, 'action': action,
  112. })
  113. def revoke(self, task_id, destination=None, terminate=False,
  114. signal='SIGTERM', **kwargs):
  115. """Tell all (or specific) workers to revoke a task by id.
  116. If a task is revoked, the workers will ignore the task and
  117. not execute it after all.
  118. :param task_id: Id of the task to revoke.
  119. :keyword terminate: Also terminate the process currently working
  120. on the task (if any).
  121. :keyword signal: Name of signal to send to process if terminate.
  122. Default is TERM.
  123. See :meth:`broadcast` for supported keyword arguments.
  124. """
  125. return self.broadcast('revoke', destination=destination,
  126. arguments={'task_id': task_id,
  127. 'terminate': terminate,
  128. 'signal': signal}, **kwargs)
  129. def ping(self, destination=None, timeout=1, **kwargs):
  130. """Ping all (or specific) workers.
  131. Will return the list of answers.
  132. See :meth:`broadcast` for supported keyword arguments.
  133. """
  134. return self.broadcast('ping', reply=True, destination=destination,
  135. timeout=timeout, **kwargs)
  136. def rate_limit(self, task_name, rate_limit, destination=None, **kwargs):
  137. """Tell all (or specific) workers to set a new rate limit
  138. for task by type.
  139. :param task_name: Name of task to change rate limit for.
  140. :param rate_limit: The rate limit as tasks per second, or a rate limit
  141. string (`'100/m'`, etc.
  142. see :attr:`celery.task.base.Task.rate_limit` for
  143. more information).
  144. See :meth:`broadcast` for supported keyword arguments.
  145. """
  146. return self.broadcast('rate_limit', destination=destination,
  147. arguments={'task_name': task_name,
  148. 'rate_limit': rate_limit},
  149. **kwargs)
  150. def add_consumer(self, queue, exchange=None, exchange_type='direct',
  151. routing_key=None, options=None, **kwargs):
  152. """Tell all (or specific) workers to start consuming from a new queue.
  153. Only the queue name is required as if only the queue is specified
  154. then the exchange/routing key will be set to the same name (
  155. like automatic queues do).
  156. .. note::
  157. This command does not respect the default queue/exchange
  158. options in the configuration.
  159. :param queue: Name of queue to start consuming from.
  160. :keyword exchange: Optional name of exchange.
  161. :keyword exchange_type: Type of exchange (defaults to 'direct')
  162. command to, when empty broadcast to all workers.
  163. :keyword routing_key: Optional routing key.
  164. :keyword options: Additional options as supported
  165. by :meth:`kombu.entitiy.Queue.from_dict`.
  166. See :meth:`broadcast` for supported keyword arguments.
  167. """
  168. return self.broadcast(
  169. 'add_consumer',
  170. arguments=dict({'queue': queue, 'exchange': exchange,
  171. 'exchange_type': exchange_type,
  172. 'routing_key': routing_key}, **options or {}),
  173. **kwargs
  174. )
  175. def cancel_consumer(self, queue, **kwargs):
  176. """Tell all (or specific) workers to stop consuming from ``queue``.
  177. Supports the same keyword arguments as :meth:`broadcast`.
  178. """
  179. return self.broadcast(
  180. 'cancel_consumer', arguments={'queue': queue}, **kwargs
  181. )
  182. def time_limit(self, task_name, soft=None, hard=None, **kwargs):
  183. """Tell all (or specific) workers to set time limits for
  184. a task by type.
  185. :param task_name: Name of task to change time limits for.
  186. :keyword soft: New soft time limit (in seconds).
  187. :keyword hard: New hard time limit (in seconds).
  188. Any additional keyword arguments are passed on to :meth:`broadcast`.
  189. """
  190. return self.broadcast(
  191. 'time_limit',
  192. arguments={'task_name': task_name,
  193. 'hard': hard, 'soft': soft}, **kwargs)
  194. def enable_events(self, destination=None, **kwargs):
  195. """Tell all (or specific) workers to enable events."""
  196. return self.broadcast('enable_events', {}, destination, **kwargs)
  197. def disable_events(self, destination=None, **kwargs):
  198. """Tell all (or specific) workers to enable events."""
  199. return self.broadcast('disable_events', {}, destination, **kwargs)
  200. def pool_grow(self, n=1, destination=None, **kwargs):
  201. """Tell all (or specific) workers to grow the pool by ``n``.
  202. Supports the same arguments as :meth:`broadcast`.
  203. """
  204. return self.broadcast('pool_grow', {'n': n}, destination, **kwargs)
  205. def pool_shrink(self, n=1, destination=None, **kwargs):
  206. """Tell all (or specific) workers to shrink the pool by ``n``.
  207. Supports the same arguments as :meth:`broadcast`.
  208. """
  209. return self.broadcast('pool_shrink', {'n': n}, destination, **kwargs)
  210. def broadcast(self, command, arguments=None, destination=None,
  211. connection=None, reply=False, timeout=1, limit=None,
  212. callback=None, channel=None, **extra_kwargs):
  213. """Broadcast a control command to the celery workers.
  214. :param command: Name of command to send.
  215. :param arguments: Keyword arguments for the command.
  216. :keyword destination: If set, a list of the hosts to send the
  217. command to, when empty broadcast to all workers.
  218. :keyword connection: Custom broker connection to use, if not set,
  219. a connection will be established automatically.
  220. :keyword reply: Wait for and return the reply.
  221. :keyword timeout: Timeout in seconds to wait for the reply.
  222. :keyword limit: Limit number of replies.
  223. :keyword callback: Callback called immediately for each reply
  224. received.
  225. """
  226. with self.app.connection_or_acquire(connection) as conn:
  227. arguments = dict(arguments or {}, **extra_kwargs)
  228. return self.mailbox(conn)._broadcast(
  229. command, arguments, destination, reply, timeout,
  230. limit, callback, channel=channel,
  231. )