diff --git a/lib/core.js b/lib/core.js index bd791dd..8bf79bf 100644 --- a/lib/core.js +++ b/lib/core.js @@ -280,6 +280,35 @@ core.start = async (commander) => { }); let isReconnecting = false; + let rtmRestartAttempts = 0; + const RTM_RESTART_BASE_DELAY_MS = 5000; + const RTM_RESTART_MAX_DELAY_MS = 60000; + + function scheduleRtmRestart() { + const delay = Math.min( + RTM_RESTART_BASE_DELAY_MS * Math.pow(2, rtmRestartAttempts), + RTM_RESTART_MAX_DELAY_MS + ); + rtmRestartAttempts++; + console.log(`Retrying RTM connection in ${delay / 1000}s... (attempt ${rtmRestartAttempts})`); + setTimeout(() => { + rtm.start(); + }, delay); + } + + function isNetworkError(error) { + if (!error) return false; + const msg = error.message || String(error); + return ( + error.code === "ENOTFOUND" || + error.code === "ECONNREFUSED" || + error.code === "ETIMEDOUT" || + msg.includes("ENOTFOUND") || + msg.includes("ECONNREFUSED") || + msg.includes("ETIMEDOUT") || + msg.includes("getaddrinfo") + ); + } rtm.on("disconnect", () => { if (!isReconnecting) { @@ -295,11 +324,12 @@ core.start = async (commander) => { }); rtm.on("authenticated", (rtmStartData) => { + rtmRestartAttempts = 0; if (isReconnecting) { console.log("RTM reconnected successfully"); isReconnecting = false; } - + if (!util.startUp) { console.log( `Logged in as ${chalk.bold(rtmStartData.self.name)} of team ${chalk.green.bold(rtmStartData.team.name)}, but not yet connected to a channel` @@ -311,6 +341,9 @@ core.start = async (commander) => { rtm.on("unable_to_rtm_start", (error) => { console.error("Unable to start RTM:", error.message || error); isReconnecting = false; + if (isNetworkError(error)) { + scheduleRtmRestart(); + } }); rtm.on("message", (message) => {