From 5807217f00803a2829c13657523e7c3b33b8cee5 Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Fri, 28 Aug 2026 01:21:06 +0200 Subject: [PATCH] fix(alerts): proxy watcher says what is affected, and needs two misses 'paid proxy DOWN: YouTube-URL jobs will keep failing' fired on a single ReadTimeout while the three static proxies were fine, and read as an outage. Severity now follows the actual impact: fallback down with the statics up is informational (no impact), statics down is a cost warning (jobs run on the per-GB proxy), both down is the red one. A route has to miss two probes 30 min apart before anyone is paged. --- cloud/alerts.py | 48 ++++++++++++++++++++------- tests/test_proxy_watch_alerts.py | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 tests/test_proxy_watch_alerts.py diff --git a/cloud/alerts.py b/cloud/alerts.py index 3535f06..293bcb5 100644 --- a/cloud/alerts.py +++ b/cloud/alerts.py @@ -178,8 +178,10 @@ _PROXY_RENOTIFY = 7200 # keep nagging every 2 h while it stays down # with it), which would make an HTTPS probe cry wolf. An exhausted balance # rejects the request before forwarding, so HTTP still detects the 407. _PROXY_PROBE_URL = "http://www.google.com/generate_204" +_PROXY_STRIKES = 2 # consecutive failed probes before alerting _watch_down = {} # target name -> down-since epoch _watch_nag = {} # target name -> last-nag epoch +_watch_strikes = {} # target name -> consecutive failed probes _PAID_TARGET = "paid proxy" _STATIC_TARGET = "static proxy pool" @@ -211,10 +213,32 @@ async def _probe_one(proxy): return False, f"{type(e).__name__}: {e}" +def _watch_severity(name): + """(emoji, headline, impact) for a route being down, judged by what is + actually affected. A dead fallback while the primary answers is a lost + safety net, not an outage: the first version of this alert said "jobs + will keep failing" for that case and read as the platform being down + (27-aug-2026, a single ReadTimeout on the paid proxy).""" + static_down = bool(_watch_down.get(_STATIC_TARGET)) + paid_down = bool(_watch_down.get(_PAID_TARGET)) + other_configured = len(_watch_targets()) > 1 + if not other_configured: + return "🔴", f"{name} DOWN", "It is the only YouTube route: YouTube-URL jobs are failing until it is fixed." + if static_down and paid_down: + return "🔴", "ALL YouTube proxies DOWN", "Static pool and paid proxy both fail: YouTube-URL jobs are failing. Direct file URLs are unaffected." + if name == _STATIC_TARGET: + return "🟠", "static proxy pool down, running on the paid proxy", ("YouTube jobs still work through the PER-GB paid proxy, so every download costs money until the statics are back.") + return "â„šī¸", "paid proxy (fallback) not answering", ("No impact right now: the static pool is UP and carries all YouTube jobs. You only lose the safety net if the statics fail too.") + + async def _watch_update(name, ok, detail): - """Shared incident state machine: alert on down, nag, confirm recovery.""" + """Shared incident state machine: alert on down, nag, confirm recovery. + + A route has to fail ``_PROXY_STRIKES`` probes in a row (30 min apart) + before anyone is paged: one ReadTimeout on a per-GB proxy is routine.""" now = time.time() if ok: + _watch_strikes[name] = 0 if _watch_down.get(name): mins = int((now - _watch_down[name]) / 60) await send_admin_alert( @@ -224,26 +248,26 @@ async def _watch_update(name, ok, detail): _watch_down[name] = None _watch_nag[name] = 0.0 return - hint = ("YouTube-URL jobs are falling back to the PER-GB paid proxy — " - "ingest still works but every download now costs money." - if name == _STATIC_TARGET else - "YouTube-URL jobs will keep failing on this route until it's " - "topped up / fixed.") + _watch_strikes[name] = _watch_strikes.get(name, 0) + 1 if not _watch_down.get(name): + if _watch_strikes[name] < _PROXY_STRIKES: + return # one miss: wait for the next probe before saying anything _watch_down[name] = now _watch_nag[name] = now + icon, headline, impact = _watch_severity(name) await send_admin_alert( - f"🔴 {name} DOWN", - f"The {name} probe failed. {hint}\n" - "This alert repeats every 2 h until it answers again.\n\n" - f"Probe error: {detail[:400]}", + f"{icon} {headline}", + f"{impact}\n\nThe {name} failed {_PROXY_STRIKES} probes in a row " + f"({_PROXY_PROBE_INTERVAL // 60} min apart). This repeats every 2 h " + f"until it answers again.\n\nProbe error: {detail[:400]}", ) elif now - _watch_nag[name] >= _PROXY_RENOTIFY: _watch_nag[name] = now hours = (now - _watch_down[name]) / 3600 + icon, headline, impact = _watch_severity(name) await send_admin_alert( - f"🔴 {name} STILL down ({hours:.1f} h)", - f"{hint}\n\nProbe error: {detail[:400]}", + f"{icon} {headline} ({hours:.1f} h)", + f"{impact}\n\nProbe error: {detail[:400]}", ) diff --git a/tests/test_proxy_watch_alerts.py b/tests/test_proxy_watch_alerts.py new file mode 100644 index 0000000..0d5abc6 --- /dev/null +++ b/tests/test_proxy_watch_alerts.py @@ -0,0 +1,56 @@ +"""The proxy watcher must say what is actually affected, and not page on one miss.""" +import asyncio +import pytest + +alerts = pytest.importorskip("cloud.alerts") + + +@pytest.fixture() +def sent(monkeypatch): + out = [] + async def fake_send(title, body): + out.append((title, body)) + monkeypatch.setattr(alerts, "send_admin_alert", fake_send) + monkeypatch.setenv("STATIC_PROXY_URLS", "http://s1,http://s2") + monkeypatch.setenv("PROXY_URL", "http://paid") + alerts._watch_down.clear(); alerts._watch_nag.clear(); alerts._watch_strikes.clear() + return out + + +def _fail(name, n=1): + for _ in range(n): + asyncio.run(alerts._watch_update(name, False, "ReadTimeout: x")) + + +def test_one_miss_is_silent(sent): + _fail(alerts._PAID_TARGET) + assert sent == [] + + +def test_paid_down_with_statics_up_is_informational(sent): + _fail(alerts._PAID_TARGET, 2) + title, body = sent[-1] + assert title.startswith("â„šī¸") and "fallback" in title + assert "No impact" in body and "static pool is UP" in body + assert "failing" not in body.split("Probe error")[0] + + +def test_statics_down_is_a_cost_warning(sent): + _fail(alerts._STATIC_TARGET, 2) + title, body = sent[-1] + assert title.startswith("🟠") and "costs money" in body + + +def test_both_down_is_red(sent): + _fail(alerts._STATIC_TARGET, 2) + _fail(alerts._PAID_TARGET, 2) + title, body = sent[-1] + assert title.startswith("🔴") and "ALL YouTube proxies" in title + + +def test_recovery_resets_strikes(sent): + _fail(alerts._PAID_TARGET, 2) + asyncio.run(alerts._watch_update(alerts._PAID_TARGET, True, "")) + assert sent[-1][0].startswith("✅") + _fail(alerts._PAID_TARGET, 1) + assert len(sent) == 2 # a single new miss after recovery stays quiet