diff --git a/apps/api/src/archive/service.test.ts b/apps/api/src/archive/service.test.ts index 22d1ab1..47f2eff 100644 --- a/apps/api/src/archive/service.test.ts +++ b/apps/api/src/archive/service.test.ts @@ -125,6 +125,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 is at max_connections (P2037, BIT-33550)", 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\nToo many database connections opened: remaining connection slots are reserved for roles with the SUPERUSER attribute"), + { code: "P2037" }, + ); + }); + 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" && /too many database connections/.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 () => { @@ -205,6 +223,33 @@ describe("takeOffFutureGames stops and downgrades an unreachable database (BIT-3 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); }); + it("logs at info and stops the batch when Postgres is at max_connections (P2037, BIT-33550)", 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\nToo many database connections opened: remaining connection slots are reserved for roles with the SUPERUSER attribute"), + { code: "P2037" }, + ); + }, + }, + }; + 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" && /too many database connections/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn" && /not moved/.test(c.msg))).toBe(false); + }); }); describe("walk recovers from a P2002 race on cloudFile.create (BIT-33532)", () => { diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index 3dfde94..b05f38a 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 { isPostgresAdminShutdown, isPrismaServerUnreachable } from "../lib/pg.js"; +import { isPostgresAdminShutdown, isPrismaServerUnreachable, isPrismaTooManyConnections } from "../lib/pg.js"; import type { LocalStorage } from "../storage/storage.js"; import { CloudClient, cleanBase, pollLogin, startLogin, type CloudEntry } from "./nextcloud.js"; import { @@ -658,6 +658,13 @@ export class ArchiveService { log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: future-game check stopped, database connection terminated"); break; } + // The Postgres server is at max_connections and only SUPERUSER slots remain (P2037): every remaining folder + // would fail the same way, so stop the batch and let the next verifyAll retry once a slot frees — a warn per + // folder pages the error loop for a transient blip (BIT-33550). + if (isPrismaTooManyConnections(err)) { + log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: future-game check stopped, too many database connections"); + break; + } log.warn({ err, folderId: folder.id, path: folder.path }, "archive: gallery on a game still to come not moved"); } } @@ -716,6 +723,13 @@ export class ArchiveService { log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: gallery check stopped, database connection terminated"); break; } + // The Postgres server is at max_connections and only SUPERUSER slots remain (P2037): every remaining folder + // would fail the same way, so stop this source's batch and let the next verifyAll retry once a slot frees — + // logging a warn per folder pages the error loop for a transient blip (BIT-33550). + if (isPrismaTooManyConnections(err)) { + log.info({ err: (err as Error).message, folderId: folder.id, path: folder.path }, "archive: gallery check stopped, too many database connections"); + 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) }); }