5

我需要访问公牛队列来查看工作统计并在页面上显示。我bull-repl用来从 CLI 访问队列,如下所示:

> 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    │
└───────────┴────────┘

我正在尝试使用以下代码从 JS 做同样的事情:

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();

第一个 shell.exec 运行,正在启动bull-repl,但需要在工具内运行的其余代码永远不会执行,我认为这是因为shelljs独立运行每个命令。如何让最后两个命令在工具中运行?

4

2 回答 2

11

队列#getJobCounts

getJobCounts() : Promise<JobCounts>

返回一个承诺,它将返回给定队列的作业计数。

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

要连接到 Redis db 中的队列并按状态返回和作业数量,请执行以下操作。

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));
于 2019-07-23T15:35:10.973 回答
0

编辑

更好地理解您的问题,REPL 已启动并等待输入。其他两个命令旨在在 REPL 环境中运行。尝试将您的命令输入bull-repl如下:

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

原始答案

尝试使用逻辑 AND 运算符 ( &&) 将您的命令链接到单个.exec()调用中。(有关此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;
}

&&保证上一个命令在启动下一个命令之前成功。

于 2019-07-22T16:20:41.460 回答