Skip to content

Commit f4c16e2

Browse files
committed
fix: redis max number of clients reached
due to initializing probot instance for every job run fixed by sharing probot instance
1 parent 833da07 commit f4c16e2

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/configs/redis.ts

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

4-
export const getRedisClient = () => {
4+
export const getRedisClient = (name?: string) => {
55
const redisClient = new Redis(appConfig.redisConfig!, {
66
maxRetriesPerRequest: null,
7+
name,
78
});
89
return redisClient;
910
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const skipFullSync = args.includes("--skip-full-sync");
1414

1515
await connectMongoDB();
1616

17-
const redisClient = getRedisClient();
17+
const redisClient = getRedisClient(`${appConfig.appSlug}-app`);
1818

1919
const probot = createProbot({
2020
overrides: {

src/processor/index.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
import type { Job } from "bullmq";
2-
import { createProbot } from "probot";
3-
import { SchedulerJobData } from "@wei/probot-scheduler";
2+
import type { SchedulerJobData } from "@wei/probot-scheduler";
3+
import type { Probot } from "probot";
44
import logger from "@/src/utils/logger.ts";
55
import { getPullConfig } from "@/src/utils/get-pull-config.ts";
66
import { Pull } from "@/src/processor/pull.ts";
77

8-
export default async function RepoJobProcessor(job: Job<SchedulerJobData>) {
9-
const log = logger.child({
10-
jobId: job.id,
11-
jobData: job.data,
12-
});
8+
export function getRepoProcessor(probot: Probot) {
9+
return async function RepoJobProcessor(job: Job<SchedulerJobData>) {
10+
const log = logger.child({
11+
jobId: job.id,
12+
jobData: job.data,
13+
});
1314

14-
log.info("🏃 Processing repo job");
15+
log.info("🏃 Processing repo job");
1516

16-
const { installation_id, owner, repo } = job.data;
17+
const { installation_id, owner, repo } = job.data;
1718

18-
try {
19-
// Get Octokit
20-
const probot = createProbot({ overrides: { log } });
21-
const octokit = await probot.auth(installation_id);
19+
try {
20+
const octokit = await probot.auth(installation_id);
2221

23-
const config = await getPullConfig(octokit, log, job.data);
24-
if (!config) {
25-
log.info(`⚠️ No config found, skipping`);
26-
return;
27-
}
22+
const config = await getPullConfig(octokit, log, job.data);
23+
if (!config) {
24+
log.info(`⚠️ No config found, skipping`);
25+
return;
26+
}
2827

29-
const pull = new Pull(octokit, { owner, repo, logger: log }, config);
30-
await pull.routineCheck();
28+
const pull = new Pull(octokit, { owner, repo, logger: log }, config);
29+
await pull.routineCheck();
3130

32-
log.info(`✅ Repo job ${job.id} processed successfully`);
33-
} catch (error) {
34-
log.error(error, "❌ Repo job failed");
35-
}
31+
log.info(`✅ Repo job ${job.id} processed successfully`);
32+
} catch (error) {
33+
log.error(error, "❌ Repo job failed");
34+
}
35+
};
3636
}

src/worker.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { createSchedulerWorker } from "@wei/probot-scheduler";
22
import { Redis } from "ioredis";
33
import { appConfig } from "@/src/configs/app-config.ts";
4-
import RepoJobProcessor from "@/src/processor/index.ts";
4+
import { getRepoProcessor } from "@/src/processor/index.ts";
5+
import { createProbot } from "probot";
6+
7+
const probot = createProbot();
8+
const RepoJobProcessor = getRepoProcessor(probot);
59

610
const redisClient = new Redis(appConfig.redisConfig!, {
711
maxRetriesPerRequest: null,
12+
name: `${appConfig.appSlug}-worker`,
813
});
914

1015
const worker = createSchedulerWorker(
@@ -22,3 +27,12 @@ worker.on("completed", (job) => {
2227
worker.on("failed", (job, err) => {
2328
console.error(`Job ${job?.id} failed: ${err.message}`);
2429
});
30+
31+
const gracefulShutdown = async (signal: string) => {
32+
console.log(`Received ${signal}, closing server...`);
33+
await worker.close();
34+
Deno.exit(0);
35+
};
36+
37+
Deno.addSignalListener("SIGINT", () => gracefulShutdown("SIGINT"));
38+
Deno.addSignalListener("SIGTERM", () => gracefulShutdown("SIGTERM"));

0 commit comments

Comments
 (0)