Merge afec11e21b846b1e5b7b5fb0eb637224173c0093 into 598f1826d8b2bc969aace2c6459824737667218c

This commit is contained in:
Tom Alison 2026-03-20 21:08:26 -07:00 committed by GitHub
commit f9afb9ea9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,6 +17,7 @@ const TELEGRAM_POLL_RESTART_POLICY = {
const POLL_STALL_THRESHOLD_MS = 90_000;
const POLL_WATCHDOG_INTERVAL_MS = 30_000;
const POLL_STOP_GRACE_MS = 15_000;
const MAX_CONSECUTIVE_POLL_RESTARTS = 5;
const waitForGracefulStop = async (stop: () => Promise<void>) => {
let timer: ReturnType<typeof setTimeout> | undefined;
@ -54,6 +55,7 @@ type TelegramPollingSessionOpts = {
export class TelegramPollingSession {
#restartAttempts = 0;
#consecutiveStallRestarts = 0;
#webhookCleared = false;
#forceRestarted = false;
#activeRunner: ReturnType<typeof run> | undefined;
@ -186,11 +188,14 @@ export class TelegramPollingSession {
await this.#confirmPersistedOffset(bot);
let lastGetUpdatesAt = Date.now();
bot.api.config.use((prev, method, payload, signal) => {
bot.api.config.use(async (prev, method, payload, signal) => {
const result = await prev(method, payload, signal);
if (method === "getUpdates") {
lastGetUpdatesAt = Date.now();
this.#restartAttempts = 0;
this.#consecutiveStallRestarts = 0;
}
return prev(method, payload, signal);
return result;
});
const runner = run(bot, this.opts.runnerOptions);
@ -231,9 +236,16 @@ export class TelegramPollingSession {
}
const elapsed = Date.now() - lastGetUpdatesAt;
if (elapsed > POLL_STALL_THRESHOLD_MS && runner.isRunning()) {
this.#consecutiveStallRestarts += 1;
stalledRestart = true;
if (this.#consecutiveStallRestarts > MAX_CONSECUTIVE_POLL_RESTARTS) {
this.opts.log(
`[telegram] Polling recovery exhausted after ${this.#consecutiveStallRestarts} consecutive stall restarts without successful getUpdates; escalating to process exit.`,
);
process.exit(1);
}
this.opts.log(
`[telegram] Polling stall detected (no getUpdates for ${formatDurationPrecise(elapsed)}); forcing restart.`,
`[telegram] Polling stall detected (no getUpdates for ${formatDurationPrecise(elapsed)}); forcing restart (attempt ${this.#consecutiveStallRestarts}/${MAX_CONSECUTIVE_POLL_RESTARTS}).`,
);
void stopRunner();
void stopBot();