import type { Channel } from 'amqplib'; export const JOB_EXCHANGE = 'attachments.jobs'; export const JOB_ROUTING_KEY = 'attachment.process.v1'; export const JOB_QUEUE = 'attachments.process'; export const DEAD_LETTER_EXCHANGE = 'attachments.dlx'; export const DEAD_LETTER_QUEUE = 'attachments.process.dlq'; /** * Idempotent declaration used by both the producer and the consumer on every * (re)connect: a stand started from an empty volume and a reconnect after a * broker restart converge to the same durable topology. */ export async function assertTopology( channel: Channel, deliveryLimit: number, ): Promise { await channel.assertExchange(DEAD_LETTER_EXCHANGE, 'fanout', { durable: true, }); await channel.assertQueue(DEAD_LETTER_QUEUE, { durable: true, arguments: { 'x-queue-type': 'quorum' }, }); await channel.bindQueue(DEAD_LETTER_QUEUE, DEAD_LETTER_EXCHANGE, ''); await channel.assertExchange(JOB_EXCHANGE, 'direct', { durable: true }); await channel.assertQueue(JOB_QUEUE, { durable: true, arguments: { 'x-queue-type': 'quorum', 'x-dead-letter-exchange': DEAD_LETTER_EXCHANGE, 'x-delivery-limit': deliveryLimit, }, }); await channel.bindQueue(JOB_QUEUE, JOB_EXCHANGE, JOB_ROUTING_KEY); }