celeryctl.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import sys
  2. from optparse import OptionParser, make_option as Option
  3. from pprint import pformat
  4. from textwrap import wrap
  5. from anyjson import deserialize
  6. from celery import __version__
  7. from celery.app import app_or_default, current_app
  8. from celery.bin.base import Command as CeleryCommand
  9. from celery.utils import term
  10. commands = {}
  11. class Error(Exception):
  12. pass
  13. def command(fun):
  14. commands[fun.__name__] = fun
  15. return fun
  16. class Command(object):
  17. help = ""
  18. args = ""
  19. version = __version__
  20. option_list = CeleryCommand.preload_options + (
  21. Option("--quiet", "-q", action="store_true", dest="quiet",
  22. default=False),
  23. Option("--no-color", "-C", dest="no_color", action="store_true",
  24. help="Don't colorize output."),
  25. )
  26. def __init__(self, app=None, no_color=False):
  27. self.app = app_or_default(app)
  28. self.colored = term.colored(enabled=not no_color)
  29. def __call__(self, *args, **kwargs):
  30. try:
  31. self.run(*args, **kwargs)
  32. except Error, exc:
  33. self.error(self.colored.red("Error: %s" % exc))
  34. def error(self, s):
  35. return self.out(s, fh=sys.stderr)
  36. def out(self, s, fh=sys.stdout):
  37. s = str(s)
  38. if not s.endswith("\n"):
  39. s += "\n"
  40. sys.stdout.write(s)
  41. def create_parser(self, prog_name, command):
  42. return OptionParser(prog=prog_name,
  43. usage=self.usage(command),
  44. version=self.version,
  45. option_list=self.option_list)
  46. def run_from_argv(self, prog_name, argv):
  47. self.prog_name = prog_name
  48. self.command = argv[0]
  49. self.arglist = argv[1:]
  50. self.parser = self.create_parser(self.prog_name, self.command)
  51. options, args = self.parser.parse_args(self.arglist)
  52. self.colored = term.colored(enabled=not options.no_color)
  53. self(*args, **options.__dict__)
  54. def run(self, *args, **kwargs):
  55. raise NotImplementedError()
  56. def usage(self, command):
  57. return "%%prog %s [options] %s" % (command, self.args)
  58. def prettify_list(self, n):
  59. c = self.colored
  60. if not n:
  61. return "- empty -"
  62. return "\n".join(str(c.reset(c.white("*"), " %s" % (item, )))
  63. for item in n)
  64. def prettify_dict_ok_error(self, n):
  65. c = self.colored
  66. if "ok" in n:
  67. return (c.green("OK"),
  68. indent(self.prettify(n["ok"])[1]))
  69. elif "error" in n:
  70. return (c.red("ERROR"),
  71. indent(self.prettify(n["error"])[1]))
  72. def prettify(self, n):
  73. OK = str(self.colored.green("OK"))
  74. if isinstance(n, list):
  75. return OK, self.prettify_list(n)
  76. if isinstance(n, dict):
  77. if "ok" in n or "error" in n:
  78. return self.prettify_dict_ok_error(n)
  79. if isinstance(n, basestring):
  80. return OK, unicode(n)
  81. return OK, pformat(n)
  82. class apply(Command):
  83. args = "<task_name>"
  84. option_list = Command.option_list + (
  85. Option("--args", "-a", dest="args"),
  86. Option("--kwargs", "-k", dest="kwargs"),
  87. Option("--eta", dest="eta"),
  88. Option("--countdown", dest="countdown", type="int"),
  89. Option("--expires", dest="expires"),
  90. Option("--serializer", dest="serializer", default="json"),
  91. Option("--queue", dest="queue"),
  92. Option("--exchange", dest="exchange"),
  93. Option("--routing-key", dest="routing_key"),
  94. )
  95. def run(self, name, *_, **kw):
  96. # Positional args.
  97. args = kw.get("args") or ()
  98. if isinstance(args, basestring):
  99. args = deserialize(args)
  100. # Keyword args.
  101. kwargs = kw.get("kwargs") or {}
  102. if isinstance(kwargs, basestring):
  103. kwargs = deserialize(kwargs)
  104. # Expires can be int.
  105. expires = kw.get("expires") or None
  106. try:
  107. expires = int(expires)
  108. except (TypeError, ValueError):
  109. pass
  110. res = self.app.send_task(name, args=args, kwargs=kwargs,
  111. countdown=kw.get("countdown"),
  112. serializer=kw.get("serializer"),
  113. queue=kw.get("queue"),
  114. exchange=kw.get("exchange"),
  115. routing_key=kw.get("routing_key"),
  116. eta=kw.get("eta"),
  117. expires=expires)
  118. self.out(res.task_id)
  119. apply = command(apply)
  120. def pluralize(n, text, suffix='s'):
  121. if n > 1:
  122. return text + suffix
  123. return text
  124. class purge(Command):
  125. def run(self, *args, **kwargs):
  126. app = current_app()
  127. queues = len(app.amqp.queues.keys())
  128. messages_removed = app.control.discard_all()
  129. if messages_removed:
  130. self.out("Purged %s %s from %s known task %s." % (
  131. messages_removed, pluralize(messages_removed, "message"),
  132. queues, pluralize(queues, "queue")))
  133. else:
  134. self.out("No messages purged from %s known %s" % (
  135. queues, pluralize(queues, "queue")))
  136. purge = command(purge)
  137. class result(Command):
  138. args = "<task_id>"
  139. option_list = Command.option_list + (
  140. Option("--task", "-t", dest="task"),
  141. )
  142. def run(self, task_id, *args, **kwargs):
  143. from celery import registry
  144. result_cls = self.app.AsyncResult
  145. task = kwargs.get("task")
  146. if task:
  147. result_cls = registry.tasks[task].AsyncResult
  148. result = result_cls(task_id)
  149. self.out(self.prettify(result.get())[1])
  150. result = command(result)
  151. class inspect(Command):
  152. choices = {"active": 1.0,
  153. "active_queues": 1.0,
  154. "scheduled": 1.0,
  155. "reserved": 1.0,
  156. "stats": 1.0,
  157. "revoked": 1.0,
  158. "registered_tasks": 1.0,
  159. "enable_events": 1.0,
  160. "disable_events": 1.0,
  161. "ping": 0.2,
  162. "add_consumer": 1.0,
  163. "cancel_consumer": 1.0}
  164. option_list = Command.option_list + (
  165. Option("--timeout", "-t", type="float", dest="timeout",
  166. default=None,
  167. help="Timeout in seconds (float) waiting for reply"),
  168. Option("--destination", "-d", dest="destination",
  169. help="Comma separated list of destination node names."))
  170. def usage(self, command):
  171. return "%%prog %s [options] %s [%s]" % (
  172. command, self.args, "|".join(self.choices.keys()))
  173. def run(self, *args, **kwargs):
  174. self.quiet = kwargs.get("quiet", False)
  175. if not args:
  176. raise Error("Missing inspect command. See --help")
  177. command = args[0]
  178. if command == "help":
  179. raise Error("Did you mean 'inspect --help'?")
  180. if command not in self.choices:
  181. raise Error("Unknown inspect command: %s" % command)
  182. destination = kwargs.get("destination")
  183. timeout = kwargs.get("timeout") or self.choices[command]
  184. if destination and isinstance(destination, basestring):
  185. destination = map(str.strip, destination.split(","))
  186. def on_reply(body):
  187. c = self.colored
  188. node = body.keys()[0]
  189. reply = body[node]
  190. status, preply = self.prettify(reply)
  191. self.say("->", c.cyan(node, ": ") + status, indent(preply))
  192. self.say("<-", command)
  193. i = self.app.control.inspect(destination=destination,
  194. timeout=timeout,
  195. callback=on_reply)
  196. replies = getattr(i, command)(*args[1:])
  197. if not replies:
  198. raise Error("No nodes replied within time constraint.")
  199. return replies
  200. def say(self, direction, title, body=""):
  201. c = self.colored
  202. if direction == "<-" and self.quiet:
  203. return
  204. dirstr = not self.quiet and c.bold(c.white(direction), " ") or ""
  205. self.out(c.reset(dirstr, title))
  206. if body and not self.quiet:
  207. self.out(body)
  208. inspect = command(inspect)
  209. def indent(s, n=4):
  210. i = [" " * n + l for l in s.split("\n")]
  211. return "\n".join("\n".join(wrap(j)) for j in i)
  212. class status(Command):
  213. option_list = inspect.option_list
  214. def run(self, *args, **kwargs):
  215. replies = inspect(app=self.app,
  216. no_color=kwargs.get("no_color", False)) \
  217. .run("ping", **dict(kwargs, quiet=True))
  218. if not replies:
  219. raise Error("No nodes replied within time constraint")
  220. nodecount = len(replies)
  221. if not kwargs.get("quiet", False):
  222. self.out("\n%s %s online." % (nodecount,
  223. nodecount > 1 and "nodes" or "node"))
  224. status = command(status)
  225. class help(Command):
  226. def usage(self, command):
  227. return "%%prog <command> [options] %s" % (self.args, )
  228. def run(self, *args, **kwargs):
  229. self.parser.print_help()
  230. usage = ["",
  231. "Type '%s <command> --help' for help on a "
  232. "specific command." % (self.prog_name, ),
  233. "",
  234. "Available commands:"]
  235. for command in list(sorted(commands.keys())):
  236. usage.append(" %s" % command)
  237. self.out("\n".join(usage))
  238. help = command(help)
  239. class celeryctl(CeleryCommand):
  240. commands = commands
  241. def execute(self, command, argv=None):
  242. try:
  243. cls = self.commands[command]
  244. except KeyError:
  245. cls, argv = self.commands["help"], ["help"]
  246. cls = self.commands.get(command) or self.commands["help"]
  247. try:
  248. cls(app=self.app).run_from_argv(self.prog_name, argv)
  249. except Error:
  250. return self.execute("help", argv)
  251. def handle_argv(self, prog_name, argv):
  252. self.prog_name = prog_name
  253. try:
  254. command = argv[0]
  255. except IndexError:
  256. command, argv = "help", ["help"]
  257. return self.execute(command, argv)
  258. def main():
  259. try:
  260. celeryctl().execute_from_commandline()
  261. except KeyboardInterrupt:
  262. pass
  263. if __name__ == "__main__": # pragma: no cover
  264. main()