diff --git a/server/src/__tests__/plugin-scheduled-work.test.ts b/server/src/__tests__/plugin-scheduled-work.test.ts index 48fb5c41b84..30a9d2a2962 100644 --- a/server/src/__tests__/plugin-scheduled-work.test.ts +++ b/server/src/__tests__/plugin-scheduled-work.test.ts @@ -102,6 +102,21 @@ const support = external ? { supported: true } : await getEmbeddedPostgresTestSu expect(await runs()).toHaveLength(15); expect(rpc).not.toHaveBeenCalled(); }); + it("honors a saved deadline inside the notification batching window", async () => { + const row = await job(); + const deadline = new Date(Date.now() + 10_000); + await pluginJobStore(db).updateRunTimestamps(row.id, new Date(0), deadline); + await start(); + // registerPlugin produces a local wake immediately after startup. It may + // batch the reload but must not replace the known ten-second deadline. + await settled(); + expect(coordinator!.nextWakeAt()).toBe(deadline.getTime()); + expect(rpc).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(Math.max(0, deadline.getTime() - Date.now())); + await completed(); + expect(rpc).toHaveBeenCalledOnce(); + expect(rpc.mock.calls[0][2]).toMatchObject({ job: { scheduledAt: deadline.toISOString() } }); + }); it("keeps configured manual-only and invalid schedules quiet", async () => { await job("manual", ""); await job("invalid", "not cron"); await start(); await quiet(); expect(rpc).not.toHaveBeenCalled(); }); diff --git a/server/src/services/deadline-work.ts b/server/src/services/deadline-work.ts index f217398dd95..fe765701e11 100644 --- a/server/src/services/deadline-work.ts +++ b/server/src/services/deadline-work.ts @@ -9,11 +9,14 @@ export function registerDeadlineWork( next: () => Promise; canRun: () => boolean; retryMs: number; + // Optional batching latency for notifications; already-known deadlines still preempt it. + notificationCoalesceMs?: number; }, ) { let requested = 1, reconciled = 0; let next: number | null = null; let earliest = 0; + let coalesceUntil = 0; const worker = coordinator.register(queue, { retryMs: task.retryMs, pausePassiveMaintenance: true, @@ -21,6 +24,10 @@ export function registerDeadlineWork( run: async signal => { if (!task.canRun()) { requested++; return; } if (Date.now() < earliest) { next = earliest; return; } + if (Date.now() < coalesceUntil && (next === null || next > Date.now())) { + next = next === null ? coalesceUntil : Math.min(next, coalesceUntil); + return; + } const generation = ++requested; earliest = Date.now() + task.retryMs; await task.run(signal); @@ -29,6 +36,7 @@ export function registerDeadlineWork( // Back off failed or still-overdue work, not a successfully read future deadline. if (deadline === null || deadline > Date.now()) earliest = 0; next = deadline === null ? null : Math.max(earliest, deadline); + coalesceUntil = Date.now() + (task.notificationCoalesceMs ?? 0); reconciled = generation; }, hasPending: async () => requested !== reconciled, diff --git a/server/src/services/plugin-job-scheduler.ts b/server/src/services/plugin-job-scheduler.ts index 827600f019b..e344e205c32 100644 --- a/server/src/services/plugin-job-scheduler.ts +++ b/server/src/services/plugin-job-scheduler.ts @@ -186,6 +186,7 @@ export function createPluginJobScheduler(options: PluginJobSchedulerOptions): Pl running = true; worker = registerDeadlineWork(coordinator, DELIVERY_QUEUES.pluginSchedules, { run: tick, next: async () => next, canRun, retryMs: tickIntervalMs, + notificationCoalesceMs: tickIntervalMs, }); await worker.ready; },