diff --git a/apps/api/src/faces/service.ts b/apps/api/src/faces/service.ts index d002023..f99d113 100644 --- a/apps/api/src/faces/service.ts +++ b/apps/api/src/faces/service.ts @@ -6,6 +6,7 @@ import { DEFAULT_CODE_LIST } from "../library/iptc.js"; import type { FaceGroup, Person } from "../generated/prisma/client.js"; import type { IngestLogger } from "../ingest/service.js"; import { waitUntil } from "../lib/idle.js"; +import { isPostgresAdminShutdown } from "../lib/pg.js"; import type { Broadcaster } from "../realtime.js"; import type { LocalStorage } from "../storage/storage.js"; import { normLabel } from "../jersey/service.js"; @@ -1530,7 +1531,10 @@ export class FaceWorker { } } } catch (err) { - this.opts.log.warn({ err }, "faces: poll failed"); + // A DB restart or `pg_terminate_backend` (57P01) takes the pooled client mid-`$queryRaw` (claimWhere); the next + // tick re-claims a fresh one, so log at info and let the loop continue instead of paging the error loop (BIT-32450). + if (isPostgresAdminShutdown(err)) this.opts.log.info({ err: (err as Error).message }, "faces: poll skipped, db connection was reset"); + else this.opts.log.warn({ err }, "faces: poll failed"); } finally { this.ticking = false; if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs); diff --git a/apps/api/src/faces/worker.test.ts b/apps/api/src/faces/worker.test.ts index 35c4fd6..a1ca251 100644 --- a/apps/api/src/faces/worker.test.ts +++ b/apps/api/src/faces/worker.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest"; import { waitUntil } from "../lib/idle.js"; +import { isPostgresAdminShutdown } from "../lib/pg.js"; import { FaceWorker, type FaceService } from "./service.js"; const quiet = { info() {}, warn() {}, error() {}, debug() {} } as never; @@ -62,3 +63,76 @@ describe("the face worker", () => { expect(started).toEqual([]); }); }); + +describe("isPostgresAdminShutdown", () => { + it("matches the 57P01 admin-shutdown message Prisma surfaces from claimWhere's raw query (BIT-32450)", () => { + const err = new Error( + "Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `57P01`. Message: `terminating connection due to administrator command`", + ); + expect(isPostgresAdminShutdown(err)).toBe(true); + }); + + it("does not match an unrelated query error", () => { + expect(isPostgresAdminShutdown(new Error("relation \"photos\" does not exist"))).toBe(false); + expect(isPostgresAdminShutdown(null)).toBe(false); + expect(isPostgresAdminShutdown("string")).toBe(false); + }); +}); + +describe("FaceWorker poll error handling", () => { + type Level = "info" | "warn"; + const makeLog = (): { info: (obj: unknown, msg: string) => void; warn: (obj: unknown, msg: string) => void; error: () => void; debug: () => void; calls: { level: Level; msg: string }[] } => { + const calls: { level: Level; msg: string }[] = []; + return { + calls, + info: (obj, msg) => calls.push({ level: "info", msg }), + warn: (obj, msg) => calls.push({ level: "warn", msg }), + error: () => {}, + debug: () => {}, + }; + }; + + const makeService = (claimErr: unknown): Pick => ({ + requeueStale: async () => 0, + refreshLineupEvents: async () => 0, + queueMissed: async () => { + throw claimErr; + }, + requeueFailed: async () => 0, + requeueVideos: async () => 0, + claimVideos: async () => [], + claim: async () => [], + claimReferences: async () => [], + processReference: async () => {}, + process: async () => {}, + processVideo: async () => {}, + stopVideos: () => {}, + }); + + it("logs a 57P01 admin shutdown at info, not warn, and keeps looping (BIT-32450)", async () => { + const log = makeLog(); + const worker = new FaceWorker(makeService(new Error("terminating connection due to administrator command (57P01)")) as unknown as FaceService, { + concurrency: 1, + intervalMs: 60_000, + log: log as unknown as never, + }); + worker.start(); + await new Promise((r) => setTimeout(r, 20)); + await worker.stop(); + expect(log.calls.some((c) => c.level === "info" && /db connection was reset/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn" && /poll failed/.test(c.msg))).toBe(false); + }); + + it("still warns on a real poll failure", async () => { + const log = makeLog(); + const worker = new FaceWorker(makeService(new Error("relation photos does not exist")) as unknown as FaceService, { + concurrency: 1, + intervalMs: 60_000, + log: log as unknown as never, + }); + worker.start(); + await new Promise((r) => setTimeout(r, 20)); + await worker.stop(); + expect(log.calls.some((c) => c.level === "warn" && /poll failed/.test(c.msg))).toBe(true); + }); +});