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.
source share