mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-06 15:52:22 +01:00
Make plugin notification batching independent of saved deadlines
Opt the plugin inventory into bounded notification coalescing without delaying deadlines already loaded from durable state. Other deadline consumers reconcile notifications immediately. Verified 76 custom-image and plugin scheduling tests, including burst reads, saved near-term execution, and failure retry ownership. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
806e02d96d
commit
009794ff15
3 files changed
+24
No files matched your search
@@ -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();
|
||||
});
|
||||
|
||||
@@ -9,11 +9,14 @@ export function registerDeadlineWork(
|
||||
next: () => Promise<number | null>;
|
||||
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,
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user