1. Ian Schenck
  2. celery-pylons

Source

celery-pylons / celerypylons / commands.py

Diff from to

File celerypylons/commands.py

 __all__ = ['CeleryDaemonCommand', 'CeleryBeatCommand', 'CAMQPAdminCommand', 'CeleryEventCommand']
 
 
-## @@ We have to do some rather odd bootstrapping in order to get
-## celery to load correctly.
-def bootstrap_config():
-    try:
-        path_to_ini_file = os.path.realpath(sys.argv[2])
-        conf = paste.deploy.appconfig('config:' + path_to_ini_file)
-        config.init_app(conf.global_conf,conf.local_conf)
-    except ValueError:
-        ## @@ If the user just wants to see usage or docs, don't fail on configuration
-        pass
-    except IndexError:
-        ## @@ No configuration was passed in
-        pass
-bootstrap_config()
-
-## @@ We can now import celeryd since the configuration has been set.
-try:
-    ## @@ If paster is run without arguments this file is imported,
-    ## yet there is no config file passed in which causes these
-    ## imports to fail due to a configuration error.
-    from celery.bin import celeryd
-    from celery.bin import celerybeat
-    from celery.bin import camqadm
-    from celery.bin import celeryev
-except ImproperlyConfigured:
-    import dummy as celeryd
-    import dummy as celerybeat
-    import dummy as camqadm
-    import dummy as celeryev
-
-
 class CeleryCommand(Command):
     min_args = 1
     min_args_error = "Please provide a paster config file as an argument."
     takes_config_file = 1
     requires_config_file = True
 
+    def bootstrap_config(self):
+        path_to_ini_file = os.path.realpath(self.args[0])
+        conf = paste.deploy.appconfig('config:' + path_to_ini_file)
+        config.init_app(conf.global_conf,conf.local_conf)
+
 
 class CeleryDaemonCommand(CeleryCommand):
     """Start the celery worker
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
-    for x in celeryd.OPTION_LIST:
-        parser.add_option(x)
         
     def command(self):
-        options = celeryd.parse_options(sys.argv[3:])
+        self.bootstrap_config()
+        from celery.bin import celeryd
+        for x in celeryd.OPTION_LIST:
+            self.parser.add_option(x)
+        options = celeryd.parse_options(sys.argv[1:])
         return celeryd.run_worker(**vars(options))
 
 
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
-    for x in celerybeat.OPTION_LIST:
-        parser.add_option(x)
         
     def command(self):
-        options = celerybeat.parse_options(sys.argv[3:])
+        self.bootstrap_config()
+        from celery.bin import celerybeat
+        for x in celerybeat.OPTION_LIST:
+            self.parser.add_option(x)
+        options = celerybeat.parse_options(self.args[1:])
         return celerybeat.run_celerybeat(**vars(options))
 
 
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
-    for x in camqadm.OPTION_LIST:
-        parser.add_option(x)
         
     def command(self):
-        options, values = camqadm.parse_options(sys.argv[3:])
+        self.bootstrap_config()
+        from celery.bin import camqadm
+        for x in camqadm.OPTION_LIST:
+            self.parser.add_option(x)
+        options, values = camqadm.parse_options(self.args[1:])
         return camqadm.camqadm(*values, **vars(options))
 
     
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
-    for x in celeryev.OPTION_LIST:
-        parser.add_option(x)
         
     def command(self):
-        options = celeryev.parse_options(sys.argv[3:])
+        self.bootstrap_config()
+        from celery.bin import celeryev
+        for x in celeryev.OPTION_LIST:
+            self.parser.add_option(x)
+        options = celeryev.parse_options(self.args[1:])
         return celeryev.run_celeryev(**vars(options))