fix(api): reuse redis client in health checks (#3671)

Co-authored-by: Gergő Móricz <mo.geryy@gmail.com>
This commit is contained in:
Ritwij Aryan Parmar
2026-06-10 14:56:03 -04:00
committed by GitHub
parent b88d363e60
commit 2ecfb8d3ff
3 changed files with 30 additions and 6 deletions

View File

@@ -1,8 +1,7 @@
import { Request, Response } from "express";
import { config } from "../../../config";
import Redis from "ioredis";
import { logger } from "../../../lib/logger";
import { redisRateLimitClient } from "../../../services/rate-limiter";
import { getRedisConnection } from "../../../services/queue-service";
export async function redisHealthController(req: Request, res: Response) {
const retryOperation = async (operation, retries = 3) => {
@@ -18,7 +17,7 @@ export async function redisHealthController(req: Request, res: Response) {
};
try {
const queueRedis = new Redis(config.REDIS_URL!);
const queueRedis = getRedisConnection();
const testKey = "test";
const testValue = "test";

View File

@@ -6,6 +6,7 @@ import { withSpan, setSpanAttributes } from "../../lib/otel-tracer";
import amqp from "amqplib";
import { v5 as uuidv5, validate as isUUID } from "uuid";
import { config } from "../../config";
import { nuqRedis } from "./redis";
// === Basics
@@ -1746,6 +1747,10 @@ export const crawlGroup = new NuQJobGroup("nuq.group_crawl");
// === Cleanup
export async function nuqShutdown() {
await scrapeQueue.shutdown();
await Promise.all([
scrapeQueue.shutdown(),
crawlFinishedQueue.shutdown(),
nuqRedis.shutdown(),
]);
await nuqPool.end();
}

View File

@@ -51,8 +51,8 @@ return 1`,
} as const;
type ScriptHashes = {
[K in keyof typeof luaScripts]: {
[K2 in keyof (typeof luaScripts)[K]]: string;
-readonly [K in keyof typeof luaScripts]: {
-readonly [K2 in keyof (typeof luaScripts)[K]]: string;
};
};
@@ -123,6 +123,24 @@ export const ensureRedis = async () => {
return initPromise;
};
export const shutdownRedis = async () => {
initPromise = null;
for (const key of Object.keys(scripts) as (keyof ScriptHashes)[]) {
scripts[key] = {} as ScriptHashes[typeof key];
}
if (redis.status === "wait" || redis.status === "end") {
return;
}
try {
await redis.quit();
} catch (err) {
logger.warn("Error while closing NuQ Redis connection", { err });
redis.disconnect();
}
};
export const semaphoreKeys = (teamId: string) => {
return {
leases: `nuq:sema:{${teamId}}:leases`,
@@ -147,10 +165,12 @@ type NuQRedis = Redis & {
scripts: typeof scripts;
runScript: typeof runScript;
ensure: typeof ensureRedis;
shutdown: typeof shutdownRedis;
};
export const nuqRedis: NuQRedis = Object.assign(redis, {
scripts,
runScript,
ensure: ensureRedis,
shutdown: shutdownRedis,
});