|
| 1 | +// import amqp from "amqplib"; |
| 2 | + |
| 3 | +// let channel: amqp.Channel; |
| 4 | + |
| 5 | +// export const connectRabbitMQ = async () => { |
| 6 | +// try { |
| 7 | +// const connection = await amqp.connect("amqp://admin:admin@rabbitmq:5672"); |
| 8 | +// channel = await connection.createChannel(); |
| 9 | +// console.log("RabbitMQ connecté"); |
| 10 | +// } catch (error) { |
| 11 | +// console.error("Erreur RabbitMQ:", error); |
| 12 | +// } |
| 13 | +// }; |
| 14 | + |
| 15 | +// export const getChannel = () => channel; |
| 16 | + |
| 17 | +// src/messaging/rabbitmq.ts |
| 18 | +/** |
| 19 | + * Connexion RabbitMQ partagée pour otp-service |
| 20 | + * - garde la même connexion/channel pendant tout le runtime |
| 21 | + * - gère reconnect automatique en cas de fermeture/erreur |
| 22 | + * |
| 23 | + * Variables d'environnement : |
| 24 | + * - RABBITMQ_URL (ex: amqp://admin:admin@rabbitmq:5672) |
| 25 | + */ |
| 26 | + |
| 27 | +// src/messaging/rabbitmq.ts |
| 28 | +import * as amqp from "amqplib"; |
| 29 | +import type { Connection, Channel } from "amqplib"; |
| 30 | + |
| 31 | +let connection: amqp.Connection | null = null; |
| 32 | +let channel: Channel | null = null; |
| 33 | + |
| 34 | +// Nom des files |
| 35 | +export const QUEUE_MAIN = "notifications.main"; |
| 36 | +export const QUEUE_RETRY = "notifications.retry"; |
| 37 | +export const QUEUE_DLQ = "notifications.dlq"; |
| 38 | + |
| 39 | +// Fonction de connexion + reconnexion automatique |
| 40 | +export async function ensureChannel() { |
| 41 | + try { |
| 42 | + console.log("Tentative de connexion à RabbitMQ..."); |
| 43 | + |
| 44 | + let connection = await amqp.connect(process.env.RABBITMQ_URL!); |
| 45 | + |
| 46 | + // garder une référence locale non-nulle pour satisfaire TypeScript |
| 47 | + const conn = connection!; |
| 48 | + |
| 49 | + conn.on("error", (err) => { |
| 50 | + console.error("⚠️ Erreur RabbitMQ :", err); |
| 51 | + // connection = null; |
| 52 | + }); |
| 53 | + |
| 54 | + conn.on("close", () => { |
| 55 | + console.error("Connexion RabbitMQ fermée. Reconnexion..."); |
| 56 | + //connection = null; |
| 57 | + setTimeout(ensureChannel, 3000); |
| 58 | + }); |
| 59 | + |
| 60 | + channel = await conn.createChannel(); |
| 61 | + const ch = channel!; |
| 62 | + |
| 63 | + console.log(" Connecté à RabbitMQ !"); |
| 64 | + |
| 65 | + // ---- Déclaration des files ---- |
| 66 | + |
| 67 | + // File principale |
| 68 | + await ch.assertQueue(QUEUE_MAIN, { durable: true }); |
| 69 | + |
| 70 | + // File de retry avec TTL de 5s |
| 71 | + await ch.assertQueue(QUEUE_RETRY, { |
| 72 | + durable: true, |
| 73 | + arguments: { |
| 74 | + "x-message-ttl": 5000, |
| 75 | + "x-dead-letter-exchange": "", |
| 76 | + "x-dead-letter-routing-key": QUEUE_MAIN, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + // Dead-Letter Queue |
| 81 | + await ch.assertQueue(QUEUE_DLQ, { durable: true }); |
| 82 | + |
| 83 | + return ch; |
| 84 | + } catch (err) { |
| 85 | + console.error("Erreur de connexion à RabbitMQ :", err); |
| 86 | + setTimeout(ensureChannel, 3000); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export function getRabbitChannel(): Channel { |
| 91 | + if (!channel) { |
| 92 | + throw new Error("RabbitMQ non connecté !"); |
| 93 | + } |
| 94 | + return channel; |
| 95 | +} |
0 commit comments