auth-consumer.ts111 lines, 2.8 KB
| 1 | import { |
| 2 | AUTH_EVENT_EXCHANGE, |
| 3 | AUTH_USER_REGISTERED_ROUTING_KEY, |
| 4 | type AuthRegisteredEvent, |
| 5 | } from '@chatapp/common'; |
| 6 | |
| 7 | import { |
| 8 | connect, |
| 9 | type Channel, |
| 10 | type ChannelModel, |
| 11 | type Connection, |
| 12 | type ConsumeMessage, |
| 13 | type Replies, |
| 14 | } from 'amqplib'; |
| 15 | |
| 16 | import { env } from '@/config/env'; |
| 17 | import { logger } from '@/utils/logger'; |
| 18 | import { userService } from '@/services/user.service'; |
| 19 | |
| 20 | type ManageConnection = Connection & ChannelModel; |
| 21 | |
| 22 | let connectionRef: ManageConnection | null = null; |
| 23 | let channel: Channel | null = null; |
| 24 | let consumerTag: string | null = null; |
| 25 | |
| 26 | const QUEUE_NAME = 'auth-service.auth-events'; |
| 27 | |
| 28 | const closeConnection = async (conn: ManageConnection) => { |
| 29 | await conn.close(); |
| 30 | connectionRef = null; |
| 31 | channel = null; |
| 32 | consumerTag = null; |
| 33 | }; |
| 34 | |
| 35 | const handleMessage = async (message: ConsumeMessage, ch: Channel) => { |
| 36 | const raw = message.content.toString('utf-8'); |
| 37 | const event = JSON.parse(raw) as AuthRegisteredEvent; |
| 38 | |
| 39 | await userService.syncFromAuthUser(event.payload); |
| 40 | |
| 41 | ch.ack(message); |
| 42 | }; |
| 43 | |
| 44 | export const startAuthEventConsumer = async () => { |
| 45 | if (!env.RABBITMQ_URL) { |
| 46 | logger.warn('RabbitMQ URL is not configured, skip'); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (channel) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | const connection = (await connect(env.RABBITMQ_URL)) as ManageConnection; |
| 55 | connectionRef = connection; |
| 56 | const ch = await connection.createChannel(); |
| 57 | channel = ch; |
| 58 | |
| 59 | await ch.assertExchange(AUTH_EVENT_EXCHANGE, 'topic', { durable: true }); |
| 60 | const queue = await ch.assertQueue(QUEUE_NAME, { durable: true }); |
| 61 | await ch.bindQueue(queue.queue, AUTH_EVENT_EXCHANGE, AUTH_USER_REGISTERED_ROUTING_KEY); |
| 62 | |
| 63 | const consumeHandler = (msg: ConsumeMessage | null) => { |
| 64 | if (!msg) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | void handleMessage(msg, ch).catch((error: unknown) => { |
| 69 | logger.error({ err: error }, 'Failed to proccess auth event'); |
| 70 | ch.nack(msg, false, false); |
| 71 | }); |
| 72 | }; |
| 73 | |
| 74 | const result: Replies.Consume = await ch.consume(queue.queue, consumeHandler); |
| 75 | consumerTag = result.consumerTag; |
| 76 | |
| 77 | connection.on('close', () => { |
| 78 | logger.warn('Auth consumer connection closed'); |
| 79 | connectionRef = null; |
| 80 | channel = null; |
| 81 | consumerTag = null; |
| 82 | consumerTag = null; |
| 83 | }); |
| 84 | |
| 85 | connection.on('error', (error) => { |
| 86 | logger.error({ err: error }, 'Auth consumer connection error'); |
| 87 | }); |
| 88 | |
| 89 | logger.info('Auth event consumer started'); |
| 90 | }; |
| 91 | |
| 92 | export const stopAuthEventConsume = async () => { |
| 93 | try { |
| 94 | const ch = channel; |
| 95 | if (ch && consumerTag) { |
| 96 | await ch.cancel(consumerTag); |
| 97 | consumerTag = null; |
| 98 | } |
| 99 | if (ch) { |
| 100 | await ch.close(); |
| 101 | channel = null; |
| 102 | } |
| 103 | const conn = connectionRef; |
| 104 | if (conn) { |
| 105 | await closeConnection(conn); |
| 106 | connectionRef = null; |
| 107 | } |
| 108 | } catch (error) { |
| 109 | logger.error({ err: error }, 'Failed to stop auth event consumer'); |
| 110 | } |
| 111 | }; |