Private
Public Access
Make the readiness check catch fetching that is scheduled but failing (#27)
/health/ready/ proved beat and a worker were alive but not that fetching worked: every feed could be failing, or the queue could be hours behind, and it would still report ok. apps.core.health adds three checks: Celery queue depth (real Redis list length, over 1000 fails), a successful fetch within three poll intervals (30 min minimum, with a grace period for a brand-new install), and the share of active feeds failing (over half, once there are at least four). The payload also reports queue depth, active/failing feed counts, and the age of the last successful fetch. Verified against real Redis with queued Celery tasks, and the suite on PostgreSQL. docs/install.md lists each check. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f8cd1eb33e
commit
fd494a9c08
4 files changed
+252
-11
No files matched your search
+137
-4
@@ -22,8 +22,17 @@ def test_health_check_works_before_first_run_setup(client):
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def broker_queue(monkeypatch):
|
||||
"""Stands in for the Redis broker's queue length (tests have no Redis):
|
||||
set `broker_queue["depth"]` to a number, or None for "unreachable"."""
|
||||
state = {"depth": 0}
|
||||
monkeypatch.setattr("apps.core.health.queue_depth", lambda: state["depth"])
|
||||
return state
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client):
|
||||
def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client, broker_queue):
|
||||
response = client.get(reverse("core:health_ready"))
|
||||
assert response.status_code == 503
|
||||
body = response.json()
|
||||
@@ -34,22 +43,26 @@ def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client):
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_is_ok_after_poll_due_feeds_runs(client):
|
||||
def test_health_ready_is_ok_after_poll_due_feeds_runs(client, broker_queue):
|
||||
from apps.feeds.tasks import poll_due_feeds
|
||||
|
||||
poll_due_feeds()
|
||||
|
||||
response = client.get(reverse("core:health_ready"))
|
||||
assert response.status_code == 200
|
||||
assert response.json()["checks"] == {
|
||||
checks = response.json()["checks"]
|
||||
assert checks == {
|
||||
"database": True,
|
||||
"redis": True,
|
||||
"poll_heartbeat": True,
|
||||
"queue_depth": True,
|
||||
"recent_fetch_success": True,
|
||||
"feed_failures": True,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_flags_a_stale_poll_heartbeat(client):
|
||||
def test_health_ready_flags_a_stale_poll_heartbeat(client, broker_queue):
|
||||
from datetime import timedelta
|
||||
|
||||
from django.core.cache import cache
|
||||
@@ -119,3 +132,123 @@ def test_retry_feed_resets_backoff_and_requeues(client, django_user_model):
|
||||
assert response.status_code == 302
|
||||
mock_reset.assert_called_once_with(feed.id)
|
||||
mock_fetch.assert_called_once_with(feed.id)
|
||||
|
||||
|
||||
def _ready(client):
|
||||
from django.core.cache import cache
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.feeds.tasks import POLL_HEARTBEAT_CACHE_KEY
|
||||
|
||||
# Record the heartbeat directly: running poll_due_feeds here would
|
||||
# execute fetches eagerly (CELERY_TASK_ALWAYS_EAGER) and rewrite the
|
||||
# feed states each test sets up.
|
||||
cache.set(POLL_HEARTBEAT_CACHE_KEY, timezone.now())
|
||||
response = client.get(reverse("core:health_ready"))
|
||||
return response.status_code, response.json()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_flags_a_backed_up_queue(client, broker_queue):
|
||||
from .health import MAX_QUEUE_DEPTH
|
||||
|
||||
broker_queue["depth"] = MAX_QUEUE_DEPTH + 1
|
||||
|
||||
status, body = _ready(client)
|
||||
|
||||
assert status == 503
|
||||
assert body["checks"]["queue_depth"] is False
|
||||
assert body["queue_depth"] == MAX_QUEUE_DEPTH + 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_flags_an_unreachable_broker_queue(client, broker_queue):
|
||||
broker_queue["depth"] = None
|
||||
|
||||
status, body = _ready(client)
|
||||
|
||||
assert status == 503
|
||||
assert body["checks"]["queue_depth"] is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_flags_no_recent_successful_fetch(client, broker_queue):
|
||||
from datetime import timedelta
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
feed = Feed.objects.create(
|
||||
feed_url="https://a.example.com/feed.xml",
|
||||
last_fetch_status=Feed.STATUS_OK,
|
||||
last_fetched_at=timezone.now() - timedelta(days=2),
|
||||
)
|
||||
Feed.objects.filter(pk=feed.pk).update(created_at=timezone.now() - timedelta(days=30))
|
||||
|
||||
status, body = _ready(client)
|
||||
|
||||
assert status == 503
|
||||
assert body["checks"]["recent_fetch_success"] is False
|
||||
assert body["last_successful_fetch_age_seconds"] > 86400
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_is_ok_with_a_recent_successful_fetch(client, broker_queue):
|
||||
from django.utils import timezone
|
||||
|
||||
Feed.objects.create(
|
||||
feed_url="https://a.example.com/feed.xml",
|
||||
last_fetch_status=Feed.STATUS_NOT_MODIFIED,
|
||||
last_fetched_at=timezone.now(),
|
||||
)
|
||||
|
||||
status, body = _ready(client)
|
||||
|
||||
assert status == 200
|
||||
assert body["active_feeds"] == 1
|
||||
assert body["failing_feeds"] == 0
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_gives_a_brand_new_feed_time_before_it_counts_as_stale(client, broker_queue):
|
||||
Feed.objects.create(feed_url="https://new.example.com/feed.xml")
|
||||
|
||||
status, body = _ready(client)
|
||||
|
||||
assert status == 200
|
||||
assert body["checks"]["recent_fetch_success"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_health_ready_flags_most_feeds_failing_but_not_a_single_broken_one(client, broker_queue):
|
||||
from django.utils import timezone
|
||||
|
||||
now = timezone.now()
|
||||
healthy = Feed.objects.create(
|
||||
feed_url="https://ok.example.com/feed.xml",
|
||||
last_fetch_status=Feed.STATUS_OK,
|
||||
last_fetched_at=now,
|
||||
)
|
||||
broken = [
|
||||
Feed.objects.create(
|
||||
feed_url=f"https://bad{i}.example.com/feed.xml",
|
||||
last_fetch_status=Feed.STATUS_ERROR,
|
||||
fetch_error_count=3,
|
||||
last_fetched_at=now,
|
||||
)
|
||||
for i in range(2)
|
||||
]
|
||||
|
||||
status, body = _ready(client)
|
||||
assert status == 200, "2 of 3 failing is below the minimum feed count for the ratio"
|
||||
|
||||
Feed.objects.create(
|
||||
feed_url="https://bad9.example.com/feed.xml",
|
||||
last_fetch_status=Feed.STATUS_ERROR,
|
||||
fetch_error_count=1,
|
||||
last_fetched_at=now,
|
||||
)
|
||||
status, body = _ready(client)
|
||||
assert status == 503
|
||||
assert body["checks"]["feed_failures"] is False
|
||||
assert body["failing_feeds"] == 3
|
||||
assert healthy.pk and broken
|
||||
Reference in new issue
Block a user