diff --git a/apps/api/src/archive/service.test.ts b/apps/api/src/archive/service.test.ts index 72e2c09..2341b9a 100644 --- a/apps/api/src/archive/service.test.ts +++ b/apps/api/src/archive/service.test.ts @@ -89,6 +89,24 @@ describe("rematch stops and downgrades a P1001 unreachable database (BIT-32462)" expect(log.calls.some((c) => c.level === "warn" && /gallery not checked/.test(c.msg))).toBe(false); }); + it("logs at info and stops the batch when the database is unreachable (P2010 raw-query wrapper, BIT-32469)", async () => { + const log = makeLog(); + const calls: string[] = []; + const db = makeDb(async () => { + calls.push("findUnique"); + throw Object.assign( + new Error("Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `N/A`. Message: `Can't reach database server at 127.0.0.1:5432`"), + { code: "P2010" }, + ); + }); + const service = new ArchiveService({ db, log } as never); + const moved = await (service as unknown as { rematch: (sourceId: string) => Promise }).rematch("src-1"); + expect(calls).toEqual(["findUnique"]); + expect(moved).toBe(0); + expect(log.calls.some((c) => c.level === "info" && /database unreachable/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn" && /gallery not checked/.test(c.msg))).toBe(false); + }); + it("still warns on a real per-folder error", async () => { const log = makeLog(); const db = makeDb(async () => { @@ -100,3 +118,45 @@ describe("rematch stops and downgrades a P1001 unreachable database (BIT-32462)" expect(log.calls.some((c) => c.level === "info" && /database unreachable/.test(c.msg))).toBe(false); }); }); + +describe("takeOffFutureGames stops and downgrades an unreachable database (BIT-32469)", () => { + type Level = "info" | "warn"; + const makeLog = (): { info: (obj: unknown, msg: string) => void; warn: (obj: unknown, msg: string) => void; error: () => void; debug: () => void; calls: { level: Level; msg: string }[] } => { + const calls: { level: Level; msg: string }[] = []; + return { + calls, + info: (obj, msg) => calls.push({ level: "info", msg }), + warn: (obj, msg) => calls.push({ level: "warn", msg }), + error: () => {}, + debug: () => {}, + }; + }; + + it("logs at info and stops the batch when the database is unreachable (P2010, BIT-32469)", async () => { + const log = makeLog(); + const calls: string[] = []; + const db = { + cloudFolder: { + findMany: async () => [ + { id: "f-1", path: "/Future Game", eventId: "evt-1", matchedBy: "NAME", fixtureRejected: false, source: { id: "src-1" } }, + { id: "f-2", path: "/Future Game 2", eventId: "evt-2", matchedBy: "NAME", fixtureRejected: false, source: { id: "src-1" } }, + ], + }, + event: { + findUnique: async () => { + calls.push("findUnique"); + throw Object.assign( + new Error("Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `N/A`. Message: `Can't reach database server at 127.0.0.1:5432`"), + { code: "P2010" }, + ); + }, + }, + }; + const service = new ArchiveService({ db, log } as never); + const moved = await (service as unknown as { takeOffFutureGames: () => Promise }).takeOffFutureGames(); + expect(calls).toEqual(["findUnique"]); + expect(moved).toBe(0); + expect(log.calls.some((c) => c.level === "info" && /database unreachable/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn" && /not moved/.test(c.msg))).toBe(false); + }); +}); diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index 6d6d280..a2238f3 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -618,6 +618,13 @@ export class ArchiveService { moved++; log.warn({ folderId: folder.id, path: folder.path }, "archive: gallery taken off a game still to come"); } catch (err) { + // The database server is unreachable (P1001/P2010 wrapping "Can't reach database server": a restart, a + // downed route). Every remaining folder would fail the same way, so stop the batch and let the next + // verifyAll retry — logging a warn per folder pages the error loop for a transient blip (BIT-32469). + if (isPrismaServerUnreachable(err)) { + log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: future-game check stopped, database unreachable"); + break; + } log.warn({ err, folderId: folder.id, path: folder.path }, "archive: gallery on a game still to come not moved"); } } diff --git a/apps/api/src/lib/pg.test.ts b/apps/api/src/lib/pg.test.ts index daff231..ff5a291 100644 --- a/apps/api/src/lib/pg.test.ts +++ b/apps/api/src/lib/pg.test.ts @@ -51,6 +51,14 @@ describe("isPrismaServerUnreachable", () => { expect(isPrismaServerUnreachable(err)).toBe(true); }); + it("matches a P2010 raw-query failure wrapping the unreachable message (BIT-32469)", () => { + const err = new PrismaLike( + "P2010", + "Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `N/A`. Message: `Can't reach database server at 127.0.0.1:5432`", + ); + expect(isPrismaServerUnreachable(err)).toBe(true); + }); + it("matches the unreachable message without the code", () => { expect(isPrismaServerUnreachable(new Error("Can't reach database server at pr-db:5432"))).toBe(true); });