Skip to content

Commit 75fd4d2

Browse files
committed
Added a production-mode check to verify that the cache is functioning as expected.
- Legacy-Id: 11471
1 parent 19fb344 commit 75fd4d2

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

ietf/checks.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import time
23

34
from django.conf import settings
45
from django.core import checks
@@ -52,14 +53,14 @@ def check_doc_email_aliases_exists(app_configs, **kwargs):
5253
try:
5354
ok = check_doc_email_aliases()
5455
if not ok:
55-
errors.append(checks.Critical(
56+
errors.append(checks.Error(
5657
"Found no aliases in the document email aliases file\n'%s'."%settings.DRAFT_ALIASES_PATH,
5758
hint="Please run ietf/bin/generate-draft-aliases to generate them.",
5859
obj=None,
5960
id="datatracker.E0004",
6061
))
6162
except IOError as e:
62-
errors.append(checks.Critical(
63+
errors.append(checks.Error(
6364
"Could not read document email aliases:\n %s" % e,
6465
hint="Please run ietf/bin/generate-draft-aliases to generate them.",
6566
obj=None,
@@ -176,4 +177,35 @@ def check_proceedings_directories(app_configs, **kwargs):
176177
))
177178
return errors
178179

180+
@checks.register('cache')
181+
def check_cache(app_configs, **kwargs):
182+
errors = []
183+
if settings.SERVER_MODE == 'production':
184+
from django.core.cache import cache
185+
def cache_error(msg, errnum):
186+
return checks.Warning(
187+
( "A cache test failed with the message:\n '%s'.\n"
188+
"This indicates that the cache is unavailable or not working as expected.\n"
189+
"It will impact performance, but isn't fatal. The default cache is:\n"
190+
" CACHES['default']['BACKEND'] = %s\n") % (
191+
msg,
192+
settings.CACHES["default"]["BACKEND"],
193+
),
194+
hint = "Please check that the configured cache backend is available.\n",
195+
id = "datatracker.%s" % errnum,
196+
)
197+
key = "ietf:checks:check_cache"
198+
val = os.urandom(32)
199+
wait = 1
200+
cache.set(key, val, wait)
201+
if not cache.get(key) == val:
202+
errors.append(cache_error("Could not get value from cache", "E0014"))
203+
time.sleep(wait+1)
204+
# should have timed out
205+
if cache.get(key) == val:
206+
errors.append(cache_error("Cache value didn't time out", "E0015"))
207+
cache.set(key, val, settings.SESSION_COOKIE_AGE)
208+
if not cache.get(key) == val:
209+
errors.append(cache_error("Cache didn't accept session cookie age", "E0016"))
210+
return errors
179211

0 commit comments

Comments
 (0)