Files
PaperClipAI/server/src/services/deadline-work.ts
T
DottaandPaperclip 1bada576a4 Merge bounded gateway credential cleanup into sleep-09-decision-continuations
Propagate bounded gateway credential cleanup and notification coalescing while preserving saved deadlines and each PR boundary.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-10-27 00:31:48 -05:00

47 lines
1.8 KiB
TypeScript

import type { createDeliveryWorkCoordinator } from "./delivery-work-coordinator.js";
import type { DeliveryQueue } from "./delivery-work-notifications.js";
export function registerDeadlineWork(
coordinator: ReturnType<typeof createDeliveryWorkCoordinator>,
queue: DeliveryQueue,
task: {
run: (signal: AbortSignal) => Promise<unknown>;
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,
onSettled: () => { requested++; },
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);
if (signal.aborted) return;
const deadline = await task.next();
// 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,
nextRunAt: () => next,
});
return { ...worker, wake: () => { requested++; worker.wake(); } };
}