Redis BullMQ - Closing connections/Deleting Queue related data

134 views Asked by At

I have a multiple-instance environment where there will be more than one instance of Node.js servers running. And there will be a sequence of messages received to the servers from different visitors. I am using Redis BullMQ (V4) to implement a Message Queue system to create a Queue for each visitor and process their messages through this Queue so that one visitor doesn't have to wait for other visitors' messages to be processed. After a certain period, I need to close all the connections to the Redis and delete the Queue and Worker data.

The following snippet creates a queue, worker for the queue.

// Queue & Worker are the classes from bullmq package.
// const { Queue, Worker } = require('bullmq'); 

createMessageQueue = (createMessageQueueRequest, createMessageQueueResponse) => {
    try {
        let queueName = 'queue#@#' + createMessageQueueRequest.body.visitorId;
        let messageQueue = new Queue(queueName);

        const worker = new Worker(queueName, async job => {
            try {
                */
                    Job Processing Code
                */
            } catch (error) {
                console.log('Error processing the job : ', error);
                createMessageQueueResponse.status(400).send({
                    message: 'ERROR',
                    data: error
                });
            }
        });
            
        createMessageQueueResponse.status(200).send({
            message: 'SUCCESS'
        });
    } catch (error) {
        console.log('Error in creation of Queue : ', error);
        createMessageQueueResponse.status(500).send({
            message: 'ERROR',
            data: error
        });
    }
};

Question 1. After all the jobs are added and processed, I want to clear all the data used to process the jobs, say the Queue, Worker, and Redis Connections. How can I achieve this? Note: I might not have the Queue or Worker instance I received during their creation.

Question 2. While creating Queue or Worker, I am not using the 'IORedis' package to pass the IORedis instance to the connection object. Instead, I am passing Redis host & port values. What is the efficient way to handle the Redis connections with Redis BullMQ?

0

There are 0 answers