/** * Scheduler — OS-level in-process cron-like scheduler (Phase 3, C2). * * Runs periodic jobs on a CLOCK, alongside the existing event-debounce * triggers. The existing job implementations (consolidation, decay-sweep, * auto-improve, observation-bridge) are unchanged — the scheduler just invokes * their existing entrypoints at scheduled times. * * Design: * - Durable job table (PostgreSQL + PG parity): persists job definitions + * nextRunAt/lastRunAt so a restart resumes the schedule. * - Job registry: handlers keyed by jobKind. The scheduler invokes the * registered handler; never the job implementation directly. * - Scheduler loop: a single setInterval tick (default 60s, env-overridable). * On each tick, loads enabled jobs whose nextRunAt <= now and fires them. * - Missed-run policy: SKIP with a logged warning (avoid stampede). If a job's * nextRunAt is far in the past (more than one tick), we skip the missed * runs and reschedule from now. * - Concurrency guard: never run the same jobKind twice concurrently. * - Concurrency cap: at most MAX_CONCURRENT jobs firing at once. * - Singleton: module-level cached instance + getScheduler()/resetScheduler(). * - Lifecycle: start() loads enabled jobs + begins the tick; stop() clears the * interval. The interval is .unref()'d so it never keeps the event loop * alive solely for scheduling (parity with the job-reaper pattern). * * Config (env-overridable, default OFF or conservative): * MASSA_TH0TH_SCHEDULER_ENABLED (default false) master switch * MASSA_TH0TH_SCHEDULER_TICK_MS (default 60000) tick interval * MASSA_TH0TH_SCHEDULER_MAX_CONCURRENT (default 2) concurrent fire cap * * Default job intervals (conservative, env-overridable per kind): * memory-consolidation: 30 min * decay-sweep: 60 min * auto-improve: 30 min * observation-bridge: 30 min * All default to disabled=false unless MASSA_TH0TH_SCHEDULER_ENABLED=true AND * the job's own enable flag is set. The boot wiring registers them as * default-disabled; a deployment opts in by setting enabled=true on the row or * MASSA_TH0TH_SCHEDULER__ENABLED=true before boot. */ import type { ScheduledJobStore } from "./scheduler-store.js"; import type { JobHandler, JobKind, ScheduledJob, ScheduleSpec, SchedulerStatus, TickResult } from "./scheduler-types.js"; export interface SchedulerOptions { /** Inject a store (tests). Defaults to getScheduledJobStore(). */ store?: ScheduledJobStore; /** Override the tick interval (tests). Defaults to env or 60000. */ tickIntervalMs?: number; /** Override the concurrency cap (tests). Defaults to env or 2. */ maxConcurrent?: number; /** Override the master switch (tests). Defaults to env or false. */ enabled?: boolean; } export declare class Scheduler { private readonly store; private readonly tickIntervalMs; private readonly maxConcurrent; private readonly enabled; /** Registry: jobKind → handler. */ private handlers; /** Parsed cron cache (avoids re-parsing on every tick). */ private cronCache; /** Currently-running jobKinds (concurrency guard). */ private running; private timer; private started; constructor(opts?: SchedulerOptions); /** * Register a handler for a jobKind. The scheduler will invoke this handler * when a scheduled job of that kind fires. Overwrites any prior registration. */ registerHandler(jobKind: JobKind, handler: JobHandler): void; /** Unregister a handler (tests / shutdown). */ unregisterHandler(jobKind: JobKind): void; /** List registered jobKinds. */ registeredKinds(): JobKind[]; /** * Register or upsert a scheduled job definition. Persists it to the store. * If the job already exists (by id), its definition is updated; nextRunAt is * preserved unless the schedule changed. */ registerJob(job: Omit & { lastRunAt?: number; }): ScheduledJob; /** * Register or RESUME a scheduled job definition across a restart. Like * registerJob, but when the schedule is unchanged and the job already exists, * it preserves the existing nextRunAt (resume on restart) rather than * recomputing. If the preserved nextRunAt is in the past and the job is * enabled, reschedule from now (avoids a noisy missed-run log on boot). * * This is the boot-time registration used by registerDefaultJobs(). */ registerOrResumeJob(job: Omit & { lastRunAt?: number; }): ScheduledJob; private scheduleChanged; /** Enable/disable a job by id. Persists. */ setEnabled(id: string, enabled: boolean): void; /** Delete a job definition. */ removeJob(id: string): void; /** * Compute the next run time for a schedule, anchored at `fromMs`. * For intervals: fromMs + intervalMs. * For cron: the next cron match strictly after fromMs. */ computeNextRun(schedule: ScheduleSpec, fromMs: number): number; /** * Start the scheduler. Loads enabled jobs from the store and begins the tick. * Safe to call when already started (no-op). Safe to call when the master * switch is off (logs once, no tick). */ start(): void; /** * Stop the scheduler. Clears the interval. Does NOT delete job definitions. */ stop(): void; isRunning(): boolean; /** * Run one scheduler tick. Evaluates all enabled jobs, fires those due, skips * missed runs with a warning, and reschedules. Returns a summary for logging. */ tick(now?: number): Promise; /** * Fire a single job: invoke its handler, update lastRunAt + nextRunAt, * persist. Never throws — all errors are caught and logged. */ private fireJob; status(now?: number): SchedulerStatus; /** Test/debug: is a jobKind currently executing? */ isJobRunning(jobKind: JobKind): boolean; } export declare function getScheduler(): Scheduler; export declare function resetScheduler(): void; //# sourceMappingURL=scheduler.d.ts.map