Skip to content

Commit

Permalink
refactor: improve Redis connection handling and add process exit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoruan committed Jan 10, 2025
1 parent 08aca7e commit 4937548
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/server/store/Redis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import process from "node:process";

import { Cluster, Redis } from "ioredis";

import logger from "@/server/logger";

import {
BaseStore,
type RedisClusterConfig,
Expand All @@ -16,7 +12,6 @@ import {

const defaultOptions = {
ttl: STATE_TTL,
lazyConnect: true,
} satisfies RedisConfig;

export default class RedisStore extends BaseStore implements Store {
Expand Down Expand Up @@ -66,10 +61,12 @@ export default class RedisStore extends BaseStore implements Store {
}
}

this.redis.connect().catch((e) => {
logger.error({ message: e.message }, "Failed to connect to redis: @{message}");
this.addProcessExitHandler();
}

process.exit(1);
private addProcessExitHandler(): void {
process.once("exit", async () => {
await this.redis.quit();
});
}

Expand Down

0 comments on commit 4937548

Please sign in to comment.