diff --git a/apps/api/src/archive/service.test.ts b/apps/api/src/archive/service.test.ts index 2341b9a..09d5b8d 100644 --- a/apps/api/src/archive/service.test.ts +++ b/apps/api/src/archive/service.test.ts @@ -107,6 +107,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 Postgres terminates the connection (57P01, BIT-32473)", async () => { + const log = makeLog(); + const calls: string[] = []; + const db = makeDb(async () => { + calls.push("findUnique"); + throw Object.assign( + new Error("Invalid `prisma.sportsFixture.findMany()` invocation:\n\nDatabase error. Code: `57P01`. Message: `terminating connection due to administrator command`"), + { code: "P2039" }, + ); + }); + 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" && /connection terminated/.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 () => { @@ -159,4 +177,32 @@ describe("takeOffFutureGames stops and downgrades an unreachable database (BIT-3 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); }); + + it("logs at info and stops the batch when Postgres terminates the connection (57P01, BIT-32473)", 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.sportsFixture.findMany()` invocation:\n\nDatabase error. Code: `57P01`. Message: `terminating connection due to administrator command`"), + { code: "P2039" }, + ); + }, + }, + }; + 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" && /connection terminated/.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 a2238f3..f3207e6 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -14,7 +14,7 @@ import type { CloudFolder, CloudSource } from "../generated/prisma/client.js"; import type { IngestLogger, IngestService } from "../ingest/service.js"; import { ingestWindow } from "../library/events.js"; import { waitUntil } from "../lib/idle.js"; -import { isPrismaServerUnreachable } from "../lib/pg.js"; +import { isPostgresAdminShutdown, isPrismaServerUnreachable } from "../lib/pg.js"; import type { LocalStorage } from "../storage/storage.js"; import { CloudClient, cleanBase, pollLogin, startLogin, type CloudEntry } from "./nextcloud.js"; import { @@ -625,6 +625,13 @@ export class ArchiveService { log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: future-game check stopped, database unreachable"); break; } + // Postgres killed the pooled client mid-query (a restart, `pg_terminate_backend`, error 57P01): the next + // folder would hit the same terminated connection until the pool reclaims a fresh client, so stop the batch + // and let the next verifyAll retry — a warn per folder pages the error loop for a transient blip (BIT-32473). + if (isPostgresAdminShutdown(err)) { + log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: future-game check stopped, database connection terminated"); + break; + } log.warn({ err, folderId: folder.id, path: folder.path }, "archive: gallery on a game still to come not moved"); } } @@ -676,6 +683,13 @@ export class ArchiveService { log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: gallery check stopped, database unreachable"); break; } + // Postgres killed the pooled client mid-query (a restart, `pg_terminate_backend`, error 57P01): the next + // folder would hit the same terminated connection until the pool reclaims a fresh client, so stop the batch + // and let the next verifyAll retry — a warn per folder pages the error loop for a transient blip (BIT-32473). + if (isPostgresAdminShutdown(err)) { + log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: gallery check stopped, database connection terminated"); + break; + } log.warn({ err, folderId: folder.id, path: folder.path }, "archive: gallery not checked"); report?.errors.push({ path: folder.path, error: err instanceof Error ? err.message : String(err) }); }