Node.js 访问bull队列以查看来自nodejs的作业统计信息
我需要访问bull queue以查看作业统计信息并显示在页面上。我使用Node.js 访问bull队列以查看来自nodejs的作业统计信息,node.js,task-queue,shelljs,Node.js,Task Queue,Shelljs,我需要访问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)
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
独立运行每个命令。如何在工具中运行最后两个命令?Edit
为了更好地理解您的问题,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()
调用中。(更多信息)
&&
保证在启动下一个命令之前,上一个命令是成功的。队列#获取作业计数
getJobCounts():承诺
返回将返回给定队列的作业计数的承诺
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));
这不起作用,因为我不想把他们锁起来
bull repl
在启动工具时没有任何输出。我尝试了child\u process.execFileSync('bull-repl',{stdio:'inherit'})
如shelljs
FAQ中所建议的,但是结果是一样的,它只启动工具。“链接”将允许命令在同一个子进程中执行。您是否在stats.stdout
或stats.stderr
中看到任何消息?没有,我已按建议链接了console.log(shell.exec(`bull repl&&\connect)reddis://localhost:6379 &&\stats`)
所有这些都会产生初始进程,其他两个命令不会运行。好吧,我更好地理解你的情况,bull repl
正在等待用户输入,connect
和stats
仅在repl环境中工作。我想我有一个解决办法。将编辑答案。是的,没错。我想我的措辞让人困惑。谢谢
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));