Node.js 持续显示节点队列上的内容的最佳方式(使用瓶颈)
我使用的是名为瓶颈的节点包,我基本上希望队列中运行的所有节点都有一个恒定的日志 如果可以运行它,以便作业一到达队列,它就会在控制台窗口中显示以下内容:Node.js 持续显示节点队列上的内容的最佳方式(使用瓶颈),node.js,angular,npm,queue,node-modules,Node.js,Angular,Npm,Queue,Node Modules,我使用的是名为瓶颈的节点包,我基本上希望队列中运行的所有节点都有一个恒定的日志 如果可以运行它,以便作业一到达队列,它就会在控制台窗口中显示以下内容: ************ CURRENTLY IN QUEUE ********** Name: name_of_api_called ID: id_of_call Priority: priority_given Weight: weight_given Recieved: date_and_time_recieved Status: WAI
************ CURRENTLY IN QUEUE **********
Name: name_of_api_called
ID: id_of_call
Priority: priority_given
Weight: weight_given
Recieved: date_and_time_recieved
Status: WAITING / ON QUEUE
Name: name_of_api_called
ID: id_of_call
Priority: priority_given
Weight: weight_given
Recieved: date_and_time_recieved
Status: WAITING / ON QUEUE
******************************************
然后,一旦完成,它就会将其从队列日志中删除。是否有一种方法可以对其进行持续监控,以使其具有实时队列日志。如果队列中没有作业,则只显示
*****队列中没有任何内容******以防万一有人读到这篇文章-我只是做了以下操作: **这可能不是最理想或最好的解决方案,但它满足了我的需要**
const queue = new Array();
const success = new Array();
const error = new Array();
let i = 0;
let j = 0;
// Once the limiter is idle, print out all the stats.
limiter.on("idle", function () {
// Give it a second to recieve the final success statement
setTimeout(() => {
console.log('************ QUEUE STATS *****************');
(success.length === 0) ? console.log('NOTHING RUNNING') : console.log(success);
(error.length === 0) ? console.log('NO ERRORS') : console.log(error);
console.log('******************************************');
// Reset variables for next run.
queue.length = 0;
success.length = 0;
error.length = 0;
i = 0;
}, 1000);
});
// Show a live status on what is happening in the queue.
limiter.on("queued", function (info) {
console.log(limiter.counts());
});
// Add the times to each of the id's added to the queue so that we can work out how long each takes
function addToQueue(id, api, company) {
if (!queue[id]) {
queue[id] = new Date();
}
}
// Once successful we take the time the id was added to the queue and then minus it from the time now. Store a string of information to print out later (on idle)
function addToSuccess(id, api, company) {
if (queue[id]) {
const timeToComplete = new Date() - queue[id];
success[i] = 'ID: ' + id + ' | API: /' + api + ' | Company: ' + company + ' | Status: Completed with Success | Time to complete: ' + timeToComplete / 10000 + ' seconds';
i++;
}
}
// If there is an error then work out the data and store as an error.
function addToError(id, api, company) {
if (queue[id]) {
const timeToComplete = new Date() - queue[id];
error[j] = 'ID: ' + id + ' | API: /' + api + ' | Company: ' + company + ' | Status: Error | Time to complete: ' + timeToComplete / 10000 + ' seconds';
j++;
}
}
// in order to get a really random number for the ID to track it (running the api multiple times, so makes it easier to distinguish the id)
const id = Math.floor(( Math.random() * 1000) * ( Math.random() * 10) / Math.random() * 100);
const ops = {
priority: 2,
id: id
};
addToQueue(ops.id, 'api', body.customerId);
limiter.schedule(ops, () => axios.post(url, body, {
headers
})).then((res) => {
addToSuccess(ops.id, 'api', body.customerId);
res.status(200).json(JSON.parse(res.data));
}).catch((error) => {
addToError(ops.id, 'api', body.customerId);
res.status(500).send(error);
});