From baa22f7ea07ff5c02f7a385818555b14465148d6 Mon Sep 17 00:00:00 2001 From: nelson-baptista Date: Fri, 4 Dec 2026 14:00:54 +0000 Subject: [PATCH] fix(worker): downgrade P2037 to info in cloud target ensure at startup (BIT-33558) The ensureCloudM19Target and ensureCloudW17Target calls fire concurrently at worker startup. During a deploy overlap (two workers live briefly), the Postgres server hits max_connections and these one-shot queries get P2037. The .catch() handler logged at warn with the raw error, which triggered the reporter's logHooks and paged the error loop. Follow the same pattern as develop/sports/archive workers: check isPrismaTooManyConnections and log at info (below the reporter's warn threshold) so the error loop stays quiet. The targets retry on the next worker restart. Also add tests for isPrismaTooManyConnections covering the P2037 code, the bare message, the P2010/53300 raw-query variant, and negative cases. Paperclip-Issue: BIT-33558 --- apps/api/src/lib/pg.test.ts | 27 ++++++++++++++++++++++++++- apps/api/src/worker.ts | 12 ++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) 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(); -- 2.47.3