mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 12:50:37 +01:00
Retain exact cleanup ownership through paused or removed local receivers, release captured runtimes only after accepted close receipts, and join the existing event owner handshake before Cloud enrollment. Keep blocked dispatch and owner replacement recoverable. Co-Authored-By: Paperclip <noreply@paperclip.ing>
45 lines
2.5 KiB
TypeScript
45 lines
2.5 KiB
TypeScript
import { logger } from "../middleware/logger.js";
|
|
import { registerScopedDeadlineWork } from "./scoped-deadline-work.js";
|
|
import type { Db } from "@paperclipai/db";
|
|
import type { createDeliveryWorkCoordinator } from "./delivery-work-coordinator.js";
|
|
import { registerDeadlineWork } from "./deadline-work.js";
|
|
import { DELIVERY_QUEUES } from "./delivery-work-notifications.js";
|
|
import { registerConnectionEventWake } from "./connection-event-wake.js";
|
|
import type { githubConnectionEventService } from "./github-connection-events.js";
|
|
import type { createGitHubGrantContinuity } from "./tool-access/github-grant-continuity.js";
|
|
|
|
type Coordinator = ReturnType<typeof createDeliveryWorkCoordinator>;
|
|
export function registerGitHubConnectionEventWork(db: Db, coordinator: Coordinator,
|
|
service: ReturnType<typeof githubConnectionEventService>, canRun: () => boolean) {
|
|
const worker = registerDeadlineWork(coordinator, DELIVERY_QUEUES.connectionEvents, {
|
|
retryMs: 5_000, minimumSpacingMs: 5_000, notificationCoalesceMs: 1_000, canRun,
|
|
onBlocked: () => service.declineEventWakeEnrollment(),
|
|
run: () => service.pollOnce({ forDelivery: true }),
|
|
next: async () => service.nextPollAt(),
|
|
});
|
|
const stops = [registerConnectionEventWake(db, worker.wake, { canRun, join: signal => {
|
|
const joined = service.joinEventWakeEnrollment(signal);
|
|
worker.wake();
|
|
return joined;
|
|
} })];
|
|
return { ...worker, stop: async () => { for (const stop of stops) stop(); await worker.stop(); } };
|
|
}
|
|
export function registerGitHubConnectionContinuityWork(db: Db, coordinator: Coordinator,
|
|
service: Pick<ReturnType<typeof createGitHubGrantContinuity>, "sweepGitHubConnectionContinuity" | "nextGitHubConnectionContinuityAt" | "githubConnectionContinuitySchedule">,
|
|
canRun: () => boolean) {
|
|
const worker = registerScopedDeadlineWork(coordinator, {
|
|
queue: DELIVERY_QUEUES.connectionContinuity,
|
|
minimumPassIntervalMs: 5_000,
|
|
decodeScope: hint => hint === "*" ? undefined : hint,
|
|
matches: (entry, companyId?: string) => !companyId || entry.companyId === companyId,
|
|
load: companyId => service.githubConnectionContinuitySchedule(companyId),
|
|
check: async entry => {
|
|
const result = await service.sweepGitHubConnectionContinuity(entry.companyId);
|
|
if (result.failed) logger.warn(result, "GitHub credential continuity retained retry obligations");
|
|
const dueAt = await service.nextGitHubConnectionContinuityAt(entry.companyId);
|
|
return dueAt === null ? null : { ...entry, dueAt };
|
|
},
|
|
}, canRun);
|
|
return worker;
|
|
}
|