changeset 8242:894a662b12b3

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.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 13 Feb 2020 16:41:51 +0100
parents 173612a900ef
children 3b1b440b5082
files kallithea/__init__.py kallithea/bin/kallithea_cli_celery.py kallithea/config/app_cfg.py kallithea/controllers/admin/repos.py kallithea/lib/celerylib/__init__.py kallithea/lib/celerylib/tasks.py
diffstat 6 files changed, 16 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/__init__.py	Wed Feb 12 14:37:15 2020 +0100
+++ b/kallithea/__init__.py	Thu Feb 13 16:41:51 2020 +0100
@@ -40,7 +40,7 @@
     'git': 'Git repository',
 }
 
-CELERY_ON = False
+CELERY_APP = None  # set to Celery app instance if using Celery
 CELERY_EAGER = False
 
 CONFIG = {}
--- a/kallithea/bin/kallithea_cli_celery.py	Wed Feb 12 14:37:15 2020 +0100
+++ b/kallithea/bin/kallithea_cli_celery.py	Thu Feb 13 16:41:51 2020 +0100
@@ -32,10 +32,9 @@
     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))
--- a/kallithea/config/app_cfg.py	Wed Feb 12 14:37:15 2020 +0100
+++ b/kallithea/config/app_cfg.py	Thu Feb 13 16:41:51 2020 +0100
@@ -34,6 +34,7 @@
 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 @@
             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
 
--- a/kallithea/controllers/admin/repos.py	Wed Feb 12 14:37:15 2020 +0100
+++ b/kallithea/controllers/admin/repos.py	Thu Feb 13 16:41:51 2020 +0100
@@ -181,12 +181,11 @@
         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:
--- a/kallithea/lib/celerylib/__init__.py	Wed Feb 12 14:37:15 2020 +0100
+++ b/kallithea/lib/celerylib/__init__.py	Thu Feb 13 16:41:51 2020 +0100
@@ -33,7 +33,7 @@
 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 @@
 
 
 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 @@
             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)
--- a/kallithea/lib/celerylib/tasks.py	Wed Feb 12 14:37:15 2020 +0100
+++ b/kallithea/lib/celerylib/tasks.py	Thu Feb 13 16:41:51 2020 +0100
@@ -36,7 +36,7 @@
 
 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 @@
         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')