diff --git a/server/env.ts b/server/env.ts index 604d4b99f3..60ba6df0a2 100644 --- a/server/env.ts +++ b/server/env.ts @@ -221,6 +221,17 @@ export class Environment { 10 ); + /** + * The time in milliseconds that a redis connection can stay unavailable + * before the process exits, so that the process manager can restart it. + */ + @IsNumber() + @Min(1000) + public REDIS_UNAVAILABLE_TIMEOUT = parseInt( + environment.REDIS_UNAVAILABLE_TIMEOUT || "300000", + 10 + ); + /** * The fully qualified, external facing domain name of the server. * If not set, will be derived from HEROKU_APP_NAME for Heroku deployments. diff --git a/server/storage/redis.test.ts b/server/storage/redis.test.ts index 0fff4e4403..a0c7d15b7b 100644 --- a/server/storage/redis.test.ts +++ b/server/storage/redis.test.ts @@ -1,4 +1,6 @@ import { randomUUID } from "node:crypto"; +import env from "@server/env"; +import Logger from "@server/logging/Logger"; import Redis from "./redis"; describe("RedisAdapter#zaddWithSequence", () => { @@ -66,3 +68,54 @@ describe("RedisAdapter#zaddWithSequence", () => { ).rejects.toThrow("Redis unavailable"); }); }); + +describe("RedisAdapter unavailable timeout", () => { + let client: Redis; + + beforeEach(() => { + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); + vi.spyOn(Logger, "fatal").mockImplementation(() => undefined); + client = new Redis(env.REDIS_URL); + }); + + afterEach(() => { + client.disconnect(); + vi.useRealTimers(); + vi.restoreAllMocks(); + }); + + it("exits when the connection does not recover", () => { + client.status = "reconnecting"; + client.emit("reconnecting"); + vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT); + + expect(Logger.fatal).toHaveBeenCalledWith( + "Redis connection unavailable", + expect.any(Error), + expect.objectContaining({ status: "reconnecting" }) + ); + }); + + it("does not exit when the connection recovers", () => { + client.status = "reconnecting"; + client.emit("reconnecting"); + vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT / 2); + + client.status = "ready"; + client.emit("ready"); + vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT); + + expect(Logger.fatal).not.toHaveBeenCalled(); + }); + + it("does not exit after the connection is closed", () => { + client.status = "reconnecting"; + client.emit("reconnecting"); + + client.status = "end"; + client.emit("end"); + vi.advanceTimersByTime(env.REDIS_UNAVAILABLE_TIMEOUT); + + expect(Logger.fatal).not.toHaveBeenCalled(); + }); +}); diff --git a/server/storage/redis.ts b/server/storage/redis.ts index 8324de9af6..bbdbf3a215 100644 --- a/server/storage/redis.ts +++ b/server/storage/redis.ts @@ -77,13 +77,47 @@ export default class RedisAdapter extends Redis { this.setMaxListeners(100); this.on("error", (err) => { - if (err.name === "MaxRetriesPerRequestError") { - Logger.fatal("Redis maximum retries exceeded", err); - } else { - Logger.error("Redis error", err); - } + Logger.error("Redis error", err); }); + // Exit when the connection stays unavailable, as a restart can recover a + // process that is unable to reconnect by itself. + let unavailableTimeout: NodeJS.Timeout | undefined; + + const startUnavailableTimeout = () => { + if (unavailableTimeout) { + return; + } + + unavailableTimeout = setTimeout(() => { + unavailableTimeout = undefined; + if (this.status === "ready" || this.status === "end") { + return; + } + + Logger.fatal( + "Redis connection unavailable", + new Error( + `Redis connection not ready after ${env.REDIS_UNAVAILABLE_TIMEOUT}ms` + ), + { connectionName, status: this.status } + ); + }, env.REDIS_UNAVAILABLE_TIMEOUT); + + // Don't keep the Node event loop alive solely for the timeout. + unavailableTimeout.unref(); + }; + + const clearUnavailableTimeout = () => { + clearTimeout(unavailableTimeout); + unavailableTimeout = undefined; + }; + + this.on("connecting", startUnavailableTimeout); + this.on("reconnecting", startUnavailableTimeout); + this.on("ready", clearUnavailableTimeout); + this.on("end", clearUnavailableTimeout); + // Skip the healthcheck on connections reserved for blocking or pub/sub // operations (signalled via maxRetriesPerRequest: null). A PING issued on // those connections queues behind the in-flight blocking command and would