Skip to content

Commit

Permalink
Retry failed connection attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Sep 18, 2024
1 parent 232e870 commit b801c9e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions app/src/bolt/bolt.service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -7,23 +7,24 @@ import { BoltLogger } from "./bolt-logger";
@Injectable()
export class BoltService {
private bolt: App<StringIndexed>;
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() {
Expand All @@ -33,4 +34,19 @@ export class BoltService {
getBolt(): App<StringIndexed> {
return this.bolt;
}

private async connectWithRetry(): Promise<void> {
// 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));
}
}
}
}

0 comments on commit b801c9e

Please sign in to comment.