Node.js 获取正在运行的进程/承诺

huangapple go评论59阅读模式
英文:

nodejs getting running processs/promises

问题

在Node应用程序中检查是否有任何Promise正在运行的方法?假设我们有以下代码片段:

router.post('/someEndpoint', async (req, res) => {
    await someFunctions();
});

我们想要创建一个端点 /getRunningProcess 来检查应用程序中是否有任何运行中的进程。是否有一种通用的方法来做到这一点,而不是编写特定于应用程序的代码来逐个检查每个进程的状态?/getRunningProcess 的代码应该类似于这样:

router.get('/getRunningProcess', (req, res) => {
   // 检查运行中的进程或活动Promise的代码
   ...
   res.status(200).json({ "process": process })
});

最初,我们计划通过编写特定于应用程序的代码来检查应用程序中是否有任何运行中的进程,但这将花费我们相当多的时间。

英文:

Is there a way to check if any promises are running in a Node application? Let's say we have the following code snippet:

router.post('/someEndpoint', async (req, res) => {
    await someFunctions();
});

We want to create an endpoint /getRunningProcess to check if there are any running processes in the application. Is there a generic way to do this instead of writing application-specific code to check the status of each process individually? The code for /getRunningProcess should look something like this:

router.get('/getRunningProcess', (req, res) => {
   // code to check for running processes or active promises
   ...
   res.status(200).json({ "process": process})
});

we initially planned on doing it the proper way by writing application specific code to status check the application for any running processes but it would take us a considerable amount of time to do it for everything.

答案1

得分: 1

我假设您想要计算未解决请求的数量。

没有内置的方法来查找处于待定状态的 promise 数量。您可以使用 on-finished 中间件来跟踪未解决的请求。

此外,看起来您正在使用 expressjs。因此,我将使用 expressjs 语法编写一个示例。

您可以创建一个中间件来跟踪未解决的请求。

const onFinished = require('on-finished')

const unresolvedRequestIds = new Set()
const requestTracker = (_, res, next) => {
  const id = Math.random();
  unresolvedRequestIds.add(id)
  onFinished(res, () => {
    unresolvedRequestIds.delete(id)
  });
  next();
};

现在,您可以在任何自定义中间件之前使用这个 requestTracker 中间件:

router.post('/someEndpoint', requestTracker, async (req, res) => {
  ...
});

要获取未解决请求的数量,您可以简单地计算 unresolvedRequestIds 中的条目数量:

router.get('/getUnresolvedRequestCount', (req, res) => {
   res.status(200).json({ unresolvedRequestCount: unresolvedRequestIds.size })
});

而不是在所有异步路由中单独添加 requestTracker 中间件,您可以将其添加到应用程序的顶层。但请注意,这可能还会计算非异步未解决的请求。

app.use(requestTracker);
英文:

I am assuming that you want to count the number of unresolved requests.

There is no built-in way to find the count of promises in the pending state. You could make use of the on-finished middleware to keep track of unresolved requests.

Also, it looks like you are using expressjs. So, I will write a sample using expressjs syntax.

You could create a middleware to keep track of unresolved requests.

const onFinished = require('on-finished')

const unresolvedRequestIds = new Set()
const requestTracker = (_, res, next) => {
  const id = Math.random();
  unresolvedRequestIds.add(id)
  onFinished(res, () => {
    unresolvedRequestIds.delete(id)
  });
  next();
};

Now, you can use this requestTracker middleware before any custom middleware:

router.post('/someEndpoint', requestTracker, async (req, res) => {
  ...
});

To get your unresolved request count, you could simply count the number of entries in unresolvedRequestIds:

router.get('/getUnresolvedRequestCount', (req, res) => {
   res.status(200).json({ unresolvedRequestCount: unresolvedRequestIds.size })
});

Instead of adding the requestTracker middleware in all your async routes individually, you can add it at the top level of the app. But do note that this may also count non-async unresolved requests.

app.use(requestTracker);

huangapple
  • 本文由 发表于 2023年6月16日 13:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定