[英]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運行,啟動bull-repl
,但是需要在工具內運行的其余代碼從未執行,我認為這是因為shelljs
獨立運行每個命令。 我如何才能在工具中運行最后兩個命令?
getJobCounts() : Promise<JobCounts>
返回一個promise,該promise將返回給定隊列的作業計數。
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));
編輯
為了更好地理解您的問題,REPL已啟動並等待輸入。 其他兩個命令旨在在REPL環境中運行。 嘗試將命令bull-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;
}
&&
確保在啟動下一個命令之前成功執行上一個命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.