Files
photoreport/infra/pm2/api.config.cjs
CI Runner c28aae2740
ci / typescript (pull_request) Failing after 7s
ci / rust (pull_request) Successful in 9m16s
fix(api): drain worker jobs before Prisma pool disconnect (BIT-31189)
PM2 max_memory SIGINT was ending the Prisma/pg pool while archive and
delivery jobs still queried it, flooding "Cannot use a pool after calling
end". Await idle on all loop workers before $disconnect, and raise the
worker memory/kill_timeout so burst indexing does not restart every few
minutes.

Paperclip-Agent: David Santos (urlKey founding-engineer; id 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376)
Paperclip-Issue: BIT-31189
Paperclip-Run: 61448079-9293-4b3b-989d-b5857bf058c1
2026-10-15 01:30:06 +01:00

49 lines
2.1 KiB
JavaScript

// PM2 processes for the API and the background worker. Environment comes from apps/api/.env (never written by deploys)
// plus APP_VERSION set by infra/deploy/deploy.sh.
const path = require("node:path");
const fs = require("node:fs");
// Container-local override for exec_mode/instances, outside the repo so `git reset --hard`
// in deploy.sh never touches it. Lets a host opt out of cluster mode (e.g. a kernel/LXC
// combination where native addons crash under node's cluster fork) without changing the
// default for every other deployment.
let apiOverride = {};
try {
apiOverride = JSON.parse(fs.readFileSync("/etc/photoreport/pm2-api.override.json", "utf8"));
} catch {}
module.exports = {
apps: [
{
name: "photoreport-api",
cwd: path.join(__dirname, "../../apps/api"),
script: "dist/server.js",
node_args: "--env-file=.env",
instances: apiOverride.instances ?? 2,
exec_mode: apiOverride.exec_mode ?? "cluster",
max_memory_restart: "1G",
kill_timeout: 10000,
// Keep retrying with a growing pause (up to 15 s) instead of giving up after 15 quick crashes, e.g. while
// the database container is still starting after a host reboot.
exp_backoff_restart_delay: 200,
},
{
// One worker per installation (FTP ingest + delivery queue): it owns the drop folder, so never clustered.
name: "photoreport-worker",
cwd: path.join(__dirname, "../../apps/api"),
script: "dist/worker.js",
node_args: "--env-file=.env",
instances: 1,
exec_mode: "fork",
// Archive burst (many downloads + face processes) on 48-vCPU hosts routinely exceeds 2G; a too-low
// cap caused SIGINT restart loops and Prisma "pool after end" noise during unclean disconnect (BIT-31189).
max_memory_restart: "6G",
// Must exceed worker drain window (25s) so in-flight jobs finish before PM2 SIGKILLs.
kill_timeout: 45000,
// Keep retrying with a growing pause (up to 15 s) instead of giving up after 15 quick crashes, e.g. while
// the database container is still starting after a host reboot.
exp_backoff_restart_delay: 200,
},
],
};