# HG changeset patch # User Mads Kiilerich # Date 2020-02-13 16:41:51 # Node ID 894a662b12b3ab3e972fa75bd3080d6afed9efba # Parent 173612a900efae31bb338ae8fd108658a5d4ca9e celery: refactor initialization - replace global CELERY_ON flag with CELERY_APP with the actual celery app that it indicates Prepare for fixing how 193138922d56 broke celery due to magic dependencies on initialization of global state at import time. diff --git a/kallithea/__init__.py b/kallithea/__init__.py --- a/kallithea/__init__.py +++ b/kallithea/__init__.py @@ -40,7 +40,7 @@ BACKENDS = { 'git': 'Git repository', } -CELERY_ON = False +CELERY_APP = None # set to Celery app instance if using Celery CELERY_EAGER = False CONFIG = {} diff --git a/kallithea/bin/kallithea_cli_celery.py b/kallithea/bin/kallithea_cli_celery.py --- a/kallithea/bin/kallithea_cli_celery.py +++ b/kallithea/bin/kallithea_cli_celery.py @@ -32,10 +32,9 @@ def celery_run(celery_args): by this CLI command. """ - if not kallithea.CELERY_ON: + if not kallithea.CELERY_APP: raise Exception('Please set use_celery = true in .ini config ' 'file before running this command') - app = celerypylons.make_app() - cmd = celerypylons.worker.worker(app) + cmd = celerypylons.worker.worker(kallithea.CELERY_APP) return cmd.run_from_argv(None, command='celery-run -c CONFIG_FILE --', argv=list(celery_args)) diff --git a/kallithea/config/app_cfg.py b/kallithea/config/app_cfg.py --- a/kallithea/config/app_cfg.py +++ b/kallithea/config/app_cfg.py @@ -34,6 +34,7 @@ from tg.support.converters import asbool import kallithea.lib.locale import kallithea.model.base import kallithea.model.meta +from kallithea.lib import celerypylons from kallithea.lib.middleware.https_fixup import HttpsFixup from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl from kallithea.lib.middleware.simplegit import SimpleGit @@ -158,7 +159,8 @@ def setup_configuration(app): sys.exit(1) # store some globals into kallithea - kallithea.CELERY_ON = str2bool(config.get('use_celery')) + if str2bool(config.get('use_celery')): + kallithea.CELERY_APP = celerypylons.make_app() kallithea.CELERY_EAGER = str2bool(config.get('celery.always.eager')) kallithea.CONFIG = config diff --git a/kallithea/controllers/admin/repos.py b/kallithea/controllers/admin/repos.py --- a/kallithea/controllers/admin/repos.py +++ b/kallithea/controllers/admin/repos.py @@ -181,12 +181,11 @@ class ReposController(BaseRepoController task_id = request.GET.get('task_id') if task_id and task_id not in ['None']: - from kallithea import CELERY_ON from kallithea.lib import celerypylons - if CELERY_ON: - task = celerypylons.result.AsyncResult(task_id) - if task.failed(): - raise HTTPInternalServerError(task.traceback) + if kallithea.CELERY_APP: + task_result = celerypylons.result.AsyncResult(task_id) + if task_result.failed(): + raise HTTPInternalServerError(task_result.traceback) repo = Repository.get_by_repo_name(repo_name) if repo and repo.repo_state == Repository.STATE_CREATED: diff --git a/kallithea/lib/celerylib/__init__.py b/kallithea/lib/celerylib/__init__.py --- a/kallithea/lib/celerylib/__init__.py +++ b/kallithea/lib/celerylib/__init__.py @@ -33,7 +33,7 @@ from hashlib import md5 from decorator import decorator from tg import config -from kallithea import CELERY_EAGER, CELERY_ON +import kallithea from kallithea.lib.pidlock import DaemonLock, LockHeld from kallithea.lib.utils2 import safe_bytes from kallithea.model import meta @@ -57,10 +57,10 @@ class FakeTask(object): def task(f_org): - """Wrapper of celery.task.task, running async if CELERY_ON + """Wrapper of celery.task.task, running async if CELERY_APP """ - if CELERY_ON: + if kallithea.CELERY_APP: def f_async(*args, **kwargs): log.info('executing %s task', f_org.__name__) try: @@ -128,7 +128,7 @@ def dbsession(func): ret = func(*fargs, **fkwargs) return ret finally: - if CELERY_ON and not CELERY_EAGER: + if kallithea.CELERY_APP and not kallithea.CELERY_EAGER: meta.Session.remove() return decorator(__wrapper, func) diff --git a/kallithea/lib/celerylib/tasks.py b/kallithea/lib/celerylib/tasks.py --- a/kallithea/lib/celerylib/tasks.py +++ b/kallithea/lib/celerylib/tasks.py @@ -36,7 +36,7 @@ from time import mktime from tg import config -from kallithea import CELERY_ON +import kallithea from kallithea.lib import celerylib, ext_json from kallithea.lib.helpers import person from kallithea.lib.hooks import log_create_repository @@ -218,7 +218,7 @@ def get_commits_stats(repo_name, ts_min_ lock.release() # execute another task if celery is enabled - if len(repo.revisions) > 1 and CELERY_ON and recurse_limit > 0: + if len(repo.revisions) > 1 and kallithea.CELERY_APP and recurse_limit > 0: get_commits_stats(repo_name, ts_min_y, ts_max_y, recurse_limit - 1) elif recurse_limit <= 0: log.debug('Not recursing - limit has been reached')