From 732be16490814a28ec8e46d441e9498912413bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadeusz=20So=C5=9Bnierz?= <tadeusz@sosnierz.com> Date: Thu, 11 Nov 2021 14:15:23 +0100 Subject: [PATCH] Make sure the bridge doesn't crash (and crashloop) it the token is invalid --- src/discordas.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/discordas.ts b/src/discordas.ts index e31065b..182b5cd 100644 --- a/src/discordas.ts +++ b/src/discordas.ts @@ -214,20 +214,34 @@ async function run(): Promise<void> { roomhandler.bindThirdparty(); + try { + await startDiscordBot(discordbot); + log.info("Discordbot started successfully"); + } catch (err) { + log.error(err); + log.error("Failure during startup. Exiting"); + process.exit(1); + } + await appservice.begin(); log.info(`Started listening on port ${port}`); +} + +async function startDiscordBot(discordbot: DiscordBot, falloffSeconds = 5) { try { await discordbot.init(); await discordbot.run(); - log.info("Discordbot started successfully"); } catch (err) { - log.error(err); - log.error("Failure during startup. Exiting"); - process.exit(1); + // no more than 5 minutes + const newFalloffSeconds = Math.min(falloffSeconds * 2, 5 * 60); + log.error(`Failed do start Discordbot: ${err.code}. Will try again in ${newFalloffSeconds} seconds`); + await new Promise((r, _) => setTimeout(r, newFalloffSeconds * 1000)); + return startDiscordBot(discordbot, newFalloffSeconds); } } + run().catch((err) => { log.error("A fatal error occurred during startup:", err); process.exit(1); -- GitLab