Skip to content

Commit

Permalink
Warn when deprecated settings are used (#6353)
Browse files Browse the repository at this point in the history
* Warn when deprecated settings are used.

* Mention deprecation in docs.

* Refer to the right place in the documentation.
  • Loading branch information
thedrow committed Sep 23, 2020
1 parent 5a919c0 commit b7ddd8a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
5 changes: 5 additions & 0 deletions celery/app/log.py
Expand Up @@ -9,12 +9,14 @@
import logging
import os
import sys
import warnings
from logging.handlers import WatchedFileHandler

from kombu.utils.encoding import set_default_encoding_file

from celery import signals
from celery._state import get_current_task
from celery.exceptions import CDeprecationWarning, CPendingDeprecationWarning
from celery.local import class_property
from celery.platforms import isatty
from celery.utils.log import (ColorFormatter, LoggingProxy, get_logger,
Expand Down Expand Up @@ -70,6 +72,9 @@ def setup(self, loglevel=None, logfile=None, redirect_stdouts=False,
CELERY_LOG_LEVEL=str(loglevel) if loglevel else '',
CELERY_LOG_FILE=str(logfile) if logfile else '',
)
warnings.filterwarnings('always', category=CDeprecationWarning)
warnings.filterwarnings('always', category=CPendingDeprecationWarning)
logging.captureWarnings(True)
return handled

def redirect_stdouts(self, loglevel=None, name='celery.redirected'):
Expand Down
20 changes: 20 additions & 0 deletions celery/app/utils.py
Expand Up @@ -77,6 +77,11 @@ class Settings(ConfigurationView):
"""

def __init__(self, *args, deprecated_settings=None, **kwargs):
super().__init__(*args, **kwargs)

self.deprecated_settings = deprecated_settings

@property
def broker_read_url(self):
return (
Expand Down Expand Up @@ -190,6 +195,20 @@ def humanize(self, with_defaults=False, censored=True):
f'{key}: {pretty(value, width=50)}'
for key, value in self.table(with_defaults, censored).items())

def maybe_warn_deprecated_settings(self):
# TODO: Remove this method in Celery 6.0
if self.deprecated_settings:
from celery.utils import deprecated
from celery.app.defaults import _TO_NEW_KEY
for setting in self.deprecated_settings:
deprecated.warn(description=f'The {setting!r} setting',
removal='6.0.0',
alternative=f'Use the {_TO_NEW_KEY[setting]} instead')

return True

return False


def _new_key_to_old(key, convert=_TO_OLD_KEY.get):
return convert(key, key)
Expand Down Expand Up @@ -263,6 +282,7 @@ def detect_settings(conf, preconf=None, ignore_keys=None, prefix=None,
return Settings(
preconf, [conf, defaults],
(_old_key_to_new, _new_key_to_old),
deprecated_settings=is_in_old,
prefix=prefix,
)

Expand Down
8 changes: 8 additions & 0 deletions celery/apps/worker.py
Expand Up @@ -140,6 +140,14 @@ def on_start(self):
if not self._custom_logging and self.redirect_stdouts:
app.log.redirect_stdouts(self.redirect_stdouts_level)

# TODO: Remove the following code in Celery 6.0
if app.conf.maybe_warn_deprecated_settings():
logger.warning(
"Please run `celery upgrade settings path/to/settings.py` "
"to avoid these warnings and to allow a smoother upgrade "
"to Celery 6.0."
)

def emit_banner(self):
# Dump configuration to screen so we have some basic information
# for when users sends bug reports.
Expand Down
12 changes: 7 additions & 5 deletions docs/userguide/configuration.rst
Expand Up @@ -47,12 +47,14 @@ names, are the renaming of some prefixes, like ``celery_beat_`` to ``beat_``,
``celeryd_`` to ``worker_``, and most of the top level ``celery_`` settings
have been moved into a new ``task_`` prefix.

.. note::
.. warning::

Celery will still be able to read old configuration files until Celery 6.0.
Afterwards, support for the old configuration files will be removed.
We provide the ``celery upgrade`` command that should handle
plenty of cases (including :ref:`Django <latentcall-django-admonition>`).

Celery will still be able to read old configuration files, so
there's no rush in moving to the new settings format. Furthermore,
we provide the ``celery upgrade`` command that should handle plenty
of cases (including :ref:`Django <latentcall-django-admonition>`).
Please migrate to the new configuration scheme as soon as possible.


========================================== ==============================================
Expand Down
2 changes: 1 addition & 1 deletion docs/whatsnew-5.0.rst
Expand Up @@ -116,7 +116,7 @@ please do so now.
We elected to extend the deprecation period until 6.0 since
we did not loudly warn about using these deprecated settings.

Please refer to the :ref:`migration guide <v400-upgrade-settings>` for instructions.
Please refer to the :ref:`migration guide <conf-old-settings-map>` for instructions.

Step 3: Read the important notes in this document
-------------------------------------------------
Expand Down

0 comments on commit b7ddd8a

Please sign in to comment.