fix: prevent Telegram polling errors from blocking container startup (#989) (#1019)

This fix addresses the issue where the container gets stuck in an endless
loop of Telegram connection errors when the bot is configured but Telegram
is unreachable during startup.

Changes:
- Add 10-second startup delay before initializing Telegram polling to allow
  the system to settle
- Implement exponential backoff (5s to 5min) when Telegram connection fails
- Add rate-limited error logging (max once per minute per user) to reduce
  log spam and prevent event loop blocking
- Track error state per user to manage backoff independently
- Auto-reset error state on successful connection
- Update tests to account for new error state tracking

Fixes #989
This commit is contained in:
Chris 2026-04-13 20:44:27 +03:00 committed by GitHub
parent e2d0b4d228
commit 828b5ebc0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 137 additions and 18 deletions

View file

@ -8,25 +8,37 @@ async function initializeTelegramPolling() {
return;
}
try {
// Find users with configured Telegram tokens
const usersWithTelegram = await User.findAll({
where: {
telegram_bot_token: {
[require('sequelize').Op.ne]: null,
},
},
});
// Add a delay before starting Telegram polling to allow the system to settle
// and prevent immediate error floods if Telegram is temporarily unreachable
const startupDelay = 10000; // 10 seconds
if (usersWithTelegram.length > 0) {
// Add each user to the polling list
for (const user of usersWithTelegram) {
await telegramPoller.addUser(user);
setTimeout(async () => {
try {
// Find users with configured Telegram tokens
const usersWithTelegram = await User.findAll({
where: {
telegram_bot_token: {
[require('sequelize').Op.ne]: null,
},
},
});
if (usersWithTelegram.length > 0) {
console.log(
`Initializing Telegram polling for ${usersWithTelegram.length} user(s)...`
);
// Add each user to the polling list
for (const user of usersWithTelegram) {
await telegramPoller.addUser(user);
}
}
} catch (error) {
console.error(
'Error initializing Telegram polling:',
error.message
);
}
} catch (error) {
// Telegram polling will be initialized later when the database is available
}
}, startupDelay);
}
module.exports = { initializeTelegramPolling };