Skip to content

Commit

Permalink
fix: redis max number of clients reached
Browse files Browse the repository at this point in the history
due to initializing probot instance for every job run
fixed by sharing probot instance
  • Loading branch information
wei committed Nov 19, 2024
1 parent 833da07 commit f4c16e2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/configs/redis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { appConfig } from "@/src/configs/app-config.ts";
import { Redis } from "ioredis";

export const getRedisClient = () => {
export const getRedisClient = (name?: string) => {
const redisClient = new Redis(appConfig.redisConfig!, {
maxRetriesPerRequest: null,
name,
});
return redisClient;
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const skipFullSync = args.includes("--skip-full-sync");

await connectMongoDB();

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

const probot = createProbot({
overrides: {
Expand Down
48 changes: 24 additions & 24 deletions src/processor/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import type { Job } from "bullmq";
import { createProbot } from "probot";
import { SchedulerJobData } from "@wei/probot-scheduler";
import type { SchedulerJobData } from "@wei/probot-scheduler";
import type { Probot } from "probot";
import logger from "@/src/utils/logger.ts";
import { getPullConfig } from "@/src/utils/get-pull-config.ts";
import { Pull } from "@/src/processor/pull.ts";

export default async function RepoJobProcessor(job: Job<SchedulerJobData>) {
const log = logger.child({
jobId: job.id,
jobData: job.data,
});
export function getRepoProcessor(probot: Probot) {
return async function RepoJobProcessor(job: Job<SchedulerJobData>) {
const log = logger.child({
jobId: job.id,
jobData: job.data,
});

log.info("🏃 Processing repo job");
log.info("🏃 Processing repo job");

const { installation_id, owner, repo } = job.data;
const { installation_id, owner, repo } = job.data;

try {
// Get Octokit
const probot = createProbot({ overrides: { log } });
const octokit = await probot.auth(installation_id);
try {
const octokit = await probot.auth(installation_id);

const config = await getPullConfig(octokit, log, job.data);
if (!config) {
log.info(`⚠️ No config found, skipping`);
return;
}
const config = await getPullConfig(octokit, log, job.data);
if (!config) {
log.info(`⚠️ No config found, skipping`);
return;
}

const pull = new Pull(octokit, { owner, repo, logger: log }, config);
await pull.routineCheck();
const pull = new Pull(octokit, { owner, repo, logger: log }, config);
await pull.routineCheck();

log.info(`✅ Repo job ${job.id} processed successfully`);
} catch (error) {
log.error(error, "❌ Repo job failed");
}
log.info(`✅ Repo job ${job.id} processed successfully`);
} catch (error) {
log.error(error, "❌ Repo job failed");
}
};
}
16 changes: 15 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createSchedulerWorker } from "@wei/probot-scheduler";
import { Redis } from "ioredis";
import { appConfig } from "@/src/configs/app-config.ts";
import RepoJobProcessor from "@/src/processor/index.ts";
import { getRepoProcessor } from "@/src/processor/index.ts";
import { createProbot } from "probot";

const probot = createProbot();
const RepoJobProcessor = getRepoProcessor(probot);

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

const worker = createSchedulerWorker(
Expand All @@ -22,3 +27,12 @@ worker.on("completed", (job) => {
worker.on("failed", (job, err) => {
console.error(`Job ${job?.id} failed: ${err.message}`);
});

const gracefulShutdown = async (signal: string) => {
console.log(`Received ${signal}, closing server...`);
await worker.close();
Deno.exit(0);
};

Deno.addSignalListener("SIGINT", () => gracefulShutdown("SIGINT"));
Deno.addSignalListener("SIGTERM", () => gracefulShutdown("SIGTERM"));

0 comments on commit f4c16e2

Please sign in to comment.