Monitoring pending async operations in Node.js promised environment

后端 未结 1 379
青春惊慌失措
青春惊慌失措 2020-12-19 13:24

I have built in Node.js a very stable robot app that basically sends requests continuously to an API. To make sure nothing can go wrong, I handle any possible error and I ha

1条回答
  •  囚心锁ツ
    2020-12-19 14:16

    Sounds like you might need some kind of job management.

    I've been trying kue lately to manage asynchronous jobs and it's pretty good.

    It has an API for starting and running jobs. Each job can report it's progress. It comes with a builtin job dashboard that shows you the running jobs and their progress. It has an extensive set of events so that you can monitor the status of each job.

    You need to install Redis to use this, but that's not difficult.

    It doesn't support promises, but you can see in my code example below that it's easy to start a job and then wrap it in a promise so that you can await job completion:

    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's fairly easy to make this work in parallel over multiple CPUs using the Node.js cluster module.

    Read more on the kue web page.

    0 讨论(0)
提交回复
热议问题