Changeset - 970ee88be388
[Not reviewed]
default
0 2 0
domruf - 8 years ago 2017-06-11 16:13:09
dominikruf@gmail.com
celery: let celerypylons.app be the actual app, not a factory

Change extracted by Mads Kiilerich.
2 files changed with 9 insertions and 6 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/celerypylons/__init__.py
Show inline comments
 
# -*- coding: utf-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.
 

	
 
Also, the loader depends on Pylons being configured to it can read the Celery
 
configuration out of it. To make sure that really is the case and give an early
 
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
 
warning, we check one of the mandatory settings.
 

	
 
This module must thus not be imported in global scope but must be imported on
 
demand in function scope.
 
demand in function scope after tg.config has been initialized.
 
"""
 

	
 
import os
 
import warnings
 

	
 
import celery.app
 

	
 
# 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
 

	
 
# Import (and expose) celery, thus immediately triggering use of the custom Pylons loader
 
import celery.app as app
 
# Create celery app, thus immediately triggering use of the custom Pylons loader
 
app = celery.app.app_or_default()
 

	
 
import celery.result as result
 
from celery.task import task
 
from celery.bin import worker
kallithea/lib/paster_commands/celeryd.py
Show inline comments
 
@@ -12,25 +12,25 @@ __all__ = ['Command']
 
class Command(BasePasterCommand):
 
    """Kallithea: Celery worker for asynchronous tasks"""
 

	
 
    # Starts the celery worker using configuration from a paste.deploy
 
    # configuration file.
 

	
 
    def take_action(self, args):
 
        if not kallithea.CELERY_ON:
 
            raise Exception('Please set use_celery = true in .ini config '
 
                            'file before running celeryd')
 

	
 
        from kallithea.lib import celerypylons
 
        cmd = celerypylons.worker.worker(celerypylons.app.app_or_default())
 
        cmd = celerypylons.worker.worker(celerypylons.app)
 

	
 
        celery_args = args.celery_args
 
        if '--' in celery_args:
 
            celery_args.remove('--')
 

	
 
        return cmd.run_from_argv('kallithea celery worker', celery_args)
 

	
 
    def get_parser(self, prog_name):
 
        parser = super(Command, self).get_parser(prog_name)
 

	
 
        parser.add_argument('celery_args', nargs=argparse.REMAINDER,
 
            help="Pass extra options to Celery after a '--' separator",
0 comments (0 inline, 0 general)