event-publisher.ts106 lines, 2.8 KB
| 1 | import { USER_CREATED_ROUTING_KEY, USER_EVENTS_EXCHANGE } from '@chatapp/common'; |
| 2 | import amqplib from 'amqplib'; |
| 3 | |
| 4 | import type { UserCreatedEvent, UserCreatedPayload } from '@chatapp/common'; |
| 5 | import type { Channel, ChannelModel, Connection } from 'amqplib'; |
| 6 | |
| 7 | import { env } from '@/config/env'; |
| 8 | import { logger } from '@/utils/logger'; |
| 9 | |
| 10 | type ManagedConnection = Connection & Pick<ChannelModel, 'close' | 'createChannel'>; |
| 11 | |
| 12 | let connection: ManagedConnection | null = null; |
| 13 | let channel: Channel | null = null; |
| 14 | |
| 15 | const messagingEnabled = Boolean(env.RABBITMQ_URL); |
| 16 | |
| 17 | const ensureChannel = async (): Promise<Channel | null> => { |
| 18 | if (!messagingEnabled) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | if (channel) { |
| 23 | return channel; |
| 24 | } |
| 25 | |
| 26 | if (!env.RABBITMQ_URL) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | const amqpConnection = (await amqplib.connect(env.RABBITMQ_URL)) as unknown as ManagedConnection; |
| 31 | connection = amqpConnection; |
| 32 | amqpConnection.on('close', () => { |
| 33 | logger.warn('RabbitMQ connection closed'); |
| 34 | connection = null; |
| 35 | channel = null; |
| 36 | }); |
| 37 | amqpConnection.on('error', (error) => { |
| 38 | logger.error({ err: error }, 'RabbitMQ connection error'); |
| 39 | }); |
| 40 | |
| 41 | const amqpChannel = await amqpConnection.createChannel(); |
| 42 | channel = amqpChannel; |
| 43 | await amqpChannel.assertExchange(USER_EVENTS_EXCHANGE, 'topic', { durable: true }); |
| 44 | |
| 45 | return amqpChannel; |
| 46 | }; |
| 47 | |
| 48 | export const initMessaging = async () => { |
| 49 | if (!messagingEnabled) { |
| 50 | logger.info('RabbitMQ URL is not configured; messaging disabled'); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | await ensureChannel(); |
| 55 | logger.info('User service RabbitMQ publisher initialized'); |
| 56 | }; |
| 57 | |
| 58 | export const closeMessaging = async () => { |
| 59 | try { |
| 60 | if (channel) { |
| 61 | const currentChannel: Channel = channel; |
| 62 | channel = null; |
| 63 | await currentChannel.close(); |
| 64 | } |
| 65 | if (connection) { |
| 66 | const currentConnection: ManagedConnection = connection; |
| 67 | connection = null; |
| 68 | await currentConnection.close(); |
| 69 | } |
| 70 | |
| 71 | logger.info('User service RabbitMQ publisher closed'); |
| 72 | } catch (error) { |
| 73 | logger.error({ err: error }, 'Error closing RabbitMQ connection/channel'); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | export const publishUserCreatedEvent = async (payload: UserCreatedPayload) => { |
| 78 | const ch = await ensureChannel(); |
| 79 | |
| 80 | if (!ch) { |
| 81 | logger.debug({ payload }, 'Skipping user.created event publish; messaging disabled'); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | const event: UserCreatedEvent = { |
| 86 | type: USER_CREATED_ROUTING_KEY, |
| 87 | payload, |
| 88 | occurredAt: new Date().toISOString(), |
| 89 | metadata: { version: 1 }, |
| 90 | }; |
| 91 | |
| 92 | try { |
| 93 | const sucess = ch.publish( |
| 94 | USER_EVENTS_EXCHANGE, |
| 95 | USER_CREATED_ROUTING_KEY, |
| 96 | Buffer.from(JSON.stringify(event)), |
| 97 | { contentType: 'application/json', persistent: true }, |
| 98 | ); |
| 99 | |
| 100 | if (!sucess) { |
| 101 | logger.warn({ event }, 'Failed to publish user.created event'); |
| 102 | } |
| 103 | } catch (error) { |
| 104 | logger.error({ err: error }, 'Error publishing user.created event'); |
| 105 | } |
| 106 | }; |