From 8ad94c8dadf2f2dc10be048356fca0a0e95921e5 Mon Sep 17 00:00:00 2001 From: anor Date: Wed, 28 Oct 2026 15:00:55 +1000 Subject: [PATCH] =?UTF-8?q?feat(=D0=BF=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5):=20=D1=82=D1=80=D0=B5=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=B0,=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20=D0=BF=D0=BB=D0=B0?= =?UTF-8?q?=D1=82=D1=91=D0=B6=D0=BA=D0=B0=20=D1=82=D1=80=D0=B8=D0=B6=D0=B4?= =?UTF-8?q?=D1=8B=20=D0=BF=D0=BE=D0=B4=D1=80=D1=8F=D0=B4=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=D0=B0=20=D0=BF=D0=BB=D0=B0?= =?UTF-8?q?=D1=82=D1=91=D0=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- db.py | 7 +++++++ tests/test_topup.py | 16 ++++++++++++++++ topup.py | 13 +++++++++++++ 3 files changed, 36 insertions(+) diff --git a/db.py b/db.py index 87651f0..11885e2 100644 --- a/db.py +++ b/db.py @@ -1170,6 +1170,13 @@ def get_topup_by_provider(provider: str, provider_id: str) -> dict | None: return dict(row) if row else None +def recent_topup_statuses(limit: int) -> list[str]: + """Статусы последних платежей, новые первыми — сторожу серии отказов платёжки.""" + with _conn() as conn: + return [r[0] for r in conn.execute( + "SELECT status FROM topups ORDER BY id DESC LIMIT ?", (limit,)).fetchall()] + + def count_open_topups_since(username: str, since_ts: float) -> int: with _conn() as conn: return int(conn.execute( diff --git a/tests/test_topup.py b/tests/test_topup.py index b25b2db..b5016e3 100644 --- a/tests/test_topup.py +++ b/tests/test_topup.py @@ -441,3 +441,19 @@ def test_person_block_and_status_view(env, monkeypatch): assert topup.status_for_person(USER, 99999) is None monkeypatch.setenv("TOPUP_ENABLED", "0") assert topup.person_block(USER) is None + + +def test_provider_down_three_times_in_a_row_alerts(env, monkeypatch): + """Платёжка не создала платёж три раза подряд — тревога один раз на серию; два сбоя — + тишина. Порча: тревога на каждом сбое или никогда.""" + db, topup, balance, prov, sent = env + monkeypatch.setattr(topup, "MAX_OPEN_PER_HOUR", 99) + prov.fail_create = True + for _ in range(2): + with pytest.raises(topup.TopupError): + _start(topup) + assert not any("подряд" in t for t in sent) + for _ in range(2): + with pytest.raises(topup.TopupError): + _start(topup) + assert sum("подряд" in t for t in sent) == 1 diff --git a/topup.py b/topup.py index 31a1eb2..ea4f0ce 100644 --- a/topup.py +++ b/topup.py @@ -182,6 +182,7 @@ def start(username: str, amount_raw, ip: str | None) -> dict: except Exception as e: # noqa: BLE001 — человеку только наблюдаемый факт _log.exception("topup: платёж №%s не создан (%s)", tid, username) db.fail_topup(tid, json.dumps({"error": type(e).__name__}, ensure_ascii=False)) + _alert_if_provider_down() raise TopupError(502, PROVIDER_DOWN_TEXT) from None host = (urlsplit(res.get("pay_url") or "").hostname or "").lower() if urlsplit(res.get("pay_url") or "").scheme != "https" or host not in prov.pay_hosts: @@ -194,6 +195,18 @@ def start(username: str, amount_raw, ip: str | None) -> dict: return {"topup_id": tid, "pay_url": res["pay_url"]} +def _alert_if_provider_down() -> None: + """Тревога ровно на третьем отказе подряд: на каждом — шум, а серия видна одна.""" + import db + try: + last = db.recent_topup_statuses(4) + except Exception: # noqa: BLE001 + return + if last[:3] == ["failed"] * 3 and (len(last) < 4 or last[3] != "failed"): + _alert("🔴 smeta-web: платёжка не создала платёж три раза подряд — люди не могут " + "пополнить счёт") + + def _raw(state: dict) -> str: return json.dumps(state.get("raw") or {}, ensure_ascii=False, default=str)