Rubén M
Rubén M

Reputation: 119

Amqp nodejs disconnect event

I'm using amqp in my nodejs scrapping service. Sometimes I get random disconnects and when it autoreconnects, it starts a new whenConnected promise but the previous is still running so my app mess up:

    const connection = amqp.connect(configuration.RABBIT_URL)
    console.log("[AMQP] connecting");

    connection.on('connect', () => { console.log('Connected!'); whenConnected(connection); })
    connection.on('disconnect', params => { console.log('Disconnected: ', params.err) })
    connection.on('connectFailed', params => { console.log('connectFailed: ', params) })
    connection.on('unblocked', () => { console.log('Unblocked') })
    connection.on('blocked', params => { console.log('Blocked, reason: ', params.reason) })
const whenConnected = async (connection: IAmqpConnectionManager) => {
    try {
        const channel = connection.createChannel({
            setup: async (channel: Channel) => {
                var args = [];
                args["x-queue-mode"] = "lazy"
                args["x-max-priority"] = 10
                var msgQueueCrawl = await channel.assertQueue(configuration.CRAWLQUEUE, { durable: true, arguments: args });
                channel.prefetch(1)
                console.log("Queue lenght", msgQueueCrawl.messageCount)
                if (msgQueueCrawl.messageCount == 0) {
                    stopInstanceStop = false;
                    await stopInstance();
                }

                await channel.consume(configuration.CRAWLQUEUE, async (msg) => {
                   ...some stuff
                })

            }
        })
    }

    catch (e) {
        Sentry.captureException(e);
    }
}

How could I cancel the first Promise if there is a disconnect event so it starts a fresh one? Thanks

Upvotes: 0

Views: 6

Answers (0)

Related Questions