Просмотр исходного кода

Fix celery config deprecation warnings

Peter Justin 4 лет назад
Родитель
Сommit
c6bc2bef3a

+ 1 - 2
celery_worker.py

@@ -15,5 +15,4 @@ import os
 from flaskbb.app import create_app
 from flaskbb.extensions import celery  # noqa
 
-_basepath = os.path.dirname(os.path.abspath(__file__))
-app = create_app(config=os.path.join(_basepath, 'flaskbb.cfg'))
+app = create_app()

+ 19 - 2
flaskbb/app.py

@@ -128,6 +128,24 @@ def configure_app(app, config):
     # them on the config object
     app_config_from_env(app, prefix="FLASKBB_")
 
+    # Migrate Celery 4.x config to Celery 6.x
+    old_celery_config = app.config.get_namespace('CELERY_')
+    celery_config = {}
+    for key, value in old_celery_config.items():
+        config_key = f"CELERY_{key.upper()}"
+        # config is the new format
+        if key != "config":
+            celery_config[key] = value
+            try:
+                del app.config[config_key]
+            except KeyError:
+                pass
+
+    # merge the new config with the old one
+    new_celery_config = app.config.get("CELERY_CONFIG")
+    new_celery_config.update(celery_config)
+    app.config.update({"CELERY_CONFIG": new_celery_config})
+
     # Setting up logging as early as possible
     configure_logging(app)
 
@@ -168,8 +186,7 @@ def configure_app(app, config):
 
 def configure_celery_app(app, celery):
     """Configures the celery app."""
-    app.config.update({"BROKER_URL": app.config["CELERY_BROKER_URL"]})
-    celery.conf.update(app.config)
+    celery.conf.update(app.config.get("CELERY_CONFIG"))
 
     TaskBase = celery.Task
 

+ 5 - 2
flaskbb/configs/config.cfg.template

@@ -107,8 +107,11 @@ REDIS_DATABASE = 0
 
 # Celery
 # ------------------------------
-CELERY_BROKER_URL = "{{ redis_uri }}"
-CELERY_RESULT_BACKEND = "{{ redis_uri }}"
+CELERY_CONFIG = {
+    "broker_url": "{{ redis_uri }}",
+    "result_backend": "{{ redis_uri }}",
+    "broker_transport_options": {"max_retries": 1},
+}
 
 
 # Rate Limiting

+ 5 - 4
flaskbb/configs/default.py

@@ -265,10 +265,11 @@ class DefaultConfig(object):
     REDIS_DATABASE = 0
 
     # Celery
-    CELERY_BROKER_URL = 'redis://localhost:6379'
-    CELERY_RESULT_BACKEND = 'redis://localhost:6379'
-    BROKER_TRANSPORT_OPTIONS = {'max_retries': 1}  # necessary as there's no default
-
+    CELERY_CONFIG = {
+        "broker_url": 'redis://localhost:6379',
+        "result_backend": 'redis://localhost:6379',
+        "broker_transport_options": {'max_retries': 1},
+    }
 
     # FlaskBB Settings
     # ------------------------------ #

+ 6 - 4
flaskbb/configs/testing.py

@@ -28,10 +28,12 @@ class TestingConfig(DefaultConfig):
     # Use the in-memory storage
     WHOOSHEE_MEMORY_STORAGE = True
 
-    CELERY_ALWAYS_EAGER = True
-    CELERY_RESULT_BACKEND = "cache"
-    CELERY_CACHE_BACKEND = "memory"
-    CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
+    CELERY_CONFIG = {
+        "always_eager": True,
+        "eager_propagates_exceptions": True,
+        "result_backend": "cache",
+        "cache_backend": "memory",
+    }
 
     LOG_DEFAULT_CONF = {
         'version': 1,

+ 3 - 3
requirements.txt

@@ -4,8 +4,8 @@ attrs==20.3.0
 Babel==2.9.0
 billiard==3.6.3.0
 blinker==1.4
-celery==5.0.2
-certifi==2020.11.8
+celery==5.0.3
+certifi==2020.12.5
 chardet==3.0.4
 click==7.1.2
 click-log==0.3.2
@@ -40,7 +40,7 @@ mistune==0.8.4
 olefile==0.46
 Pillow==8.0.1
 pluggy==0.13.1
-Pygments==2.7.2
+Pygments==2.7.3
 python-dateutil==2.8.1
 python-editor==1.0.4
 pytz==2020.4