changeset 8829:01cb988e82a5

celery: celery-run should only initialize app and sqlalchemy after workers have been forked If app and SqlAlchemy were initialized before launching celery, the forked workers would inherit the database connection ... and that doesn't work. This could be handled by disposing the engine after forking the worker or before each task ... but it remains unnecessary and wrong to initialize the engine early when it isn't used, and then fork it.
author Mads Kiilerich <mads@kiilerich.com>
date Fri, 01 Jan 2021 18:04:16 +0100
parents e0f7da1d3c56
children 853717af31d3
files kallithea/bin/kallithea_cli_celery.py kallithea/config/app_cfg.py
diffstat 2 files changed, 5 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/bin/kallithea_cli_celery.py	Sat Nov 07 18:49:57 2020 +0100
+++ b/kallithea/bin/kallithea_cli_celery.py	Fri Jan 01 18:04:16 2021 +0100
@@ -17,6 +17,7 @@
 
 import kallithea
 import kallithea.bin.kallithea_cli_base as cli_base
+from kallithea.lib import celery_app
 from kallithea.lib.utils2 import asbool
 
 
@@ -37,8 +38,9 @@
         raise Exception('Please set use_celery = true in .ini config '
                         'file before running this command')
 
-    # do as config_file_initialize_app
-    kallithea.config.application.make_app(config.global_conf, **config.local_conf)
+    kallithea.CELERY_APP.config_from_object(celery_app.make_celery_config(config))
+
+    kallithea.CELERY_APP.loader.on_worker_process_init = lambda: kallithea.config.application.make_app(config.global_conf, **config.local_conf)
 
     cmd = celery.bin.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	Sat Nov 07 18:49:57 2020 +0100
+++ b/kallithea/config/app_cfg.py	Fri Jan 01 18:04:16 2021 +0100
@@ -135,7 +135,7 @@
     # store some globals into kallithea
     kallithea.DEFAULT_USER_ID = db.User.get_default_user().user_id
 
-    if asbool(config.get('use_celery')):
+    if asbool(config.get('use_celery')) and not kallithea.CELERY_APP.finalized:
         kallithea.CELERY_APP.config_from_object(celery_app.make_celery_config(config))
     kallithea.CONFIG = config