Skip to content

Commit 38ac68c

Browse files
authored
fix: redis graceful exit and job cleanup (wei#636)
* fix: redis graceful exit and job cleanup * release: v2.0.0-alpha.4
1 parent 227fc55 commit 38ac68c

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.0.0-alpha.3",
2+
"version": "2.0.0-alpha.4",
33
"tasks": {
44
"dev": "deno run --env-file --allow-all src/index.ts",
55
"dev:skip-full-sync": "deno task dev --skip-full-sync",

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ const redisClient = new Redis(appConfig.redisConfig!, {
1212
name: `${appConfig.appSlug}-worker`,
1313
});
1414

15+
const MAX_RETAINED_JOBS = 1000;
16+
const JOB_RETENTION_SECONDS = 3600; // 1 hour
17+
1518
const worker = createSchedulerWorker(
1619
RepoJobProcessor,
1720
{
1821
connection: redisClient,
1922
concurrency: 10,
23+
removeOnComplete: {
24+
count: MAX_RETAINED_JOBS,
25+
age: JOB_RETENTION_SECONDS,
26+
},
27+
removeOnFail: {
28+
count: MAX_RETAINED_JOBS,
29+
age: JOB_RETENTION_SECONDS,
30+
},
2031
},
2132
);
2233

@@ -31,6 +42,11 @@ worker.on("failed", (job, err) => {
3142
const gracefulShutdown = async (signal: string) => {
3243
console.log(`Received ${signal}, closing worker...`);
3344
await worker.close();
45+
try {
46+
await redisClient.quit();
47+
} catch {
48+
// ignore
49+
}
3450
Deno.exit(0);
3551
};
3652

0 commit comments

Comments
 (0)