perf(jobs): poll a queue only its cron feeds every 30 seconds

pg-boss polls each idle worker slot every two seconds, so the hundred-odd
bindings asked the database for work about fifty times a second while
most of them wait for one tick a day. `createAndWork` now gives a queue
that a registrar schedules and nothing else sends to a 30-second poll.
Scheduled queues that also take sends keep the default, because with one
job per fetch a slow poll would drain a burst at one job per interval;
the runtime guard holds that list to the send sites in the tree,
including sends through a variable.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 03:35:09 +02:00
1 parent 0770ae3590
commit d9e56482d9
3 files changed
+170 -3

No files matched your search

@@ -41,6 +41,7 @@ import {
type QueueRuntime,
type SourceSite,
} from "@/lib/jobs/queue-runtime";
import { SCHEDULED_AND_SENT_QUEUES } from "@/lib/jobs/reminder/registrar-shared";
const SRC = join(process.cwd(), "src");
const REGISTRARS = [
@@ -369,3 +370,48 @@ describe("every queue declares its runtime", () => {
).toEqual(["planted-long: expiry 900s out of range"]);
});
});
/**
* Queues a file sends to through a variable (`boss.send(queueName, …)`): every
* known queue name the file spells out, as a constant or a map value, counts
* as sent, because the matcher above cannot tell which one the variable holds.
*/
function dynamicallySent(texts: ReadonlyMap<string, string>): Set<string> {
const out = new Set<string>();
for (const [, raw] of texts) {
const text = code(raw);
if (
!/\b(?:boss|getGlobalBoss\(\)\??)\s*\.\s*send\s*(?:<[^(]*?>)?\(\s*[a-z]/.test(
text,
)
) {
continue;
}
for (const m of text.matchAll(/"([a-z0-9-]+)"/g)) {
if (m[1] in QUEUE_RUNTIME) out.add(m[1]);
}
}
return out;
}
describe("a cron-fed queue polls slowly, a sent one does not", () => {
const all = scan(source);
const scheduled = new Set(all.schedules.map((s) => s.queue));
const sent = new Set([
...all.sends.map((s) => s.queue),
...dynamicallySent(source),
]);
it("lists exactly the scheduled queues that also take sends", () => {
const expected = [...scheduled].filter((q) => sent.has(q)).sort();
expect([...SCHEDULED_AND_SENT_QUEUES].sort()).toEqual(expected);
// Most scheduled queues are fed by their cron alone.
expect(scheduled.size - expected.length).toBeGreaterThanOrEqual(40);
});
it("counts a send through a variable as a send to every queue the file names", () => {
const dynamic = dynamicallySent(source);
expect(dynamic.has("withings-activity-sync")).toBe(true);
expect(dynamic.has("whoop-recovery-sync")).toBe(true);
});
});