fix(worker): downgrade P2037 to info in cloud target ensure at startup (BIT-33558)
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 9m22s
ci / typescript (pull_request) Successful in 14m46s

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
This commit is contained in:
nelson-baptista committed 2026-12-04 14:00:54 +00:00
1 parent 6cd8ba0b95
commit baa22f7ea0
2 files changed
+36 -3

No files matched your search

+26 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"; 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 { class PrismaLike extends Error {
constructor(readonly code: string, message: string) { 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", () => { describe("isPrismaTransactionTimeout", () => {
it("matches a PrismaClientKnownRequestError with code P2028 (BIT-32083)", () => { it("matches a PrismaClientKnownRequestError with code P2028 (BIT-32083)", () => {
const err = new PrismaLike( const err = new PrismaLike(
+10 -2
View File
@@ -17,6 +17,7 @@ import { DevelopService, RenderWorker } from "./develop/service.js";
import { FaceProcess, GrowingFacePool } from "./faces/process.js"; import { FaceProcess, GrowingFacePool } from "./faces/process.js";
import { AutopilotService, AutopilotWorker } from "./autopilot/service.js"; import { AutopilotService, AutopilotWorker } from "./autopilot/service.js";
import { ensureCloudM19Target, ensureCloudW17Target } from "./delivery/fpa-m19.js"; import { ensureCloudM19Target, ensureCloudW17Target } from "./delivery/fpa-m19.js";
import { isPrismaTooManyConnections } from "./lib/pg.js";
import { jogaClientFromConfig } from "./sports/joga.js"; import { jogaClientFromConfig } from "./sports/joga.js";
import { SportsService, SportsWorker } from "./sports/service.js"; import { SportsService, SportsWorker } from "./sports/service.js";
import { ArchiveService, ArchiveWorker, localDirs } from "./archive/service.js"; import { ArchiveService, ArchiveWorker, localDirs } from "./archive/service.js";
@@ -176,8 +177,15 @@ const sports = config.SPORTS_SYNC === "on"
}) })
: undefined; : undefined;
ensureCloudM19Target(db).catch((err) => log.warn({ err }, "delivery: Cloud M19 profile not created")); // Pool exhaustion (P2037) during a deploy overlap is transient: the next worker restart retries these
ensureCloudW17Target(db).catch((err) => log.warn({ err }, "delivery: Cloud W17 profile not created")); // one-shot targets. Log at info so the reporter hook (warn+) does not page the error loop (BIT-33558).
const ensureCloudTarget = (fn: Promise<number>, 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(); scanner.start();
if (secrets) archive.start(); if (secrets) archive.start();
autopilot.start(); autopilot.start();