changeset 6802:b9853a3cc254

celery: simplify internal configuration and app creation We used celery.app.app_or_default() which creates a "global fallback app instance" which relies on the CELERY_LOADER environment variable to load the configuration. That worked but was messy. Instead, do something more like described in http://docs.celeryproject.org/en/3.1/userguide/application.html where the app is a celery.Celery() instance and configuration is loaded explicitly by its config_from_object method. Using config_from_object we don't need explicit invocation of import_default_modules and can take pass PylonsSettingsProxy directly, leaving PylonsLoader unused and removed. Modified by Mads Kiilerich.
author domruf <dominikruf@gmail.com>
date Sun, 11 Jun 2017 16:13:09 +0200
parents 970ee88be388
children 3fb0ce6de10d
files kallithea/lib/celerypylons/__init__.py kallithea/lib/celerypylons/loader.py
diffstat 2 files changed, 8 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/celerypylons/__init__.py	Sun Jun 11 16:13:09 2017 +0200
+++ b/kallithea/lib/celerypylons/__init__.py	Sun Jun 11 16:13:09 2017 +0200
@@ -3,10 +3,8 @@
 """
 Kallithea wrapper of Celery
 
-The Celery configuration is in the normal Pylons ini file. We thus have to set
-the `CELERY_LOADER` environment variable to point at a custom "loader" that can
-read it. That environment variable must be set *before* importing celery. To
-ensure that, we wrap celery in this module.
+The Celery configuration is in the ini file. To read the settings and translate
+to a Celery format we use PylonsSettingsProxy.
 
 We read the configuration from tg.config, thus it must be initialized before
 loading this module. To make sure that really is the case and give an early
@@ -19,20 +17,17 @@
 import os
 import warnings
 
-import celery.app
+import celery
+
+from kallithea.lib.celerypylons.loader import PylonsSettingsProxy
 
 # Verify Pylons configuration has been loaded
 from tg import config
 assert config['celery.imports'] == 'kallithea.lib.celerylib.tasks', 'Kallithea Celery configuration has not been loaded'
 
-# Prepare environment to point at Kallithea Pylons loader
-CELERYPYLONS_LOADER = 'kallithea.lib.celerypylons.loader.PylonsLoader'
-if os.environ.get('CELERY_LOADER', CELERYPYLONS_LOADER) != CELERYPYLONS_LOADER:
-    warnings.warn("'CELERY_LOADER' environment variable will be overridden by celery-pylons.")
-os.environ['CELERY_LOADER'] = CELERYPYLONS_LOADER
-
-# Create celery app, thus immediately triggering use of the custom Pylons loader
-app = celery.app.app_or_default()
+# Create celery app from the TurboGears configuration file
+app = celery.Celery()
+app.config_from_object(PylonsSettingsProxy())
 
 import celery.result as result
 from celery.task import task
--- a/kallithea/lib/celerypylons/loader.py	Sun Jun 11 16:13:09 2017 +0200
+++ b/kallithea/lib/celerypylons/loader.py	Sun Jun 11 16:13:09 2017 +0200
@@ -1,6 +1,5 @@
 # -*- coding: utf-8 -*-
 
-from celery.loaders.base import BaseLoader
 from tg import config
 
 # TODO: drop this mangling and just use a separate celery config section
@@ -59,19 +58,3 @@
         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()