# HG changeset patch # User Mads Kiilerich # Date 2021-01-01 18:04:16 # Node ID 01cb988e82a561d470ffd23d677d827374f5e19f # Parent e0f7da1d3c56d91a753a9e4d3402c6feb61447ee 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. 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 @@ -17,6 +17,7 @@ import click 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 @@ def celery_run(celery_args, config): 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)) 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 @@ -135,7 +135,7 @@ def setup_configuration(app): # 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