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
+2 -1
View File
@@ -13,6 +13,7 @@ import { parseJobMessage } from '../src/jobs/job-message';
import {
JobProcessor,
Outcome,
ProcessorHooks,
ProcessorOptions,
processorOptions,
} from '../src/jobs/processor';
@@ -55,7 +56,7 @@ export async function createTestApp(
export async function processAttachment(
app: INestApplication,
attachmentId: string,
hooks: { afterStore?: () => Promise<void> } = {},
hooks: ProcessorHooks = {},
options: Partial<ProcessorOptions> = {},
): Promise<Outcome> {
const prisma = app.get(PrismaService);
+19 -1
View File
@@ -5,7 +5,7 @@ import request from 'supertest';
import { ApiError } from '../src/common/filters/api-error';
import { PrismaService } from '../src/db/prisma.service';
import { jobMessage, parseJobMessage } from '../src/jobs/job-message';
import { backoffMs, recoverExpiredLeases, replayJob } from '../src/jobs/job-store';
import { backoffMs, recoverExpiredLeases, replayJob, republishStale } from '../src/jobs/job-store';
import { JobProcessor, processorOptions } from '../src/jobs/processor';
import { fitInside } from '../src/jobs/variants';
import { S3Service } from '../src/storage/s3.service';
@@ -235,6 +235,24 @@ describe('Фоновая обработка вложений (e2e)', () => {
expect(await events(id)).toEqual(['enqueued', 'processing', 'lease_expired', 'processing', 'ready']);
});
it('сообщение потеряно брокером: queued без свежей публикации публикуется повторно один раз', async () => {
const id = await upload();
const row = await job(id);
const later = new Date(Date.now() + 10 * 60 * 1000);
expect(await republishStale(prisma, 5 * 60 * 1000, later)).toBe(0);
await prisma.outboxEntry.updateMany({
where: { jobId: row.id },
data: { publishedAt: new Date() },
});
expect(await republishStale(prisma, 5 * 60 * 1000, new Date())).toBe(0);
expect(await republishStale(prisma, 5 * 60 * 1000, later)).toBeGreaterThanOrEqual(1);
expect(await prisma.outboxEntry.count({ where: { jobId: row.id } })).toBe(2);
expect(await republishStale(prisma, 5 * 60 * 1000, later)).toBe(0);
expect(await prisma.outboxEntry.count({ where: { jobId: row.id } })).toBe(2);
expect(await processAttachment(app, id)).toBe('ready');
expect(await events(id)).toEqual(['enqueued', 'republished', 'processing', 'ready']);
});
it('устаревшее сообщение, чужой attachment_id и другая pipeline_version игнорируются', async () => {
const id = await upload();
const row = await job(id);