8

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?

Yury Stanev
  • 419
  • 1
  • 5
  • 16

2 Answers2

16

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));
Yury Stanev
  • 419
  • 1
  • 5
  • 16
0

Edit

Better understanding your question, the REPL is started and awaiting input. The other two commands are meant to run inside the REPL environment. Try piping your commands into bull-repl like so:

function queueStats(){
    let stats;

    stats = shell.exec('echo "connect marathon reddis://localhost:6379 && stats" | 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;
}

Original Answer

Try chaining your commands with the logical AND operator (&&) into a single .exec() call. (more info on this https://www.howtogeek.com/269509/how-to-run-two-or-more-terminal-commands-at-once-in-linux/)

function queueStats(){
    let stats;
    const commands = [
      'bull-repl',
      'connect marathon reddis://localhost:6379',
      'stats'
    ];

    stats = shell.exec(commands.join(' && '));

    // 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;
}

&& guarantees the previous command is successful before initiating the next.

SteamDev
  • 4,294
  • 5
  • 20
  • 29
  • That doesn't work as I'm not trying to chain them. `bull-repl` doesn't have any output it launches the tool. I've tried `child_process.execFileSync('bull-repl', {stdio: 'inherit'})` as suggested in `shelljs` FAQ, but the results are the same, it only launches the tool. – Yury Stanev Jul 22 '19 at 18:36
  • The "chaining" would allow the commands to execute within the same child process. Do you see any messages in `stats.stdout` or `stats.stderr`? – SteamDev Jul 22 '19 at 18:46
  • 1
    No, I've chained as suggested ```console.log( shell.exec(`bull-repl &&\ connect marathon reddis://localhost:6379 &&\ stats`) )``` all this does it spawn the initial process, the other 2 commands don't run. – Yury Stanev Jul 22 '19 at 18:55
  • Okay I better understand your situation, `bull-repl` is awaiting user input, `connect` and `stats` only work in the repl environment. I think I have a solution for that. Will edit answer. – SteamDev Jul 22 '19 at 20:04
  • Yeah, that's correct. I guess the way I worded it was confusing. Thanks. – Yury Stanev Jul 22 '19 at 20:12
  • That partially does the `connect` runs. I've been modifying the `echo` part, but I can't get the `stats` to run. ``echo "connect marathon reddis://localhost:6379\n `echo stats\n`" | bull-repl`` – Yury Stanev Jul 22 '19 at 20:24
  • actually any piped input seems to break the API – Yury Stanev Jul 22 '19 at 20:37