Skip to content

Commit 251266b

Browse files
committed
fix: redis graceful exit and job cleanup
1 parent 84d7bd2 commit 251266b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

scripts/full-sync.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { appConfig } from "@/src/configs/app-config.ts";
88

99
async function main() {
1010
let exitCode = 0;
11+
let redisClient: ReturnType<typeof getRedisClient> | undefined;
1112

1213
try {
1314
await connectMongoDB();
14-
const redisClient = getRedisClient(`${appConfig.appSlug}-full-sync`);
15+
redisClient = getRedisClient(`${appConfig.appSlug}-full-sync`);
1516

1617
const probot = createProbot({ overrides: { log: logger } });
1718
await fullSync(probot, {
@@ -23,6 +24,13 @@ async function main() {
2324
exitCode = 1;
2425
} finally {
2526
await disconnectMongoDB();
27+
if (redisClient) {
28+
try {
29+
await redisClient.quit();
30+
} catch {
31+
// ignore
32+
}
33+
}
2634
Deno.exit(exitCode);
2735
}
2836
}

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Deno.addSignalListener("SIGTERM", () => handleAppTermination("SIGTERM"));
5656
function handleAppTermination(signal: string) {
5757
log.info(`[${signal}] Signal received: closing MongoDB connection`);
5858
disconnectMongoDB();
59+
try {
60+
// Close Redis connection to avoid lingering connections
61+
redisClient.quit();
62+
} catch {
63+
// ignore
64+
}
5965
log.info("[MongoDB] Connection closed due to app termination");
6066
Deno.exit(0);
6167
}

src/worker.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ const worker = createSchedulerWorker(
1717
{
1818
connection: redisClient,
1919
concurrency: 10,
20+
removeOnComplete: {
21+
count: 1000,
22+
age: 3600,
23+
},
24+
removeOnFail: {
25+
count: 1000,
26+
age: 3600,
27+
},
2028
},
2129
);
2230

@@ -31,6 +39,11 @@ worker.on("failed", (job, err) => {
3139
const gracefulShutdown = async (signal: string) => {
3240
console.log(`Received ${signal}, closing worker...`);
3341
await worker.close();
42+
try {
43+
await redisClient.quit();
44+
} catch {
45+
// ignore
46+
}
3447
Deno.exit(0);
3548
};
3649

0 commit comments

Comments
 (0)