在Node.js承诺的环境中监视挂起的异步操作 [英] Monitoring pending async operations in Node.js promised environment

查看:126
本文介绍了在Node.js承诺的环境中监视挂起的异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node.js中构建了一个非常稳定的机器人应用程序,它基本上可以将请求连续发送到API。为了确保没有任何问题可以解决,我处理任何可能的错误,并且我已经设定了可能需要很长时间才能解决的承诺超时...

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 have set timeouts for promises that could take too long to resolve...

现在,我想通过删除我的安全网来改进应用程序,并监视异步操作以找到任何类型的异步泄漏,例如永久未决的承诺或任何我不知道的奇怪结果(这是我的问题的重点)。

Now, I would like to improve the app by removing my safety nets, and monitoring async operations to find any kind of "async leaking", eg promises pending forever or any strange outcome I'm not aware of (that's the point of my question).

是否有任何工具可用于监控Node.js异步流?例如,在给定时间内获取流程中的待处理承诺总额?或者,如果任何承诺已超过给定时间,并且跟踪该承诺,则会收到警告?

Are there any tools meant to monitor the Node.js async flow ? For instance, getting the total amount of pending promises in the process at a given time ? Or getting a warning if any promise has been pending for more than a given time, and tracking that promise ?

如果这可以指导答案,以下是我使用的模块:

If that may guide 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 not being able to formulate my question precisely : I have no clue as to what I can expect/wish for exactly...

编辑:所以到目前为止,我的研究已经让我:

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


  • Bluebird的资源管理工具,但我找不到让它们变得有用的方法

  • 令人惊讶的npm 监视器和附带的监视器 - 仪表板,但由于某种原因我还不能让它工作我的需求...

  • Bluebird's resource management tools, but I can't figure out a way to make them useful
  • The amazing npm monitor and the shipped-in monitor-dashboard, but for some reason I can't yet make it work for my needs...

由于我还在开发应用程序,除了应用程序之外还有其他生活,我没有太多东西是时候看看了,但我肯定会在某个时候认真地解决这个问题!

Since I'm still developing the app and have a life besides the app, I don't have much time to look into it, but I'm definitely going to address this question seriously at some point !

推荐答案

听起来你可能会需要某种工作管理。

Sounds like you might need some kind of job management.

我最近一直在尝试kue来管理异步工作,而且非常好。

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

它有一个用于启动和运行作业的API。每个工作都可以报告它的进度。它带有一个内置的作业仪表板,可以显示正在运行的作业及其进度。它有一系列广泛的事件,以便您可以监视每个作业的状态。

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.

您需要安装Redis才能使用它,但这并不困难。

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

它不支持promises,但你可以在下面的代码示例中看到,很容易启动一个作业,然后将它包装在一个promise中,以便你可以等待作业完成:

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.");
    });

使用Node.js集群模块在多个CPU上并行工作相当容易。

It's fairly easy to make this work in parallel over multiple CPUs using the Node.js cluster module.

在kue网页上阅读更多内容。

这篇关于在Node.js承诺的环境中监视挂起的异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆