diff --git a/apps/api/src/lib/pg.test.ts b/apps/api/src/lib/pg.test.ts index eae3abf..9784442 100644 --- a/apps/api/src/lib/pg.test.ts +++ b/apps/api/src/lib/pg.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { isPostgresAdminShutdown, isPgClientNotQueryable, isPrismaConnectionClosed, isPrismaServerUnreachable, isPrismaTransactionClosed, isPrismaTransactionTimeout, retryOnConnectionClosed } from "./pg.js"; +import { isPostgresAdminShutdown, isPgClientNotQueryable, isPrismaConnectionClosed, isPrismaServerUnreachable, isPrismaTooManyConnections, isPrismaTransactionClosed, isPrismaTransactionTimeout, retryOnConnectionClosed } from "./pg.js"; class PrismaLike extends Error { constructor(readonly code: string, message: string) { @@ -90,6 +90,31 @@ describe("isPrismaServerUnreachable", () => { }); }); +describe("isPrismaTooManyConnections", () => { + it("matches a PrismaClientKnownRequestError with code P2037 (BIT-33558)", () => { + const err = new PrismaLike( + "P2037", + "Invalid `prisma.deliveryTarget.findMany()` invocation:\n\nToo many database connections opened: remaining connection slots are reserved for roles with the SUPERUSER attribute", + ); + expect(isPrismaTooManyConnections(err)).toBe(true); + }); + + it("matches the 'remaining connection slots are reserved' message without the code", () => { + expect(isPrismaTooManyConnections(new Error("Too many database connections opened: remaining connection slots are reserved for roles with the SUPERUSER attribute"))).toBe(true); + }); + + it("matches a P2010 raw-query failure wrapping SQLSTATE 53300 (BIT-33549)", () => { + const err = new PrismaLike("P2010", "Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `53300`. Message: `sorry, too many clients already`"); + expect(isPrismaTooManyConnections(err)).toBe(true); + }); + + it("does not match an unrelated query error", () => { + expect(isPrismaTooManyConnections(new PrismaLike("P2002", "Unique constraint failed"))).toBe(false); + expect(isPrismaTooManyConnections(null)).toBe(false); + expect(isPrismaTooManyConnections("string")).toBe(false); + }); +}); + describe("isPrismaTransactionTimeout", () => { it("matches a PrismaClientKnownRequestError with code P2028 (BIT-32083)", () => { const err = new PrismaLike( diff --git a/apps/api/src/worker.ts b/apps/api/src/worker.ts index b9decdb..408d62c 100644 --- a/apps/api/src/worker.ts +++ b/apps/api/src/worker.ts @@ -17,6 +17,7 @@ import { DevelopService, RenderWorker } from "./develop/service.js"; import { FaceProcess, GrowingFacePool } from "./faces/process.js"; import { AutopilotService, AutopilotWorker } from "./autopilot/service.js"; import { ensureCloudM19Target, ensureCloudW17Target } from "./delivery/fpa-m19.js"; +import { isPrismaTooManyConnections } from "./lib/pg.js"; import { jogaClientFromConfig } from "./sports/joga.js"; import { SportsService, SportsWorker } from "./sports/service.js"; import { ArchiveService, ArchiveWorker, localDirs } from "./archive/service.js"; @@ -176,8 +177,15 @@ const sports = config.SPORTS_SYNC === "on" }) : undefined; -ensureCloudM19Target(db).catch((err) => log.warn({ err }, "delivery: Cloud M19 profile not created")); -ensureCloudW17Target(db).catch((err) => log.warn({ err }, "delivery: Cloud W17 profile not created")); +// Pool exhaustion (P2037) during a deploy overlap is transient: the next worker restart retries these +// one-shot targets. Log at info so the reporter hook (warn+) does not page the error loop (BIT-33558). +const ensureCloudTarget = (fn: Promise, name: string) => + fn.catch((err) => { + if (isPrismaTooManyConnections(err)) log.info({ err: (err as Error).message }, `delivery: ${name} profile not created, too many database connections`); + else log.warn({ err }, `delivery: ${name} profile not created`); + }); +ensureCloudTarget(ensureCloudM19Target(db), "Cloud M19"); +ensureCloudTarget(ensureCloudW17Target(db), "Cloud W17"); scanner.start(); if (secrets) archive.start(); autopilot.start();