访问 bull-queue 以查看来自 nodejs 的作业统计信息

accessing bull-queue to view job stats from nodejs

我需要访问 bull-queue 才能查看作业统计信息并显示在页面上。我正在使用 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 运行s,启动 bull-repl,但工具中需要 运行 的其余代码永远不会执行,我认为是因为 shelljs 运行s 每个命令都是独立的。如何在工具中获取最后两个命令 运行?

编辑

更好地理解您的问题,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;
}

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


队列#getJobCounts

getJobCounts() : Promise<JobCounts>

Returns 承诺 return 给定队列的作业计数。

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

要连接到 Redis 数据库中的队列和 return 以及按状态划分的作业数,请执行以下操作。

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