From 2006080757be9843f160baa400fbf8d273b20ab0 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 22:47:19 -0600 Subject: [PATCH] feat(pm-stack): add pm-core scheduler seam for quota and feed jobs --- .../aggregator/scripts/collect-fleet-feed.ts | 2 +- pm-stack/aggregator/scripts/gather-quota.ts | 4 +- pm-stack/aggregator/src/pm-core-scheduler.ts | 90 +++++++++++++++++++ .../aggregator/test/pm-core-scheduler.test.ts | 86 ++++++++++++++++++ 4 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 pm-stack/aggregator/src/pm-core-scheduler.ts create mode 100644 pm-stack/aggregator/test/pm-core-scheduler.test.ts diff --git a/pm-stack/aggregator/scripts/collect-fleet-feed.ts b/pm-stack/aggregator/scripts/collect-fleet-feed.ts index 161d265e5..496355535 100644 --- a/pm-stack/aggregator/scripts/collect-fleet-feed.ts +++ b/pm-stack/aggregator/scripts/collect-fleet-feed.ts @@ -841,7 +841,7 @@ async function collectAaSection(generatedAt: string): Promise { return withIndex; } -async function main(): Promise { +export async function main(): Promise { const now = new Date(); const sectionStatuses: NonNullable["sections"] = {}; const markSection = ( diff --git a/pm-stack/aggregator/scripts/gather-quota.ts b/pm-stack/aggregator/scripts/gather-quota.ts index 5e43767bc..3294d0bc7 100755 --- a/pm-stack/aggregator/scripts/gather-quota.ts +++ b/pm-stack/aggregator/scripts/gather-quota.ts @@ -1052,7 +1052,7 @@ async function runAnomalyDetectors(args: { } } -async function main(): Promise { +export async function main(): Promise { const started = Date.now(); const tickAt = new Date(started).toISOString(); @@ -1318,4 +1318,4 @@ async function main(): Promise { return 0; } -process.exit(await main()); +if (import.meta.main) process.exit(await main()); diff --git a/pm-stack/aggregator/src/pm-core-scheduler.ts b/pm-stack/aggregator/src/pm-core-scheduler.ts new file mode 100644 index 000000000..287f6c6a4 --- /dev/null +++ b/pm-stack/aggregator/src/pm-core-scheduler.ts @@ -0,0 +1,90 @@ +/** + * In-process PM-core job scheduling (#8570, first consolidation slice). + * + * The jobs are injected so the scheduler owns cadence and single-flight + * semantics without owning provider or feed logic. Audit records are + * write-only: the scheduler reports each attempt to the caller, but never + * reads an audit file as a freshness source. + */ + +export const QUOTA_CADENCE_MS = 15 * 60_000; +export const FLEET_FEED_CADENCE_MS = 5 * 60_000; + +export type SchedulerJobName = "quota" | "fleet-feed"; + +export interface SchedulerAudit { + job: SchedulerJobName; + startedAt: string; + finishedAt: string; + status: "ok" | "error"; + error?: string; +} + +export interface PmCoreScheduler { + start(): void; + stop(): void; + runNow(job: SchedulerJobName): Promise; + running(job: SchedulerJobName): boolean; +} + +type Timer = ReturnType; +type Job = () => Promise | void; + +export function createPmCoreScheduler(options: { + runQuota: Job; + runFleetFeed: Job; + writeAudit: (record: SchedulerAudit) => Promise | void; + now?: () => Date; + setInterval?: (callback: () => void, delayMs: number) => Timer; + clearInterval?: (timer: Timer) => void; +}): PmCoreScheduler { + const now = options.now ?? (() => new Date()); + const setTimer = options.setInterval ?? ((callback, delayMs) => setInterval(callback, delayMs)); + const clearTimer = options.clearInterval ?? ((timer) => clearInterval(timer)); + const jobs: Record = { + quota: options.runQuota, + "fleet-feed": options.runFleetFeed, + }; + const cadence: Record = { + quota: QUOTA_CADENCE_MS, + "fleet-feed": FLEET_FEED_CADENCE_MS, + }; + const active = new Set(); + const timers = new Map(); + + async function runNow(job: SchedulerJobName): Promise { + if (active.has(job)) return; + active.add(job); + const startedAt = now().toISOString(); + try { + await jobs[job](); + await options.writeAudit({ job, startedAt, finishedAt: now().toISOString(), status: "ok" }); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + await options.writeAudit({ + job, + startedAt, + finishedAt: now().toISOString(), + status: "error", + error: message, + }); + } finally { + active.delete(job); + } + } + + return { + start() { + if (timers.size > 0) return; + for (const job of Object.keys(jobs) as SchedulerJobName[]) { + timers.set(job, setTimer(() => void runNow(job), cadence[job])); + } + }, + stop() { + for (const timer of timers.values()) clearTimer(timer); + timers.clear(); + }, + runNow, + running: (job) => active.has(job), + }; +} diff --git a/pm-stack/aggregator/test/pm-core-scheduler.test.ts b/pm-stack/aggregator/test/pm-core-scheduler.test.ts new file mode 100644 index 000000000..938304c74 --- /dev/null +++ b/pm-stack/aggregator/test/pm-core-scheduler.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, test } from "bun:test"; +import { + createPmCoreScheduler, + FLEET_FEED_CADENCE_MS, + QUOTA_CADENCE_MS, + type SchedulerAudit, +} from "../src/pm-core-scheduler.ts"; + +describe("pm-core scheduler (#8570)", () => { + test("registers the quota and fleet-feed cadences without reading audit state", () => { + const timers: number[] = []; + const audits: SchedulerAudit[] = []; + const scheduler = createPmCoreScheduler({ + runQuota: () => {}, + runFleetFeed: () => {}, + writeAudit: (record) => { + audits.push(record); + }, + setInterval: (_callback, delayMs) => { + timers.push(delayMs); + return delayMs as unknown as ReturnType; + }, + clearInterval: () => {}, + }); + + scheduler.start(); + scheduler.start(); + expect(timers).toEqual([QUOTA_CADENCE_MS, FLEET_FEED_CADENCE_MS]); + expect(audits).toEqual([]); + scheduler.stop(); + }); + + test("single-flights a job and writes success audit after it completes", async () => { + let release!: () => void; + let calls = 0; + const audits: SchedulerAudit[] = []; + const scheduler = createPmCoreScheduler({ + runQuota: () => { + calls += 1; + return new Promise((resolve) => { + release = resolve; + }); + }, + runFleetFeed: () => {}, + writeAudit: (record) => { + audits.push(record); + }, + now: (() => { + let tick = 0; + return () => new Date(`2026-11-23T00:00:0${tick++}Z`); + })(), + }); + + const first = scheduler.runNow("quota"); + const second = scheduler.runNow("quota"); + expect(scheduler.running("quota")).toBe(true); + expect(calls).toBe(1); + release(); + await Promise.all([first, second]); + expect(scheduler.running("quota")).toBe(false); + expect(audits).toHaveLength(1); + expect(audits[0].status).toBe("ok"); + expect(audits[0].job).toBe("quota"); + }); + + test("records a failed job without stopping the other scheduler", async () => { + const audits: SchedulerAudit[] = []; + const scheduler = createPmCoreScheduler({ + runQuota: async () => { + throw new Error("provider unavailable"); + }, + runFleetFeed: () => {}, + writeAudit: (record) => { + audits.push(record); + }, + }); + + await scheduler.runNow("quota"); + await scheduler.runNow("fleet-feed"); + expect(audits.map((audit) => [audit.job, audit.status])).toEqual([ + ["quota", "error"], + ["fleet-feed", "ok"], + ]); + expect(audits[0].error).toBe("provider unavailable"); + }); +});