Skip to content

fix(platform): log pg-boss's failed polls once per database outage - #3548

Merged
yannickmonney merged 1 commit into
mainfrom
fix/quiet-pg-boss-outage-polls
Sep 27, 2026
Merged

yannickmonney merged 1 commit into
mainfrom
fix/quiet-pg-boss-outage-polls

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Summary

During a database outage pg-boss logged every failed worker poll as a full error dump. Now an unavailable database logs one warn line per outage in the job queue, and the line is re-armed once a poll succeeds. Every other pg-boss error is still logged in full. This is the follow-up #3506 left open (task TALE-51). There is no GitHub issue.

Problem

  • pg-boss emits every failure of its own database work as an error event. That covers each queue's worker poll (every 2 s, about 60 queues), the queue cache, the cron clock and its pool's idle clients. createBoss passed each event to console.error.
  • A 20 s restart on origin/main produced 677 events and about 11,850 log lines (measured below). Nothing reached error tracking, but the flood buried the lines that matter.
  • The shape pg-boss emits hides part of the outage from isDatabaseUnavailable:
    • A worker appends (Queue: <name>, Worker: <id>) to the error's message.
    • It then re-emits a plain copy of the error's fields, with queue and worker added.
    • The copy keeps code, so coded errors still classify. But node-postgres's code-less Connection terminated unexpectedly is recognised only by its exact message, so the suffix hides it.
    • describeDatabaseError printed any non-Error value as [object Object].

Change

  • backend/jobs/boss.ts: createBoss sends pg-boss's error event through logErrors:
    • It strips a worker's suffix, then classifies with isDatabaseUnavailable(…, { fromDatabase: true }). pg-boss's errors all come from its own node-postgres pool, so a bare socket error is the database's.
    • The first unavailable failure logs [backend] pg-boss: database unavailable, polls fail quietly until one succeeds: <code> <message>. Later failures stay quiet until a worker's lastFetchedOn is newer than that line (getWipData({ includeInternal: true })), which re-arms it.
    • The cron clock's internal worker counts too, so the api role, which runs no queue worker, also re-arms.
    • Any other error keeps today's console.error('[backend] pg-boss error:', error).
  • backend/db/unavailable.ts: describeDatabaseError reads code and message off any object, including a plain copy. The module comment names the new consumer.
  • Manual layer:
    • box PERF-B6 (job-queue log through two database restarts), with the suite's box count raised to 18;
    • a pins.md row explaining why the box exists;
    • an automation.md row pointing at the unit test.
  • Docs (EN/DE/FR): the observability page's restart paragraph now says the job queue logs one warning per outage, not one per failed poll.

Proof

Unit test, backend/jobs/boss.test.ts (new). It runs on the real PgBoss emitter returned by createBoss. Its errors come from real node-postgres connection failures: a refused connection, a server that hangs up mid-handshake (Connection terminated unexpectedly), and a fake server that answers FATAL 57P03 the database system is starting up. The test also uses a real pg.DatabaseError 57P01. Each error is shaped the way a pg-boss 12 worker re-emits it.

  • 601 idle-client and worker failures plus a queue-cache copy produce one warn line and no console.error. A 42P01 that follows is still logged in full.
  • A code-less hang-up is recognised through the worker suffix.
  • Pool exhaustion (timeout exceeded when trying to connect) and 28P01 are still logged in full.
  • The line stays quiet while the latest poll predates the outage, and logs again after a poll succeeds.

Without the change, three of the four tests fail (the burst logs no warning, the hang-up hits console.error, and the re-arm test sees no warning). The "logged in full" case passes on both, as a guard.

db/unavailable.test.ts gains a case for describing a plain copy.

End to end, the #3506 restart scenario.

  • Setup: the backend ran with ROLE=all (node … backend/main.ts) against a real tale-db container. Each restart was docker stop (SIGINT, a fast shutdown), 20 s down, then docker start, and this happened twice. Lines were counted from the stop until 15 s after the database was back.
origin/main code this branch
restart 1 677 pg-boss error: events, 11,850 log lines 1 line: pg-boss: database unavailable, … 57P01 terminating connection due to administrator command
restart 2 673 events, 11,775 log lines 1 line (re-armed after the first recovery)

The process stayed up in both runs.

Checklist

  • bun run check passes (format, lint, typecheck, all tests). Partly; CI runs the rest. These passed locally:
    • format, via the pre-commit hook;
    • type-aware oxlint on the four changed TS files;
    • tsc on a config rooted at the changed files plus backend/main.ts;
    • the tests of the changed files and of unavailable.ts's importers: 32 files, 266 tests (jobs/, db/unavailable, realtime/sse, auth/, error-reporting, rest/v1);
    • the docs suite: 34 files.
  • bun run lint:sast passes (Opengrep) on the changed files: 0 findings.
  • Translations: N/A. No catalog key changed; the log line is an operator log, English like every other.
  • Docs updated in docs/{en,de,fr}/: one sentence on the observability page.
  • README.md / README.de.md / README.fr.md: N/A, nothing they describe changed.

bun run lint:manual passes, and so does check-guide on performance.md.

Test plan

  • bunx vitest --run --project server backend/jobs/boss.test.ts backend/db/unavailable.test.ts in services/platform.
  • Manual PERF-B6:
    1. Follow docker compose logs -f backend-worker backend-api.
    2. Run docker compose restart db, wait for healthy, then restart it again.
    3. Expect exactly one pg-boss: database unavailable … warning per container per restart, and no pg-boss error: dump.
    4. Afterwards a new chat gets its generated title.

Open

  • pg-boss's LISTEN connection still logs LISTEN/NOTIFY connection ended in full if a connection drops without a FATAL message. That is one line per drop and says nothing about the database's state. A fast shutdown sends 57P01 first, which is covered.
  • If pg-boss changes its worker suffix format, a code-less hang-up would be logged in full again. Coded errors (57P01/57P03, ECONNREFUSED, …) would still classify.

pg-boss emits every failed poll of every queue's worker as an error
event, and createBoss dumped each one in full: 677 events, some 11,800
log lines, in one 20 s database restart.

An unavailable database (isDatabaseUnavailable) is now one warn line
per outage, re-armed once a pg-boss worker's poll succeeds; any other
pg-boss error is still logged in full. A worker's failure arrives as a
plain copy of the error with its queue appended to the message, so the
classifier reads through that suffix and describeDatabaseError reads a
plain copy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant