/usr/share/pyshared/celery/bin/celeryctl.py is in python-celery 2.4.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | # -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import with_statement
import sys
from optparse import OptionParser, make_option as Option
from pprint import pformat
from textwrap import wrap
from anyjson import deserialize
from celery import __version__
from celery.app import app_or_default, current_app
from celery.utils import term
from celery.bin.base import Command as CeleryCommand
commands = {}
class Error(Exception):
pass
def command(fun, name=None):
commands[name or fun.__name__] = fun
return fun
class Command(object):
help = ""
args = ""
version = __version__
option_list = CeleryCommand.preload_options + (
Option("--quiet", "-q", action="store_true", dest="quiet",
default=False),
Option("--no-color", "-C", dest="no_color", action="store_true",
help="Don't colorize output."),
)
def __init__(self, app=None, no_color=False):
self.app = app_or_default(app)
self.colored = term.colored(enabled=not no_color)
def __call__(self, *args, **kwargs):
try:
self.run(*args, **kwargs)
except Error, exc:
self.error(self.colored.red("Error: %s" % exc))
def error(self, s):
return self.out(s, fh=sys.stderr)
def out(self, s, fh=sys.stdout):
s = str(s)
if not s.endswith("\n"):
s += "\n"
sys.stdout.write(s)
def create_parser(self, prog_name, command):
return OptionParser(prog=prog_name,
usage=self.usage(command),
version=self.version,
option_list=self.option_list)
def run_from_argv(self, prog_name, argv):
self.prog_name = prog_name
self.command = argv[0]
self.arglist = argv[1:]
self.parser = self.create_parser(self.prog_name, self.command)
options, args = self.parser.parse_args(self.arglist)
self.colored = term.colored(enabled=not options.no_color)
self(*args, **options.__dict__)
def run(self, *args, **kwargs):
raise NotImplementedError()
def usage(self, command):
return "%%prog %s [options] %s" % (command, self.args)
def prettify_list(self, n):
c = self.colored
if not n:
return "- empty -"
return "\n".join(str(c.reset(c.white("*"), " %s" % (item, )))
for item in n)
def prettify_dict_ok_error(self, n):
c = self.colored
if "ok" in n:
return (c.green("OK"),
indent(self.prettify(n["ok"])[1]))
elif "error" in n:
return (c.red("ERROR"),
indent(self.prettify(n["error"])[1]))
def prettify(self, n):
OK = str(self.colored.green("OK"))
if isinstance(n, list):
return OK, self.prettify_list(n)
if isinstance(n, dict):
if "ok" in n or "error" in n:
return self.prettify_dict_ok_error(n)
if isinstance(n, basestring):
return OK, unicode(n)
return OK, pformat(n)
class list_(Command):
args = "<bindings>"
def list_bindings(self, channel):
fmt = lambda q, e, r: self.out("%s %s %s" % (q.ljust(28),
e.ljust(28), r))
fmt("Queue", "Exchange", "Routing Key")
fmt("-" * 16, "-" * 16, "-" * 16)
for binding in channel.list_bindings():
fmt(*binding)
def run(self, what, *_, **kw):
topics = {"bindings": self.list_bindings}
if what not in topics:
raise ValueError("%r not in %r" % (what, topics.keys()))
with self.app.broker_connection() as conn:
self.app.amqp.get_task_consumer(conn).declare()
with conn.channel() as channel:
return topics[what](channel)
list_ = command(list_, "list")
class apply(Command):
args = "<task_name>"
option_list = Command.option_list + (
Option("--args", "-a", dest="args"),
Option("--kwargs", "-k", dest="kwargs"),
Option("--eta", dest="eta"),
Option("--countdown", dest="countdown", type="int"),
Option("--expires", dest="expires"),
Option("--serializer", dest="serializer", default="json"),
Option("--queue", dest="queue"),
Option("--exchange", dest="exchange"),
Option("--routing-key", dest="routing_key"),
)
def run(self, name, *_, **kw):
# Positional args.
args = kw.get("args") or ()
if isinstance(args, basestring):
args = deserialize(args)
# Keyword args.
kwargs = kw.get("kwargs") or {}
if isinstance(kwargs, basestring):
kwargs = deserialize(kwargs)
# Expires can be int.
expires = kw.get("expires") or None
try:
expires = int(expires)
except (TypeError, ValueError):
pass
res = self.app.send_task(name, args=args, kwargs=kwargs,
countdown=kw.get("countdown"),
serializer=kw.get("serializer"),
queue=kw.get("queue"),
exchange=kw.get("exchange"),
routing_key=kw.get("routing_key"),
eta=kw.get("eta"),
expires=expires)
self.out(res.task_id)
apply = command(apply)
def pluralize(n, text, suffix='s'):
if n > 1:
return text + suffix
return text
class purge(Command):
def run(self, *args, **kwargs):
app = current_app()
queues = len(app.amqp.queues.keys())
messages_removed = app.control.discard_all()
if messages_removed:
self.out("Purged %s %s from %s known task %s." % (
messages_removed, pluralize(messages_removed, "message"),
queues, pluralize(queues, "queue")))
else:
self.out("No messages purged from %s known %s" % (
queues, pluralize(queues, "queue")))
purge = command(purge)
class result(Command):
args = "<task_id>"
option_list = Command.option_list + (
Option("--task", "-t", dest="task"),
)
def run(self, task_id, *args, **kwargs):
from celery import registry
result_cls = self.app.AsyncResult
task = kwargs.get("task")
if task:
result_cls = registry.tasks[task].AsyncResult
result = result_cls(task_id)
self.out(self.prettify(result.get())[1])
result = command(result)
class inspect(Command):
choices = {"active": 1.0,
"active_queues": 1.0,
"scheduled": 1.0,
"reserved": 1.0,
"stats": 1.0,
"revoked": 1.0,
"registered_tasks": 1.0, # alias to registered
"registered": 1.0,
"enable_events": 1.0,
"disable_events": 1.0,
"ping": 0.2,
"add_consumer": 1.0,
"cancel_consumer": 1.0}
option_list = Command.option_list + (
Option("--timeout", "-t", type="float", dest="timeout",
default=None,
help="Timeout in seconds (float) waiting for reply"),
Option("--destination", "-d", dest="destination",
help="Comma separated list of destination node names."))
show_body = True
def usage(self, command):
return "%%prog %s [options] %s [%s]" % (
command, self.args, "|".join(self.choices.keys()))
def run(self, *args, **kwargs):
self.quiet = kwargs.get("quiet", False)
self.show_body = kwargs.get("show_body", True)
if not args:
raise Error("Missing inspect command. See --help")
command = args[0]
if command == "help":
raise Error("Did you mean 'inspect --help'?")
if command not in self.choices:
raise Error("Unknown inspect command: %s" % command)
destination = kwargs.get("destination")
timeout = kwargs.get("timeout") or self.choices[command]
if destination and isinstance(destination, basestring):
destination = map(str.strip, destination.split(","))
def on_reply(body):
c = self.colored
node = body.keys()[0]
reply = body[node]
status, preply = self.prettify(reply)
self.say("->", c.cyan(node, ": ") + status, indent(preply))
self.say("<-", command)
i = self.app.control.inspect(destination=destination,
timeout=timeout,
callback=on_reply)
replies = getattr(i, command)(*args[1:])
if not replies:
raise Error("No nodes replied within time constraint.")
return replies
def say(self, direction, title, body=""):
c = self.colored
if direction == "<-" and self.quiet:
return
dirstr = not self.quiet and c.bold(c.white(direction), " ") or ""
self.out(c.reset(dirstr, title))
if body and self.show_body:
self.out(body)
inspect = command(inspect)
def indent(s, n=4):
i = [" " * n + l for l in s.split("\n")]
return "\n".join("\n".join(wrap(j)) for j in i)
class status(Command):
option_list = inspect.option_list
def run(self, *args, **kwargs):
replies = inspect(app=self.app,
no_color=kwargs.get("no_color", False)) \
.run("ping", **dict(kwargs, quiet=True, show_body=False))
if not replies:
raise Error("No nodes replied within time constraint")
nodecount = len(replies)
if not kwargs.get("quiet", False):
self.out("\n%s %s online." % (nodecount,
nodecount > 1 and "nodes" or "node"))
status = command(status)
class help(Command):
def usage(self, command):
return "%%prog <command> [options] %s" % (self.args, )
def run(self, *args, **kwargs):
self.parser.print_help()
usage = ["",
"Type '%s <command> --help' for help on a "
"specific command." % (self.prog_name, ),
"",
"Available commands:"]
for command in list(sorted(commands.keys())):
usage.append(" %s" % command)
self.out("\n".join(usage))
help = command(help)
class celeryctl(CeleryCommand):
commands = commands
def execute(self, command, argv=None):
try:
cls = self.commands[command]
except KeyError:
cls, argv = self.commands["help"], ["help"]
cls = self.commands.get(command) or self.commands["help"]
try:
cls(app=self.app).run_from_argv(self.prog_name, argv)
except Error:
return self.execute("help", argv)
def remove_options_at_beginning(self, argv, index=0):
if argv:
while index <= len(argv):
value = argv[index]
if value.startswith("--"):
pass
elif value.startswith("-"):
index += 1
else:
return argv[index:]
index += 1
return []
def handle_argv(self, prog_name, argv):
self.prog_name = prog_name
argv = self.remove_options_at_beginning(argv)
try:
command = argv[0]
except IndexError:
command, argv = "help", ["help"]
return self.execute(command, argv)
def main():
try:
celeryctl().execute_from_commandline()
except KeyboardInterrupt:
pass
if __name__ == "__main__": # pragma: no cover
main()
|