feat: add rabbitmq broker, outbox relay and worker services

This commit is contained in:
2026-11-15 16:08:11 +03:00
parent f4793b50cd
commit 350cc23e54
12 changed files with 741 additions and 11 deletions
+29
View File
@@ -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(