您可以创建一个DISABLE_CHECKS
设置并从检查功能本身中强制跳过检查。我注意到即使您设置SILENCED_SYSTEM_CHECKS
了settings.py
,某些manage.py
命令仍会运行检查(例如迁移)。这是我使用的:
import logging
from django.conf import settings
from django.core.checks import Error
from django.db import connections
from django.core.cache import caches
def check_cache_connectivity(app_configs, **kwargs):
"""
Check cache
:param app_configs:
:param kwargs:
:return:
"""
errors = []
# Short circuit here, checks still ran by manage.py cmds regardless of SILENCED_SYSTEM_CHECKS
if settings.DISABLE_CHECKS:
return errors
cache_settings = settings.CACHES.keys()
for cur_cache in cache_settings:
try:
key = 'check_cache_connectivity_{}'.format(cur_cache)
caches[cur_cache].set(key, 'connectivity_ok', 30)
value = caches[cur_cache].get(key)
print("Cache '{}' connection ok, key '{}', value '{}'".format(cur_cache, key, value))
except Exception as e:
msg = "ERROR: Cache {} looks to be down. {}".format(cur_cache, e)
print(msg)
logging.exception(msg)
errors.append(
Error(
msg,
hint="Unable to connect to cache {}, set as {}. {}"
"".format(cur_cache, settings.CACHES[cur_cache], e),
obj='CACHES.{}'.format(cur_cache),
id='content_services.E002',
)
)
return errors
我在构建环境中使用它,如果不是所有自定义检查,我最希望忽略。希望能帮助到你!