dougtabuchi avatar dougtabuchi committed c40a87b

Merge in changes from rhodecode to use celery app.

Comments (0)

Files changed (2)

celerypylons/commands.py

 import os
 import sys
 import warnings
+import paste.deploy
 from paste.script.command import Command, BadCommand
-import paste.deploy
+from celery.app import app_or_default
+from celery.bin import camqadm, celerybeat, celeryd, celeryev
 from pylons import config
-from celery.exceptions import ImproperlyConfigured
 
+__all__ = ['CeleryDaemonCommand', 'CeleryBeatCommand',
+           'CAMQPAdminCommand', 'CeleryEventCommand']
 
-__all__ = ['CeleryDaemonCommand', 'CeleryBeatCommand', 'CAMQPAdminCommand', 'CeleryEventCommand']
-
-
-class CeleryCommand(Command):
+class BasePasterCommand(Command):
     """
-    Abstract Base Class for celery commands.
+    Abstract Base Class for paster commands.
 
     The celery commands are somewhat aggressive about loading
     celery.conf, and since our module sets the `CELERY_LOADER`
         self.min_args -= 1
         self.bootstrap_config(args[0])
         self.update_parser()
-        return super(CeleryCommand, self).run(args[1:])
+        return super(BasePasterCommand, self).run(args[1:])
 
     def update_parser(self):
         """
         conf = paste.deploy.appconfig('config:' + path_to_ini_file)
         config.init_app(conf.global_conf,conf.local_conf)
 
+    
+class CeleryCommand(BasePasterCommand):
+    """Abstract class implements run methods needed for celery
+
+    Starts the celery worker that uses a paste.deploy configuration
+    file.
+    """
+
+    def update_parser(self):
+        """
+        Abstract method.  Allows for the class's parser to be updated
+        before the superclass's `run` method is called.  Necessary to
+        allow options/arguments to be passed through to the underlying
+        celery command.
+        """
+
+        cmd = self.celery_command(app_or_default())
+        for x in cmd.get_options():
+            self.parser.add_option(x)
+
+    def command(self):
+        cmd = self.celery_command(app_or_default())
+        return cmd.run(**vars(self.options))
 
 class CeleryDaemonCommand(CeleryCommand):
     """Start the celery worker
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
-
-    def update_parser(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))
+    celery_command = celeryd.WorkerCommand
 
 
 class CeleryBeatCommand(CeleryCommand):
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
+    celery_command = celerybeat.BeatCommand
 
-    def update_parser(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))
 
 class CAMQPAdminCommand(CeleryCommand):
     """CAMQP Admin
     description = "".join(__doc__.splitlines()[2:])
 
     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))
-
+    celery_command = camqadm.AMQPAdminCommand
 
 class CeleryEventCommand(CeleryCommand):
-    """Celery event commandd.
+    """Celery event command.
 
     Capture celery events.
     """
     description = "".join(__doc__.splitlines()[2:])
 
     parser = Command.standard_parser(quiet=True)
+    celery_command = celeryev.EvCommand
 
-    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))

celerypylons/loader.py

 from celery.loaders.base import BaseLoader
 from pylons import config
-import types
 
-
-to_pylons = lambda x: x.replace('_','.').lower()
+to_pylons = lambda x: x.replace('_', '.').lower()
 to_celery = lambda x: x.replace('.', '_').upper()
 
 LIST_PARAMS = """CELERY_IMPORTS ADMINS ROUTES""".split()
 
-
 class PylonsSettingsProxy(object):
     """Pylons Settings Proxy
 
         pylons_key = to_pylons(key)
         try:
             value = config[pylons_key]
-            if key in LIST_PARAMS:
-                return value.split()
-            return value
+            if key in LIST_PARAMS:return value.split()
+            return self.type_converter(value)
         except KeyError:
             raise AttributeError(pylons_key)
 
+    def get(self, key):
+        try:
+            return self.__getattr__(key)
+        except AttributeError:
+            return None
+
+    def __getitem__(self, key):
+        try:
+            return self.__getattr__(key)
+        except AttributeError:
+            raise KeyError()
+
     def __setattr__(self, key, value):
         pylons_key = to_pylons(key)
         config[pylons_key] = value
 
+    def __setitem__(self, key, value):
+        self.__setattr__(key, value)
+
+    def type_converter(self, value):
+        #cast to int
+        if value.isdigit():
+            return int(value)
+
+        #cast to bool
+        if value.lower() in ['true', 'false']:
+            return value.lower() == 'true'
+        return value
 
 class PylonsLoader(BaseLoader):
     """Pylons celery loader
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.