feat: add rabbitmq broker, outbox relay and worker services
This commit is contained in:
@@ -158,6 +158,35 @@ export async function recoverExpiredLeases(
|
||||
return recovered;
|
||||
}
|
||||
|
||||
export async function republishStale(
|
||||
prisma: PrismaClient,
|
||||
staleMs: number,
|
||||
now = new Date(),
|
||||
limit = 100,
|
||||
): Promise<number> {
|
||||
const cutoff = new Date(now.getTime() - staleMs);
|
||||
const stale = await prisma.$queryRaw<AttachmentJob[]>`
|
||||
SELECT j.id, j.attachment_id AS "attachmentId", j.pipeline_version AS "pipelineVersion", j.attempt
|
||||
FROM attachment_jobs j
|
||||
WHERE j.status = 'queued'
|
||||
AND j.next_attempt_at < ${cutoff}
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM outbox o
|
||||
WHERE o.job_id = j.id AND (o.published_at IS NULL OR o.published_at > ${cutoff})
|
||||
)
|
||||
ORDER BY j.next_attempt_at
|
||||
LIMIT ${limit}`;
|
||||
for (const job of stale) {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.jobEvent.create({
|
||||
data: { jobId: job.id, attempt: job.attempt, event: 'republished', createdAt: now },
|
||||
});
|
||||
await publishLater(tx, job, now);
|
||||
});
|
||||
}
|
||||
return stale.length;
|
||||
}
|
||||
|
||||
export type ReplayResult = 'replayed' | 'already_ready' | 'not_failed' | 'not_found';
|
||||
|
||||
export async function replayJob(
|
||||
|
||||
Reference in New Issue
Block a user