Skip to content

Commit

Permalink
Merge pull request #72 from berkingurcan/fix-SocketClosedUnexpectedly…
Browse files Browse the repository at this point in the history
…Error

fix reconnect strategy for redis connection
  • Loading branch information
johnmarcou authored Sep 13, 2024
2 parents e1bb7a8 + 046bca5 commit c3280b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "discord-bot",
"type": "module",
"version": "0.3.2",
"version": "0.3.3",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
11 changes: 11 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -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 });
Expand All @@ -10,6 +11,16 @@ export const redisConfig = {
socket: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
reconnectStrategy: function (retries) {
if (retries >= 20) {
log.error("Unable to reconnect to Redis reconnectStrategy");
process.exit(1);
}
return Math.max(retries * 500, 4000);
},
connectTimeout: 10000,
keepAlive: true,
keepAliveInterval: 5000,
},
tls: true,
};
Expand Down
6 changes: 5 additions & 1 deletion src/lib/makeSurveyPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit c3280b7

Please sign in to comment.