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
This commit is contained in:
1 parent
4741aa3c53
commit
c28aae2740
13 files changed
+163
-51
No files matched your search
@@ -11,6 +11,7 @@ import type { SecretBox } from "../delivery/secrets.js";
|
||||
import type { CloudFolder, CloudSource } from "../generated/prisma/client.js";
|
||||
import type { IngestLogger, IngestService } from "../ingest/service.js";
|
||||
import { ingestWindow } from "../library/events.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import { CloudClient, cleanBase, pollLogin, startLogin, type CloudEntry } from "./nextcloud.js";
|
||||
import { describeFolder, type FolderGame, isInitials, isTeamName, lisbonDay, parseFolder, pickFixture } from "./match.js";
|
||||
|
||||
@@ -807,6 +808,7 @@ export class ArchiveWorker {
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private stopped = false;
|
||||
private scanning = false;
|
||||
private ticking = false;
|
||||
|
||||
constructor(
|
||||
private readonly service: ArchiveService,
|
||||
@@ -827,33 +829,38 @@ export class ArchiveWorker {
|
||||
.then((n) => void (n && this.opts.log.info({ count: n }, "archive: re-queued images")))
|
||||
.catch((err) => this.opts.log.warn({ err }, "archive: re-queue failed"));
|
||||
const tick = async () => {
|
||||
await ready;
|
||||
this.ticking = true;
|
||||
try {
|
||||
if (!this.scanning) {
|
||||
this.scanning = true;
|
||||
// Walking runs beside the indexing (a big first walk should not hold the queue back).
|
||||
void this.service
|
||||
.scanDue()
|
||||
.catch((err) => this.opts.log.warn({ err }, "archive: scan failed"))
|
||||
.finally(() => {
|
||||
this.scanning = false;
|
||||
await ready;
|
||||
try {
|
||||
if (!this.scanning) {
|
||||
this.scanning = true;
|
||||
// Walking runs beside the indexing (a big first walk should not hold the queue back).
|
||||
void this.service
|
||||
.scanDue()
|
||||
.catch((err) => this.opts.log.warn({ err }, "archive: scan failed"))
|
||||
.finally(() => {
|
||||
this.scanning = false;
|
||||
});
|
||||
}
|
||||
// Bulk work: less of it while the machine is busy with live games, all of it when quiet.
|
||||
const burst = !!this.opts.burst && (await this.backlog()) > BURST_BACKLOG;
|
||||
const full = burst ? this.opts.burst! : this.opts.concurrency;
|
||||
const allowed = this.opts.pressure ? this.opts.pressure.allow(full, burst) : full;
|
||||
const ids = await this.service.claim(allowed - this.inFlight);
|
||||
for (const id of ids) {
|
||||
this.inFlight++;
|
||||
void this.service.index(id).finally(() => {
|
||||
this.inFlight--;
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "archive: poll failed");
|
||||
}
|
||||
// Bulk work: less of it while the machine is busy with live games, all of it when quiet.
|
||||
const burst = !!this.opts.burst && (await this.backlog()) > BURST_BACKLOG;
|
||||
const full = burst ? this.opts.burst! : this.opts.concurrency;
|
||||
const allowed = this.opts.pressure ? this.opts.pressure.allow(full, burst) : full;
|
||||
const ids = await this.service.claim(allowed - this.inFlight);
|
||||
for (const id of ids) {
|
||||
this.inFlight++;
|
||||
void this.service.index(id).finally(() => {
|
||||
this.inFlight--;
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "archive: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
@@ -870,8 +877,11 @@ export class ArchiveWorker {
|
||||
return this.backlogCount;
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
/** Stop claiming work and wait for in-flight index/scan jobs before the DB pool is closed. */
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking && !this.scanning && this.inFlight === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ inFlight: this.inFlight, scanning: this.scanning }, "archive: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { type Box, compose, frameScore, minCropWidth, peopleIn } from "../compos
|
||||
import type { SubjectDetector } from "../composition/detect.js";
|
||||
import { constrainRotated, tighter } from "../composition/geometry.js";
|
||||
import type { IngestLogger } from "../ingest/service.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import type { Broadcaster } from "../realtime.js";
|
||||
import type { LocalStorage } from "../storage/storage.js";
|
||||
import { CANDIDATE_SELECT, candidatesOf } from "./best.js";
|
||||
@@ -274,6 +275,7 @@ export class AutopilotWorker {
|
||||
private timer: ReturnType<typeof setTimeout> | undefined;
|
||||
private inFlight = 0;
|
||||
private stopped = false;
|
||||
private ticking = false;
|
||||
private lastDecide = 0;
|
||||
|
||||
constructor(
|
||||
@@ -284,6 +286,7 @@ export class AutopilotWorker {
|
||||
start(): void {
|
||||
void this.service.requeueStale();
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
const ids = await this.service.claim(this.opts.concurrency - this.inFlight);
|
||||
for (const id of ids) {
|
||||
@@ -298,14 +301,18 @@ export class AutopilotWorker {
|
||||
}
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "autopilot: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking && this.inFlight === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ inFlight: this.inFlight }, "autopilot: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { buildVars, createdStamp, eventName, PathTemplateError, renderFileName,
|
||||
import type { Db } from "../db.js";
|
||||
import type { DeliveryItem } from "../generated/prisma/client.js";
|
||||
import type { IngestLogger } from "../ingest/service.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import { eventInfoOf } from "../library/event-info.js";
|
||||
import { iptcForPhoto } from "../library/iptc.js";
|
||||
import type { Broadcaster } from "../realtime.js";
|
||||
@@ -407,6 +408,7 @@ export class DeliveryWorker {
|
||||
private inFlight = 0;
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private stopped = false;
|
||||
private ticking = false;
|
||||
|
||||
constructor(
|
||||
private readonly service: DeliveryService,
|
||||
@@ -416,12 +418,15 @@ export class DeliveryWorker {
|
||||
start(): void {
|
||||
void this.service.requeueStale().then((n) => n && this.opts.log.info({ count: n }, "delivery: re-queued stale items"));
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
await this.fill();
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "delivery: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
@@ -436,8 +441,10 @@ export class DeliveryWorker {
|
||||
}
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking && this.inFlight === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ inFlight: this.inFlight }, "delivery: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { type DevelopState, type EditCommand, type EditLogEntry, type PostEdits,
|
||||
import type { Db } from "../db.js";
|
||||
import { Prisma, type PhotoEdit } from "../generated/prisma/client.js";
|
||||
import type { IngestLogger } from "../ingest/service.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import { photoUrls } from "../library/serialize.js";
|
||||
import type { Broadcaster } from "../realtime.js";
|
||||
import { type LocalStorage, type UrlSigner, VARIANT_SIZE } from "../storage/storage.js";
|
||||
@@ -198,6 +199,7 @@ export class RenderWorker {
|
||||
private inFlight = 0;
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private stopped = false;
|
||||
private ticking = false;
|
||||
|
||||
constructor(
|
||||
private readonly service: DevelopService,
|
||||
@@ -207,6 +209,7 @@ export class RenderWorker {
|
||||
start(): void {
|
||||
void this.service.requeueStale();
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
for (const id of await this.service.claimRenders(this.opts.concurrency - this.inFlight)) {
|
||||
this.inFlight++;
|
||||
@@ -216,14 +219,18 @@ export class RenderWorker {
|
||||
}
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "develop: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking && this.inFlight === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ inFlight: this.inFlight }, "develop: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import type { Db } from "../db.js";
|
||||
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 type { Broadcaster } from "../realtime.js";
|
||||
import type { LocalStorage } from "../storage/storage.js";
|
||||
import { type Box, colourDistance, kitColour, numberBoxes, smallImage, torsoBox } from "../jersey/crop.js";
|
||||
@@ -532,6 +533,7 @@ export class FaceWorker {
|
||||
private inFlight = 0;
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private stopped = false;
|
||||
private ticking = false;
|
||||
|
||||
constructor(
|
||||
private readonly service: FaceService,
|
||||
@@ -542,6 +544,7 @@ export class FaceWorker {
|
||||
void this.service.requeueStale();
|
||||
let missedAt = 0;
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
if (Date.now() - missedAt > 60_000) {
|
||||
missedAt = Date.now();
|
||||
@@ -558,14 +561,18 @@ export class FaceWorker {
|
||||
}
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "faces: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking && this.inFlight === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ inFlight: this.inFlight }, "faces: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { readdir, stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import type { IngestLogger, IngestService } from "./service.js";
|
||||
|
||||
interface Seen {
|
||||
@@ -17,6 +18,7 @@ export class IngestScanner {
|
||||
private readonly busy = new Set<string>();
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private running = false;
|
||||
private stopped = false;
|
||||
|
||||
constructor(
|
||||
private readonly service: IngestService,
|
||||
@@ -36,13 +38,16 @@ export class IngestScanner {
|
||||
this.running = false;
|
||||
}
|
||||
}
|
||||
this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.running && this.busy.size === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ busy: this.busy.size }, "ingest: stop timed out with work still running");
|
||||
}
|
||||
|
||||
private async *walk(dir: string): AsyncGenerator<string> {
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Db } from "../db.js";
|
||||
import type { SecretBox } from "../delivery/secrets.js";
|
||||
import type { Invoice } from "../generated/prisma/client.js";
|
||||
import type { IngestLogger } from "../ingest/service.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import { lineNet } from "../shop/earnings.js";
|
||||
import { periodOf, periodRange } from "../shop/press.js";
|
||||
import { countryName, type DocumentInput, InvoiceXpress, InvoicingError } from "./invoicexpress.js";
|
||||
@@ -201,6 +202,7 @@ export class InvoicingService {
|
||||
export class InvoicingWorker {
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private stopped = false;
|
||||
private ticking = false;
|
||||
private lastMonthly = 0;
|
||||
|
||||
constructor(
|
||||
@@ -210,6 +212,7 @@ export class InvoicingWorker {
|
||||
|
||||
start(): void {
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
// Monthly press invoices: checked every hour, created once per account and month.
|
||||
if (Date.now() - this.lastMonthly > 3600_000) {
|
||||
@@ -220,14 +223,18 @@ export class InvoicingWorker {
|
||||
for (const id of await this.invoicing.claim(5)) await this.invoicing.issue(id);
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "invoicing: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({}, "invoicing: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Wait until `ok()` is true, or until `timeoutMs` elapses.
|
||||
* Used by worker shutdown to drain in-flight jobs before `$disconnect()` (BIT-31189).
|
||||
*/
|
||||
export async function waitUntil(
|
||||
ok: () => boolean,
|
||||
opts: { timeoutMs?: number; intervalMs?: number } = {},
|
||||
): Promise<boolean> {
|
||||
const timeoutMs = opts.timeoutMs ?? 25_000;
|
||||
const intervalMs = opts.intervalMs ?? 50;
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (!ok()) {
|
||||
if (Date.now() >= deadline) return false;
|
||||
await new Promise<void>((r) => setTimeout(r, intervalMs));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import type { SecretBox } from "../delivery/secrets.js";
|
||||
import type { DevelopService } from "../develop/service.js";
|
||||
import type { Event, Gallery, Order, OrderItem, Photo, Product, Site } from "../generated/prisma/client.js";
|
||||
import type { IngestLogger } from "../ingest/service.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import { codeListOf, iptcForPhoto, iptcOf } from "../library/iptc.js";
|
||||
import type { LocalStorage, UrlSigner } from "../storage/storage.js";
|
||||
import { EU, formatOrderNumber, priceOrder, splitVat } from "./pricing.js";
|
||||
@@ -716,6 +717,7 @@ export class FulfilmentWorker {
|
||||
private timer: NodeJS.Timeout | undefined;
|
||||
private stopped = false;
|
||||
private inFlight = 0;
|
||||
private ticking = false;
|
||||
|
||||
constructor(
|
||||
private readonly shop: ShopService,
|
||||
@@ -725,6 +727,7 @@ export class FulfilmentWorker {
|
||||
start(): void {
|
||||
void this.shop.requeueStale();
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
for (const id of await this.shop.claimDownloads(this.opts.concurrency - this.inFlight)) {
|
||||
this.inFlight++;
|
||||
@@ -734,14 +737,18 @@ export class FulfilmentWorker {
|
||||
}
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "shop: poll failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);
|
||||
};
|
||||
this.timer = setTimeout(tick, 0);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking && this.inFlight === 0, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({ inFlight: this.inFlight }, "shop: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import type { SportsCompetition, SportsPlayer, SportsProvider } from "../generat
|
||||
import type { IngestLogger } from "../ingest/service.js";
|
||||
import { DEFAULT_CODE_LIST } from "../library/iptc.js";
|
||||
import { badRequest, notFound } from "../lib/errors.js";
|
||||
import { waitUntil } from "../lib/idle.js";
|
||||
import type { LocalStorage } from "../storage/storage.js";
|
||||
import { type CodeClash, type SquadForCodes, buildCodeLines, hasGeneratedBlock, mergeGenerated, slugify, toTsv } from "./codegen.js";
|
||||
import {
|
||||
@@ -996,6 +997,7 @@ export class SportsService {
|
||||
export class SportsWorker {
|
||||
private timer: ReturnType<typeof setTimeout> | undefined;
|
||||
private stopped = false;
|
||||
private ticking = false;
|
||||
private lastRefresh = 0;
|
||||
|
||||
constructor(
|
||||
@@ -1005,6 +1007,7 @@ export class SportsWorker {
|
||||
|
||||
start(): void {
|
||||
const tick = async () => {
|
||||
this.ticking = true;
|
||||
try {
|
||||
if (Date.now() - this.lastRefresh >= (this.opts.refreshCheckMs ?? 10 * 60_000)) {
|
||||
this.lastRefresh = Date.now();
|
||||
@@ -1013,14 +1016,18 @@ export class SportsWorker {
|
||||
await this.service.pollLineups();
|
||||
} catch (err) {
|
||||
this.opts.log.warn({ err }, "sports: tick failed");
|
||||
} finally {
|
||||
this.ticking = false;
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.lineupEveryMs ?? 5 * 60_000);
|
||||
}
|
||||
if (!this.stopped) this.timer = setTimeout(tick, this.opts.lineupEveryMs ?? 5 * 60_000);
|
||||
};
|
||||
this.timer = setTimeout(tick, 5_000);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
async stop(timeoutMs = 25_000): Promise<void> {
|
||||
this.stopped = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
const drained = await waitUntil(() => !this.ticking, { timeoutMs });
|
||||
if (!drained) this.opts.log.warn({}, "sports: stop timed out with work still running");
|
||||
}
|
||||
}
|
||||
+17
-9
@@ -136,17 +136,25 @@ fulfilment.start();
|
||||
invoices.start();
|
||||
log.info({ ingestRoot: config.INGEST_ROOT, storage: config.STORAGE_ROOT }, "worker started");
|
||||
|
||||
/** Drain in-flight jobs before ending the Prisma/pg pool (BIT-31189). Leave headroom under PM2 kill_timeout. */
|
||||
const DRAIN_MS = 25_000;
|
||||
let shuttingDown = false;
|
||||
const shutdown = async (signal: string) => {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
log.info({ signal }, "worker stopping");
|
||||
scanner.stop();
|
||||
archive.stop();
|
||||
autopilot.stop();
|
||||
sports?.stop();
|
||||
deliveries.stop();
|
||||
faces?.stop();
|
||||
renders?.stop();
|
||||
fulfilment.stop();
|
||||
invoices.stop();
|
||||
// Stop claiming first, then wait for ticks/in-flight work so `$disconnect` does not race Prisma queries.
|
||||
await Promise.all([
|
||||
scanner.stop(DRAIN_MS),
|
||||
archive.stop(DRAIN_MS),
|
||||
autopilot.stop(DRAIN_MS),
|
||||
sports?.stop(DRAIN_MS) ?? Promise.resolve(),
|
||||
deliveries.stop(DRAIN_MS),
|
||||
faces?.stop(DRAIN_MS) ?? Promise.resolve(),
|
||||
renders?.stop(DRAIN_MS) ?? Promise.resolve(),
|
||||
fulfilment.stop(DRAIN_MS),
|
||||
invoices.stop(DRAIN_MS),
|
||||
]);
|
||||
await faceEngine?.close();
|
||||
await deliveryService.close();
|
||||
await renderEngine?.close();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { waitUntil } from "../src/lib/idle.js";
|
||||
|
||||
describe("waitUntil", () => {
|
||||
it("resolves true when the predicate is already true", async () => {
|
||||
expect(await waitUntil(() => true, { timeoutMs: 100 })).toBe(true);
|
||||
});
|
||||
|
||||
it("waits until the predicate flips", async () => {
|
||||
let ready = false;
|
||||
setTimeout(() => {
|
||||
ready = true;
|
||||
}, 30);
|
||||
expect(await waitUntil(() => ready, { timeoutMs: 500, intervalMs: 10 })).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false on timeout", async () => {
|
||||
expect(await waitUntil(() => false, { timeoutMs: 40, intervalMs: 10 })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -35,8 +35,11 @@ module.exports = {
|
||||
node_args: "--env-file=.env",
|
||||
instances: 1,
|
||||
exec_mode: "fork",
|
||||
max_memory_restart: "2G",
|
||||
kill_timeout: 30000,
|
||||
// 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,
|
||||
|
||||
Reference in new issue
Block a user