Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing bull-queue to view job stats from nodejs

I need to access bull-queue to view job stats and show on the page. I'm using bull-repl to access the queue from CLI, as follows:

> bull-repl 
BULL-REPL> connect marathon reddis://localhost:6379
Connected to reddis://localhost:6379, queue: marathon
BULL-REPL | marathon> stats
┌───────────┬────────┐
│  (index)  │ Values │
├───────────┼────────┤
│  waiting  │   0    │
│  active   │   0    │
│ completed │   55   │
│  failed   │   1    │
│  delayed  │   0    │
│  paused   │   0    │
└───────────┴────────┘

I'm trying to do the same thing from JS with the following code:

const shell = require('shelljs');
const ccommandExistsSync = require('command-exists').sync;

function installBullRepl(){
    if(ccommandExistsSync('bull-repl')){
        queueStats();
    } else{
        shell.exec('npm i -g bull-repl');
        queueStats();
    }
}

function queueStats(){
    let stats;

    shell.exec('bull-repl'); // launch `bull-repl`
    shell.exec('connect marathon reddis://localhost:6379'); // connect to redis instance
    stats = shell.exec(`stats`); // display count of jobs by groups

    return stats;
}

installBullRepl();

The first shell.exec runs, launching bull-repl, but the rest of the code that needs to be run inside the tool never executes, I assume it's because the shelljs runs each command independently. How can I get the last two commands to run within the tool?

like image 581
Yury Stanev Avatar asked Jul 22 '19 16:07

Yury Stanev


People also ask

Can I use bull without Redis?

Bull uses Redis to persist job data, so you'll need to have Redis installed on your system.

What is Bull in Nodejs?

Bull is a Node library that implements a fast and robust queue system based on redis.

What is job queue in Node JS?

A job queue is to solve a specific problem, usually with more to do than a single node. js process can handle at once so you "queue" up things to do and may even dole them out to other processes to handle.

What is BullMQ?

BullMQ is a Node. js library that implements a fast and robust queue system built on top of Redis that helps in resolving many modern age micro-services architectures.


1 Answers


Queue#getJobCounts

getJobCounts() : Promise<JobCounts>

Returns a promise that will return the job counts for the given queue.

  interface JobCounts {
    waiting: number,
    active: number,
    completed: number,
    failed: number,
    delayed: number
  }
}

To connect to the queue in Redis db and return and number of jobs by state do the following.

const Queue = require('bull');
const marathonQueue = new Queue('marathon', 'redis://127.0.0.1:6379');
marathonQueue.getJobCounts().then(res => console.log('Job Count:\n',res));
like image 115
Yury Stanev Avatar answered Oct 20 '22 20:10

Yury Stanev