Merged in fix/redis-shutdown-graceful (pull request #1969)
Fix/redis shutdown graceful * fix: shutdown redis gracefully when container restarts * throttle scans to redis to avoid overwhelming it Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -11,6 +11,10 @@ import serverTiming from "@elysiajs/server-timing";
|
|||||||
import { AuthenticationError } from "@/errors/AuthenticationError";
|
import { AuthenticationError } from "@/errors/AuthenticationError";
|
||||||
import { ModelValidationError } from "@/errors/ModelValidationError";
|
import { ModelValidationError } from "@/errors/ModelValidationError";
|
||||||
|
|
||||||
|
import { setupShutdown } from "@/shutdown";
|
||||||
|
|
||||||
|
setupShutdown();
|
||||||
|
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(serverTiming())
|
.use(serverTiming())
|
||||||
.error("AUTHENTICATION_ERROR", AuthenticationError)
|
.error("AUTHENTICATION_ERROR", AuthenticationError)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { redis } from "@/services/redis";
|
|||||||
import { ModelValidationError } from "@/errors/ModelValidationError";
|
import { ModelValidationError } from "@/errors/ModelValidationError";
|
||||||
import { loggerModule } from "@/utils/logger";
|
import { loggerModule } from "@/utils/logger";
|
||||||
import { truncate } from "@/utils/truncate";
|
import { truncate } from "@/utils/truncate";
|
||||||
|
import { timeout } from "@/utils/timeout";
|
||||||
|
|
||||||
const MIN_LENGTH = 1;
|
const MIN_LENGTH = 1;
|
||||||
|
|
||||||
@@ -92,10 +93,13 @@ function validateKey(key: string) {
|
|||||||
|
|
||||||
if (parsedKey.length < MIN_LENGTH) {
|
if (parsedKey.length < MIN_LENGTH) {
|
||||||
throw new ModelValidationError(
|
throw new ModelValidationError(
|
||||||
"Key has to be atleast 1 character long"
|
"Key has to be at least 1 character long"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (parsedKey.includes("*")) {
|
||||||
|
throw new ModelValidationError("Key cannot contain wildcards");
|
||||||
|
}
|
||||||
if (parsedKey.includes("*")) {
|
if (parsedKey.includes("*")) {
|
||||||
throw new ModelValidationError("Key cannot contain wildcards");
|
throw new ModelValidationError("Key cannot contain wildcards");
|
||||||
}
|
}
|
||||||
@@ -115,6 +119,10 @@ async function deleteWithPattern(pattern: string) {
|
|||||||
"COUNT",
|
"COUNT",
|
||||||
5000
|
5000
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Throttle calls to Redis to avoid overwhelming it
|
||||||
|
await timeout(50);
|
||||||
|
|
||||||
cursor = newCursor;
|
cursor = newCursor;
|
||||||
keys.push(...foundKeys);
|
keys.push(...foundKeys);
|
||||||
} while (cursor !== "0");
|
} while (cursor !== "0");
|
||||||
|
|||||||
16
apps/redis-api/src/shutdown.ts
Normal file
16
apps/redis-api/src/shutdown.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { loggerModule } from "@/utils/logger";
|
||||||
|
import { redis } from "@/services/redis";
|
||||||
|
|
||||||
|
const shutdownLogger = loggerModule("shutdown");
|
||||||
|
|
||||||
|
export function setupShutdown() {
|
||||||
|
process.on("SIGINT", shutdown);
|
||||||
|
process.on("SIGTERM", shutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function shutdown() {
|
||||||
|
shutdownLogger.info("Shutting down...");
|
||||||
|
shutdownLogger.info("Closing Redis connection...");
|
||||||
|
await redis.quit();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
3
apps/redis-api/src/utils/timeout.ts
Normal file
3
apps/redis-api/src/utils/timeout.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export function timeout(ms: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user