From e49ee114b362508dbba9f0360a3ddb5462585469 Mon Sep 17 00:00:00 2001 From: berkingurcan Date: Fri, 13 Sep 2024 17:59:14 +0700 Subject: [PATCH 1/4] fix reconnect strategy for redis connection connectTimeout: 10000, keepAlive: true, keepAliveInterval: 5000, --- src/config.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/config.js b/src/config.js index aacba8d..337c616 100644 --- a/src/config.js +++ b/src/config.js @@ -1,4 +1,5 @@ import package_json from "../package.json" with { type: "json" }; +import log from "./logger"; import dotenv from "dotenv"; dotenv.config({ path: `.env.local`, override: true }); @@ -10,6 +11,16 @@ export const redisConfig = { socket: { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT, + reconnectStrategy: function(retries) { + if (retries >= 10) { + log.error("Unable to reconnect to Redis reconnectStrategy") + return new Error("Unable to reconnect to Redis reconnectStrategy"); + } + return Math.min(retries * 100, 3000); + }, + connectTimeout: 10000, + keepAlive: true, + keepAliveInterval: 5000, }, tls: true, }; From ad96bf4cdfea032946bbe5a5245ba66679230e4f Mon Sep 17 00:00:00 2001 From: berkingurcan Date: Fri, 13 Sep 2024 18:31:02 +0700 Subject: [PATCH 2/4] add logs and increase retry time --- src/bot.ts | 4 ++++ src/config.js | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 10ea783..8da36b5 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -48,7 +48,11 @@ process.on("uncaughtException", (error) => { const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const redisClient = createClient(redisConfig); + + redisClient.on('error', err => log.error('Redis Client Error', err)); + await redisClient.connect(); + redisClient.on('connect', () => log.info('Connected to Redis server')); const rest = new REST({ version: "10" }).setToken(discordConfig.token); diff --git a/src/config.js b/src/config.js index 337c616..afcc2f8 100644 --- a/src/config.js +++ b/src/config.js @@ -12,11 +12,11 @@ export const redisConfig = { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT, reconnectStrategy: function(retries) { - if (retries >= 10) { + if (retries >= 20) { log.error("Unable to reconnect to Redis reconnectStrategy") return new Error("Unable to reconnect to Redis reconnectStrategy"); } - return Math.min(retries * 100, 3000); + return Math.max(retries * 500, 4000); }, connectTimeout: 10000, keepAlive: true, From c15f54e9ac406e23f3f3f7911ad314c63062c559 Mon Sep 17 00:00:00 2001 From: berkingurcan Date: Fri, 13 Sep 2024 18:32:53 +0700 Subject: [PATCH 3/4] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2255cc7..e069b05 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "discord-bot", "type": "module", - "version": "0.3.2", + "version": "0.3.3", "description": "", "main": "index.js", "scripts": { From 046bca5e6a3cc54ad72796eccd5c37778eba86c9 Mon Sep 17 00:00:00 2001 From: berkingurcan Date: Fri, 13 Sep 2024 19:19:08 +0700 Subject: [PATCH 4/4] fix add non-zero exit --- src/bot.ts | 4 ++-- src/config.js | 6 +++--- src/lib/makeSurveyPost.js | 6 +++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 8da36b5..639f809 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -49,10 +49,10 @@ process.on("uncaughtException", (error) => { const redisClient = createClient(redisConfig); - redisClient.on('error', err => log.error('Redis Client Error', err)); + redisClient.on("error", (err) => log.error("Redis Client Error", err)); await redisClient.connect(); - redisClient.on('connect', () => log.info('Connected to Redis server')); + redisClient.on("connect", () => log.info("Connected to Redis server")); const rest = new REST({ version: "10" }).setToken(discordConfig.token); diff --git a/src/config.js b/src/config.js index afcc2f8..30b6ef4 100644 --- a/src/config.js +++ b/src/config.js @@ -11,10 +11,10 @@ export const redisConfig = { socket: { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT, - reconnectStrategy: function(retries) { + reconnectStrategy: function (retries) { if (retries >= 20) { - log.error("Unable to reconnect to Redis reconnectStrategy") - return new Error("Unable to reconnect to Redis reconnectStrategy"); + log.error("Unable to reconnect to Redis reconnectStrategy"); + process.exit(1); } return Math.max(retries * 500, 4000); }, diff --git a/src/lib/makeSurveyPost.js b/src/lib/makeSurveyPost.js index 23d3f82..cee575e 100644 --- a/src/lib/makeSurveyPost.js +++ b/src/lib/makeSurveyPost.js @@ -9,7 +9,11 @@ export const makeSurveyPost = async ( if (!(await redisClient.sIsMember("surveys", surveyName))) { return [{ content: "There is no survey with that name", ephemeral: true }]; } else { - const [msg, files] = await surveyToText(redisClient, surveyName, isSummaryCommand); + const [msg, files] = await surveyToText( + redisClient, + surveyName, + isSummaryCommand, + ); const lines = msg.split("\n"); const chunks = [];