Commits

Stefan Wehrmeyer  committed cb5c59f

Use celery's Command classes for all commands. First argument must be paster config. Pylons will be init_app. If a # is present in the first argument the part behind # will be imported and called instead of init_app, e.g. development.ini#wdmmg.config.environment.load_environment the load_environment function will be imported and called.

  • Participants
  • Parent commits 7990a5d

Comments (0)

Files changed (1)

File celerypylons/commands.py

 import os
 import sys
 import warnings
+
 from paste.script.command import Command, BadCommand
 import paste.deploy
 from pylons import config
         # Decrement because we're going to lob off the first argument.
         # @@ This is hacky
         self.min_args -= 1
+        self.args = args
         self.bootstrap_config(args[0])
-        self.update_parser()
-        return super(CeleryCommand, self).run(args[1:])
+        self.set_worker()
+        return super(CeleryCommand, self).run(args)
+        
+    def command(self):
+        return self.worker.execute_from_commandline(self.args)
 
-    def update_parser(self):
+    def set_worker(self):
         """
         Abstract method.  Allows for the class's parser to be updated
         before the superclass's `run` method is called.  Necessary to
         """
         Loads the pylons configuration.
         """
-        path_to_ini_file = os.path.realpath(conf)
+        parts = conf.split("#")
+        path_to_ini_file = os.path.realpath(parts[0])
         conf = paste.deploy.appconfig('config:' + path_to_ini_file)
-        config.init_app(conf.global_conf,conf.local_conf)
+        if len(parts) > 1:
+            module_parts = parts[1].split(".")
+            module = __import__(".".join(module_parts[:-1]), fromlist=".".join(module_parts[:-2]))
+            getattr(module, module_parts[-1])(conf.global_conf,conf.local_conf)
+        else:
+            config.init_app(conf.global_conf,conf.local_conf)
 
 
 class CeleryDaemonCommand(CeleryCommand):
 
     parser = Command.standard_parser(quiet=True)
 
-    def update_parser(self):
+    def set_worker(self):
         from celery.bin.celeryd import WorkerCommand
-        w = WorkerCommand()
-        for x in w.get_options():
-            self.parser.add_option(x)
-
-    def command(self):
-        from celery.apps import worker
-        return worker.run_worker(**vars(self.options))
-
+        self.worker = WorkerCommand()
 
 class CeleryBeatCommand(CeleryCommand):
     """Start the celery beat server
 
     parser = Command.standard_parser(quiet=True)
 
-    def update_parser(self):
+    def set_worker(self):
         from celery.bin.celerybeat import BeatCommand
-        beat = BeatCommand()
-        for x in beat.get_options():
-            self.parser.add_option(x)
-
-    def command(self):
-        from celery.apps import beat
-        return beat.run_celerybeat(**vars(self.options))
+        self.worker = BeatCommand()
 
 class CAMQPAdminCommand(CeleryCommand):
     """CAMQP Admin
 
     parser = Command.standard_parser(quiet=True)
 
-    def update_parser(self):
-        from celery.bin import camqadm
-        for x in camqadm.OPTION_LIST:
-            self.parser.add_option(x)
-
-    def command(self):
-        from celery.bin import camqadm
-        return camqadm.camqadm(*self.args, **vars(self.options))
-
+    def set_worker(self):
+        from celery.bin.camqadm import AMQPAdminCommand
+        self.worker = AMQPAdminCommand()
 
 class CeleryEventCommand(CeleryCommand):
     """Celery event commandd.
 
     parser = Command.standard_parser(quiet=True)
 
-    def update_parser(self):
-        from celery.bin import celeryev
-        for x in celeryev.OPTION_LIST:
-            self.parser.add_option(x)
-
-    def command(self):
-        from celery.bin import celeryev
-        return celeryev.run_celeryev(**vars(self.options))
+    def set_worker(self):
+        from celery.bin.celeryev import EvCommand
+        self.worker = EvCommand()