Coverage for server / utilities / redis_url.py: 100%
12 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-04 09:33 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-04 09:33 +0000
1"""Resolve ``REDIS_URL``, and make a fallback to localhost audible.
3A SILENT fallback is how the platform lost a response cache for three months and
4left two Celery workers consuming from a local broker from March to September: an
5unset ``REDIS_URL`` connected SUCCESSFULLY to a local Redis, so every health check
6passed while the service was pointed at the wrong thing.
8Every host is now consolidated onto ``redis.mathmattersqa.com`` and no server runs
9a local Redis, so reaching this fallback on a deployed host means "misconfigured"
10-- never "local dev".
12Deliberately a warning and not a hard failure: local development legitimately uses
13localhost, and turning a missing variable into an import-time crash would trade a
14quiet problem for a loud outage.
16Developer: Allan Ninal
17"""
19import logging
20import os
22logger = logging.getLogger(__name__)
24_warned = False
27def redis_url_or_default(default: str) -> str:
28 """Return ``REDIS_URL``; warn once if falling back to ``default``."""
29 global _warned
31 url = os.getenv("REDIS_URL")
32 if url:
33 return url
35 if not _warned:
36 _warned = True
37 logger.warning(
38 "REDIS_URL is not set; falling back to %s. That is correct for LOCAL "
39 "DEVELOPMENT only -- no server runs a local Redis, so on a deployed host "
40 "this means the service is misconfigured and is NOT talking to the shared "
41 "instance.",
42 default,
43 )
44 return default