Ver código fonte

celeryd_multi: Made the code a bit simpler.

Ask Solem 15 anos atrás
pai
commit
f3c9d37c3e
1 arquivos alterados com 112 adições e 116 exclusões
  1. 112 116
      celery/bin/celeryd_multi.py

+ 112 - 116
celery/bin/celeryd_multi.py

@@ -5,13 +5,74 @@ import socket
 from celery.utils.compat import defaultdict
 from carrot.utils import rpartition
 
+EXAMPLES = """
+Some examples:
 
-class OptionParser(object):
+    # Advanced example with 10 workers:
+    #   * Three of the workers processes the images and video queue
+    #   * Two of the workers processes the data queue with loglevel DEBUG
+    #   * the rest processes the default' queue.
+    $ celeryd-multi start 10 -l INFO -Q:1-3 images,video -Q:4,5:data
+        -Q default -L:4,5 DEBUG
+
+    # get commands to start 10 workers, with 3 processes each
+    $ celeryd-multi start 3 -c 3
+    celeryd -n celeryd1.myhost -c 3
+    celeryd -n celeryd2.myhost -c 3
+    celeryd- n celeryd3.myhost -c 3
+
+    # start 3 named workers
+    $ celeryd-multi start image video data -c 3
+    celeryd -n image.myhost -c 3
+    celeryd -n video.myhost -c 3
+    celeryd -n data.myhost -c 3
+
+    # specify custom hostname
+    $ celeryd-multi start 2 -n worker.example.com -c 3
+    celeryd -n celeryd1.worker.example.com -c 3
+    celeryd -n celeryd2.worker.example.com -c 3
+
+    # Additionl options are added to each celeryd',
+    # but you can also modify the options for ranges of or single workers
+
+    # 3 workers: Two with 3 processes, and one with 10 processes.
+    $ celeryd-multi start 3 -c 3 -c:1 10
+    celeryd -n celeryd1.myhost -c 10
+    celeryd -n celeryd2.myhost -c 3
+    celeryd -n celeryd3.myhost -c 3
+
+    # can also specify options for named workers
+    $ celeryd-multi start image video data -c 3 -c:image 10
+    celeryd -n image.myhost -c 10
+    celeryd -n video.myhost -c 3
+    celeryd -n data.myhost -c 3
+
+    # ranges and lists of workers in options is also allowed:
+    # (-c:1-3 can also be written as -c:1,2,3)
+    $ celeryd-multi start 5 -c 3  -c:1-3 10
+    celeryd -n celeryd1.myhost -c 10
+    celeryd -n celeryd2.myhost -c 10
+    celeryd -n celeryd3.myhost -c 10
+    celeryd -n celeryd4.myhost -c 3
+    celeryd -n celeryd5.myhost -c 3
+
+    # lists also works with named workers
+    $ celeryd-multi start foo bar baz xuzzy -c 3 -c:foo,bar,baz 10
+    celeryd -n foo.myhost -c 10
+    celeryd -n bar.myhost -c 10
+    celeryd -n baz.myhost -c 10
+    celeryd -n xuzzy.myhost -c 3
+"""
+
+
+class NamespacedOptionParser(object):
 
     def __init__(self, args):
         self.args = args
         self.options = {}
         self.values = []
+        self.namespaces = defaultdict(lambda: {})
+
         self.parse()
 
     def parse(self):
@@ -40,16 +101,10 @@ class OptionParser(object):
     def process_short_opt(self, arg, value=None):
         self.add_option(arg, value, short=True)
 
-    def set_option(self, arg, value, short=False):
-        prefix = short and "-" or "--"
-        self.options[prefix + arg] = value
-
-
-class NamespacedOptionParser(OptionParser):
-
-    def __init__(self, args):
-        self.namespaces = defaultdict(lambda: {})
-        super(NamespacedOptionParser, self).__init__(args)
+    def optmerge(self, ns, defaults=None):
+        if defaults is None:
+            defaults = self.options
+        return dict(defaults, **self.namespaces[ns])
 
     def add_option(self, name, value, short=False, ns=None):
         prefix = short and "-" or "--"
@@ -59,11 +114,6 @@ class NamespacedOptionParser(OptionParser):
             dest = self.namespaces[ns]
         dest[prefix + name] = value
 
-    def optmerge(self, ns, defaults=None):
-        if defaults is None:
-            defaults = self.options
-        return dict(defaults, **self.namespaces[ns])
-
 
 def quote(v):
     return "\\'".join("'" + p + "'" for p in v.split("'"))
@@ -131,119 +181,65 @@ def multi_args(p, cmd="celeryd", append="", prefix="", suffix=""):
         yield this_name, line, expand
 
 
-def names(argv, cmd):
-    p = NamespacedOptionParser(argv)
-    print("\n".join(hostname
-                        for hostname, _, _ in multi_args(p, cmd)))
-
+def say(m):
+    sys.stderr.write("%s\n" % (m, ))
 
-def get(argv, cmd):
-    wanted = argv[0]
-    p = NamespacedOptionParser(argv[1:])
-    for name, worker, _ in multi_args(p, cmd):
-        if name == wanted:
-            print(worker)
-            return
 
+class MultiTool(object):
 
-def start(argv, cmd):
-    p = NamespacedOptionParser(argv)
-    print("\n".join(worker
-                        for _, worker, _ in multi_args(p, cmd)))
-
-
-def expand(argv, cmd=None):
-    template = argv[0]
-    p = NamespacedOptionParser(argv[1:])
-    for _, _, expander in multi_args(p, cmd):
-        print(expander(template))
-
-
-def help(argv, cmd=None):
-    print("""Some examples:
-
-    # Advanced example with 10 workers:
-    #   * Three of the workers processes the images and video queue
-    #   * Two of the workers processes the data queue with loglevel DEBUG
-    #   * the rest processes the default' queue.
-    $ celeryd-multi start 10 -l INFO -Q:1-3 images,video -Q:4,5:data
-        -Q default -L:4,5 DEBUG
-
-    # get commands to start 10 workers, with 3 processes each
-    $ celeryd-multi start 3 -c 3
-    celeryd -n celeryd1.myhost -c 3
-    celeryd -n celeryd2.myhost -c 3
-    celeryd- n celeryd3.myhost -c 3
+    def __init__(self):
+        self.commands = {"start": self.start,
+                         "names": self.names,
+                         "expand": self.expand,
+                         "get": self.get,
+                         "help": self.help}
 
-    # start 3 named workers
-    $ celeryd-multi start image video data -c 3
-    celeryd -n image.myhost -c 3
-    celeryd -n video.myhost -c 3
-    celeryd -n data.myhost -c 3
+    def __call__(self, argv, cmd="celeryd"):
+        if len(argv) == 0:
+            self.usage()
+            sys.exit(0)
 
-    # specify custom hostname
-    $ celeryd-multi start 2 -n worker.example.com -c 3
-    celeryd -n celeryd1.worker.example.com -c 3
-    celeryd -n celeryd2.worker.example.com -c 3
+        try:
+            return self.commands[argv[0]](argv[1:], cmd)
+        except KeyError, exc:
+            say("Invalid command: %s" % argv[0])
+            self.usage()
+            sys.exit(1)
 
-    # Additionl options are added to each celeryd',
-    # but you can also modify the options for ranges of or single workers
-
-    # 3 workers: Two with 3 processes, and one with 10 processes.
-    $ celeryd-multi start 3 -c 3 -c:1 10
-    celeryd -n celeryd1.myhost -c 10
-    celeryd -n celeryd2.myhost -c 3
-    celeryd -n celeryd3.myhost -c 3
-
-    # can also specify options for named workers
-    $ celeryd-multi start image video data -c 3 -c:image 10
-    celeryd -n image.myhost -c 10
-    celeryd -n video.myhost -c 3
-    celeryd -n data.myhost -c 3
-
-    # ranges and lists of workers in options is also allowed:
-    # (-c:1-3 can also be written as -c:1,2,3)
-    $ celeryd-multi start 5 -c 3  -c:1-3 10
-    celeryd -n celeryd1.myhost -c 10
-    celeryd -n celeryd2.myhost -c 10
-    celeryd -n celeryd3.myhost -c 10
-    celeryd -n celeryd4.myhost -c 3
-    celeryd -n celeryd5.myhost -c 3
-
-    # lists also works with named workers
-    $ celeryd-multi start foo bar baz xuzzy -c 3 -c:foo,bar,baz 10
-    celeryd -n foo.myhost -c 10
-    celeryd -n bar.myhost -c 10
-    celeryd -n baz.myhost -c 10
-    celeryd -n xuzzy.myhost -c 3
-""")
-
-COMMANDS = {"start": start,
-            "names": names,
-            "expand": expand,
-            "get": get,
-            "help": help}
+    def names(self, argv, cmd):
+        p = NamespacedOptionParser(argv)
+        print("\n".join(hostname
+                        for hostname, _, _ in multi_args(p, cmd)))
 
-def usage():
-    print("Please use one of the following commands: %s" % (
-        ", ".join(COMMANDS.keys())))
+    def get(self, argv, cmd):
+        wanted = argv[0]
+        p = NamespacedOptionParser(argv[1:])
+        for name, worker, _ in multi_args(p, cmd):
+            if name == wanted:
+                print(worker)
+                return
+
+    def start(self, argv, cmd):
+        p = NamespacedOptionParser(argv)
+        print("\n".join(worker
+                        for _, worker, _ in multi_args(p, cmd)))
 
+    def expand(self, argv, cmd=None):
+        template = argv[0]
+        p = NamespacedOptionParser(argv[1:])
+        for _, _, expander in multi_args(p, cmd):
+            print(expander(template))
 
-def celeryd_multi(argv, cmd="celeryd"):
-    if len(argv) == 0:
-        usage()
-        sys.exit(0)
+    def help(self, argv, cmd=None):
+        say(EXAMPLES)
 
-    try:
-        return COMMANDS[argv[0]](argv[1:], cmd)
-    except KeyError, e:
-        print("Invalid command: %s" % argv[0])
-        usage()
-        sys.exit(1)
+    def usage(self):
+        say("Please use one of the following commands: %s" % (
+            ", ".join(self.commands.keys())))
 
 
 def main():
-    celeryd_multi(sys.argv[1:])
+    MultiTool()(sys.argv[1:])
 
 
 if __name__ == "__main__":