From b801c9e2c470977267794f86ec9b1a2edb26a4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20H=C3=A4kkinen?= Date: Wed, 18 Sep 2024 10:42:17 +0300 Subject: [PATCH] Retry failed connection attempts --- app/src/bolt/bolt.service.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/src/bolt/bolt.service.ts b/app/src/bolt/bolt.service.ts index bf4a806..de12066 100644 --- a/app/src/bolt/bolt.service.ts +++ b/app/src/bolt/bolt.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from "@nestjs/common"; +import { Injectable, Logger } from "@nestjs/common"; import { App, LogLevel } from "@slack/bolt"; import { StringIndexed } from "@slack/bolt/dist/types/helpers"; import { ConfigService } from "../common/config/config.service"; @@ -7,23 +7,24 @@ import { BoltLogger } from "./bolt-logger"; @Injectable() export class BoltService { private bolt: App; + private logger = new Logger(BoltService.name); constructor(private configService: ConfigService) {} async connect() { const { appToken, token, signingSecret } = this.configService.getConfig().bolt; - const logger = new BoltLogger(); + const boltLogger = new BoltLogger(); this.bolt = new App({ appToken, token, signingSecret, socketMode: true, - logger, + logger: boltLogger, logLevel: LogLevel.INFO, }); - await this.bolt.start(); + await this.connectWithRetry(); } async disconnect() { @@ -33,4 +34,19 @@ export class BoltService { getBolt(): App { return this.bolt; } + + private async connectWithRetry(): Promise { + // eslint-disable-next-line no-constant-condition + while (true) { + try { + await this.bolt.start(); + return; + } catch (error) { + this.logger.error("Failed to initialize a connection to Slack.", error); + this.logger.log("Trying to connect again to Slack."); + + await new Promise((resolve) => setTimeout(resolve, 2000)); + } + } + } }