diff rhodecode/lib/celerypylons/loader.py @ 776:f6c613fba757 beta

Celery is configured by the .ini files and run from paster now removed celeryconfig, added homebrew celery-pylons, added paster celeryd command, fixed tasks to use pylons configs, sqlalchemy sessions
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 27 Nov 2010 01:27:24 +0100
parents
children 3a7f5b1a19dd
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rhodecode/lib/celerypylons/loader.py	Sat Nov 27 01:27:24 2010 +0100
@@ -0,0 +1,55 @@
+from celery.loaders.base import BaseLoader
+from pylons import config
+
+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
+
+    Proxies settings from pylons.config
+
+    """
+    def __getattr__(self, key):
+        pylons_key = to_pylons(key)
+        try:
+            value = config[pylons_key]
+            if key in LIST_PARAMS: return value.split()
+            return self.type_converter(value)
+        except KeyError:
+            raise AttributeError(pylons_key)
+
+    def __setattr__(self, key, value):
+        pylons_key = to_pylons(key)
+        config[pylons_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
+
+    Maps the celery config onto pylons.config
+
+    """
+    def read_configuration(self):
+        self.configured = True
+        return PylonsSettingsProxy()
+
+    def on_worker_init(self):
+        """
+        Import task modules.
+        """
+        self.import_default_modules()