Monitoring pending asynchronous operations in Node.js

I built Node.js a very stable application for robots, which basically sends requests to the API. To make sure that nothing can go wrong, I handle any possible error, and I set timeouts for promises, which can take too long ...

Now I would like to improve the application by deleting my security networks and tracking asynchronous search operations to find any “asynchronous leaks” like promises waiting forever or some strange result that I don't know about (which is the point of my question) .

Are there any tools designed to track async Node.js flow? For example, having received the total number of pending promises in the process at a given time? Or get a warning if a promise expects more than a certain time and keep track of that promise?

If this helps in the answers, here are the modules I use:

// Bluebird (promises) var Promise = require("bluebird"); // Mongoose with promises var mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); // Rate limiter with promises var Bottleneck = require("bottleneck"); // Promisified requests var request = require('request-promise'); 

Sorry for the fact that I could not accurately formulate my question: I do not know what I can expect / wish for sure ...


EDIT: So far, my research has led me to:

Since I am still developing the application and have life besides the application, I do not have much time to study it, but I am definitely going to seriously address this issue at some point!

+4
source share
1 answer

It looks like you might need some sort of job management.

I've been trying kue to manage asynchronous jobs lately, and that is pretty good.

It has an API to run and complete tasks. Each work can report on its progress. It comes with a built-in control panel that shows you the current tasks and their progress. It has an extensive set of events so that you can monitor the status of each task.

You need to install Redis to use it, but it is not difficult.

It does not support promises, but you can see in my sample code below that it is easy to run the task and then wrap it in a promise so that you can wait for the task to complete:

 const queue = kue.createQueue(); queue.process("my-job", 1, (job, done) => { const result = ... some result ... // Run your job here. if (an error occurs) { done(err); // Job failure. return; } done(null, result); // Job success }); function runMyJob () { return new Promise((resolve, reject) => { const job = queue.create("my-job").save(); job.on('complete', result => { resolve(result); // Job has completed successfully, resolve the promise. }); job.on("failed", err => { reject(err); // Job has failed, reject the promise. }); }); }; runMyJob() .then(() => { console.log("Job completed.") }) .catch(err => { console.error("Job failed."); }); 

It is quite easy to do this work in parallel to several processors using the cluster module Node.js.

Learn more about the kue webpage.

+1
source

Source: https://habr.com/ru/post/1265594/


All Articles