fallback missing Telegram city topics
This commit is contained in:
@@ -1733,6 +1733,7 @@ def _process_airport_city(
|
||||
# Send to all target chats
|
||||
sent = False
|
||||
for chat_id in chat_ids:
|
||||
thread_id = 0
|
||||
try:
|
||||
kwargs = {}
|
||||
thread_id = _resolve_thread_id(chat_id, city)
|
||||
@@ -1741,6 +1742,25 @@ def _process_airport_city(
|
||||
_rate_limited_send(bot, chat_id, message, **kwargs)
|
||||
sent = True
|
||||
except Exception as exc:
|
||||
if thread_id and "message thread not found" in str(exc).lower():
|
||||
logger.warning(
|
||||
"airport push thread missing; retrying main chat city={} chat_id={} thread_id={}",
|
||||
city,
|
||||
chat_id,
|
||||
thread_id,
|
||||
)
|
||||
try:
|
||||
_rate_limited_send(bot, chat_id, message)
|
||||
sent = True
|
||||
continue
|
||||
except Exception as fallback_exc:
|
||||
logger.warning(
|
||||
"airport push main chat fallback failed city={} chat_id={}: {}",
|
||||
city,
|
||||
chat_id,
|
||||
fallback_exc,
|
||||
)
|
||||
continue
|
||||
logger.warning("airport push failed city={} chat_id={}: {}", city, chat_id, exc)
|
||||
|
||||
if sent:
|
||||
|
||||
@@ -405,6 +405,48 @@ def test_airport_push_rejects_observation_older_than_last_push(monkeypatch):
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_airport_push_retries_main_chat_when_forum_thread_is_missing(monkeypatch):
|
||||
import src.utils.telegram_push as telegram_push
|
||||
|
||||
calls = []
|
||||
|
||||
monkeypatch.setattr(
|
||||
telegram_push,
|
||||
"_load_airport_city_weather_for_push",
|
||||
lambda _city: {
|
||||
"local_time": "15:22",
|
||||
"current": {"temp": 30.0},
|
||||
"deb": {"prediction": 31.0},
|
||||
"airport_primary": {
|
||||
"temp": 30.0,
|
||||
"obs_time": "2026-06-11T07:52:00+00:00",
|
||||
},
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(telegram_push, "_resolve_thread_id", lambda _chat, _city: 99)
|
||||
|
||||
def fake_send(_bot, chat_id, _message, **kwargs):
|
||||
calls.append((chat_id, kwargs))
|
||||
if kwargs.get("message_thread_id"):
|
||||
raise RuntimeError("Bad Request: message thread not found")
|
||||
|
||||
monkeypatch.setattr(telegram_push, "_rate_limited_send", fake_send)
|
||||
|
||||
result = telegram_push._process_airport_city(
|
||||
"shenzhen",
|
||||
now_ts=1781164400,
|
||||
last_city={},
|
||||
chat_ids=["chat-1"],
|
||||
bot=object(),
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert calls == [
|
||||
("chat-1", {"message_thread_id": 99}),
|
||||
("chat-1", {}),
|
||||
]
|
||||
|
||||
|
||||
def test_high_freq_airport_push_workers_default_to_one_for_shared_cpu(monkeypatch):
|
||||
source = Path("src/utils/telegram_push.py").read_text(encoding="utf-8")
|
||||
assert 'TELEGRAM_AIRPORT_PUSH_MAX_WORKERS", 1' in source
|
||||
|
||||
Reference in New Issue
Block a user