mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
refactor(server): migrate plugin jobs off node-cron
The plugin host's job scheduler now uses the cron package (the one @nestjs/schedule wraps), so node-cron and @types/node-cron leave the dependency tree entirely. Verified against both real libraries that the accepted cron grammar is identical — no plugin's declared schedule tightens — and pinned that with an unmocked compat test.
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import cron, { type ScheduledTask } from 'node-cron';
|
||||
import { CronJob, validateCronExpression } from 'cron';
|
||||
import { JOBS_PERMISSION } from '../protocol/envelope';
|
||||
|
||||
/**
|
||||
* Host-side scheduler for a plugin's declared background jobs (#plugins).
|
||||
*
|
||||
* A plugin declares `jobs` (id + cron `schedule`) in its code; the child reports
|
||||
* them at load. This wires each to node-cron in the HOST and fires
|
||||
* them at load. This wires each to a `cron` CronJob in the HOST and fires
|
||||
* `invoke(id, 'invoke.job', { jobId })` on the tick. Kept deliberately small and
|
||||
* pure so it is unit-testable in isolation of the supervisor.
|
||||
*
|
||||
@@ -16,6 +16,10 @@ import { JOBS_PERMISSION } from '../protocol/envelope';
|
||||
* egress. (3) An invalid cron is skipped (never runs). (4) The tick callback and
|
||||
* `stop()` are wrapped so a throwing job or a stray task can never break the host
|
||||
* or leak across a deactivation.
|
||||
*
|
||||
* The library is `cron` (the one @nestjs/schedule wraps), migrated from
|
||||
* node-cron when the core scheduler retired; tests/unit/plugins/
|
||||
* plugin-jobs.cron-compat.test.ts pins that the accepted grammar did not narrow.
|
||||
*/
|
||||
|
||||
export interface ScheduledJob {
|
||||
@@ -27,25 +31,29 @@ export interface ScheduledJob {
|
||||
export { JOBS_PERMISSION as JOBS_RUN_PERMISSION } from '../protocol/envelope';
|
||||
|
||||
/**
|
||||
* Schedule every valid-cron job (returns the node-cron tasks to keep for teardown).
|
||||
* Schedule every valid-cron job (returns the CronJobs to keep for teardown).
|
||||
* Empty when the plugin lacks `jobs:run`, so a plugin without the grant runs nothing.
|
||||
*/
|
||||
export function scheduleJobs(
|
||||
granted: ReadonlySet<string>,
|
||||
jobs: readonly ScheduledJob[],
|
||||
run: (jobId: string) => void,
|
||||
): ScheduledTask[] {
|
||||
): CronJob[] {
|
||||
if (!granted.has(JOBS_PERMISSION)) return [];
|
||||
const tasks: ScheduledTask[] = [];
|
||||
const tasks: CronJob[] = [];
|
||||
for (const job of jobs) {
|
||||
if (!job.schedule || !cron.validate(job.schedule)) continue; // invalid cron -> never runs
|
||||
if (!job.schedule || !validateCronExpression(job.schedule).valid) continue; // invalid cron -> never runs
|
||||
tasks.push(
|
||||
cron.schedule(job.schedule, () => {
|
||||
try {
|
||||
run(job.id);
|
||||
} catch {
|
||||
/* a job must never break the host loop */
|
||||
}
|
||||
CronJob.from({
|
||||
cronTime: job.schedule,
|
||||
start: true,
|
||||
onTick: () => {
|
||||
try {
|
||||
run(job.id);
|
||||
} catch {
|
||||
/* a job must never break the host loop */
|
||||
}
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -53,7 +61,7 @@ export function scheduleJobs(
|
||||
}
|
||||
|
||||
/** Stop + drop the given tasks (called on deactivation/kill so nothing leaks). */
|
||||
export function stopJobs(tasks: ScheduledTask[] | undefined): void {
|
||||
export function stopJobs(tasks: CronJob[] | undefined): void {
|
||||
for (const t of tasks ?? []) {
|
||||
try {
|
||||
t.stop();
|
||||
|
||||
@@ -53,7 +53,7 @@ interface Supervised {
|
||||
lastRss: number; // last reported resident set size (bytes)
|
||||
routes: PluginRouteInfo[];
|
||||
jobs: ScheduledJob[]; // declared background jobs (id + cron schedule)
|
||||
jobTasks?: ReturnType<typeof scheduleJobs>; // live node-cron tasks (only when jobs:run granted)
|
||||
jobTasks?: ReturnType<typeof scheduleJobs>; // live cron jobs (only when jobs:run granted)
|
||||
hooks: string[]; // provider hooks the plugin implements (e.g. 'placeDetailProvider')
|
||||
events: string[]; // core events the plugin subscribes to (names or '*')
|
||||
exports: string[]; // functions the plugin exposes to dependents (ctx.plugins.call)
|
||||
@@ -638,7 +638,7 @@ export class PluginSupervisor {
|
||||
sup.child = null;
|
||||
// In-flight host→child invokes can never complete now.
|
||||
this.rejectPending(sup, 'plugin exited');
|
||||
// The dead child's node-cron tasks keep ticking (node-cron holds them, not the
|
||||
// The dead child's cron jobs keep ticking (the host holds them, not the
|
||||
// child). Stop them here — otherwise every crash-restart cycle leaks a task-set
|
||||
// AND re-schedules a fresh one, so the job fires N+1 times per tick after N
|
||||
// crashes (duplicate egress/db side effects). kill() already does this for the
|
||||
|
||||
Reference in New Issue
Block a user