MySQL 成功执行后,我的 nodejs 脚本没有自行退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21831493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:04:26  来源:igfitidea点击:

my nodejs script is not exiting on its own after successful execution

mysqlnode.jssolrscripting

提问by Vickrant

I have written a script to update my db table after reading data from db tables and solr. I am using asyn.waterfall module. The problem is that the script is not getting exited after successful completion of all operations. I have used db connection pool also thinking that may be creating the script to wait infinitly. I want to put this script in crontab and if it will not exit properly it would be creating a hell lot of instances unnecessarily.

我编写了一个脚本来在从 db 表和 solr 读取数据后更新我的 db 表。我正在使用 asyn.waterfall 模块。问题是在成功完成所有操作后脚本没有退出。我使用过 db 连接池也认为可能正在创建脚本以无限等待。我想将此脚本放在 crontab 中,如果它不能正确退出,则会不必要地创建大量实例。

回答by The Lazy Coder

I just went through this issue.

我刚刚经历了这个问题。

The problem with just using process.exit()is that the program I am working on was creating handles, but never destroying them.

只是使用的问题process.exit()是我正在处理的程序正在创建句柄,但从未销毁它们。

It was processing a directory and putting data into orientdb.

它正在处理一个目录并将数据放入orientdb

so some of the things that I have come to learn is that database connections need to be closed before getting rid of the reference. And that process.exit()does not solve all cases.

所以我学到的一些东西是在摆脱引用之前需要关闭数据库连接。这process.exit()并不能解决所有情况。

When my project processed 2,000 files. It would get down to about 500 left, and the extra handles would have filled up the available working memory. Which means it would not be able to continue. Therefore never reaching the process.exitat the end.

当我的项目处理 2,000 个文件时。它会减少到大约 500 个,额外的句柄会填满可用的工作内存。这意味着它将无法继续。因此永远不会到达process.exit最后。

On the other hand, if you close the items that are requesting the app to stay open, you can solve the problem at its source.

另一方面,如果您关闭请求应用程序保持打开状态的项目,则可以从源头上解决问题。

The two "Undocumented Functions" that I was able to use, were

我能够使用的两个“未记录的函数”是

process._getActiveHandles();
process._getActiveRequests();

I am not sure what other functions will help with debugging these types of issues, but these ones were amazing.

我不确定还有哪些其他功能可以帮助调试这些类型的问题,但这些功能非常棒。

They return an array, and you can determine a lot about what is going on in your process by using these methods.

它们返回一个数组,您可以通过使用这些方法确定很多关于您的过程中正在发生的事情。

I just hope that helps anyone else stumbling across this post.

我只是希望能帮助其他在这篇文章中绊倒的人。

回答by awinder

You have to tell it when you're done, by calling

你必须在完成后告诉它,通过调用

process.exit();

More specifically, you'll want to call this in the callback from async.waterfall() (the second argument to that function). At that point, all your asynchronous code has executed, and your script should be ready to exit.

更具体地说,您需要在 async.waterfall() (该函数的第二个参数)的回调中调用它。此时,您的所有异步代码都已执行,您的脚本应该准备好退出。

EDIT: As pointed out by @Aaron below, this likely has to do with something like a database connection being active, and not allowing the node process to end.

编辑:正如下面@Aaron 所指出的,这可能与数据库连接处于活动状态有关,并且不允许节点进程结束。

回答by Kim Kern

You can use the node module why-is-node-running:

您可以使用节点模块why-is-node-running

1) Run npm install -D why-is-node-running

1) 运行 npm install -D why-is-node-running

2) Add import * as log from 'why-is-node-running';in your code

2)添加import * as log from 'why-is-node-running';你的代码

3) When you expect your program to exit, add a log statement:

3)当您希望程序退出时,添加一条日志语句:

afterAll(async () => {
  await app.close();
  log();
}


This will print a list of open handles with a stacktrace to find out where they originated:

这将打印一个带有堆栈跟踪的打开句柄列表,以找出它们的来源:

There are 5 handle(s) keeping the process running
# Timeout
/home/maf/dev/node_modules/why-is-node-running/example.js:6  - setInterval(function () {}, 1000)
/home/maf/dev/node_modules/why-is-node-running/example.js:10 - createServer()

# TCPSERVERWRAP
/home/maf/dev/node_modules/why-is-node-running/example.js:7  - server.listen(0)
/home/maf/dev/node_modules/why-is-node-running/example.js:10 - createServer()

回答by Lusfikar Sheba

We can quit the execution by using:

我们可以使用以下命令退出执行:

connection.destroy();

回答by Thai

If you use Visual Studio code, you can attach to an already running Node scriptdirectly from it.

如果您使用 Visual Studio 代码,则可以直接从它附加到已运行的 Node 脚本

First, run the Debug: Attached to Node Processcommand:

首先,运行Debug: Attached to Node Process命令:

screenshot of a command palette

命令面板的屏幕截图

When you invoke the command, VS Code will prompt you which Node.js process to attach to:

当您调用该命令时,VS Code 会提示您附加到哪个 Node.js 进程:

A quick pick view with Node processes listed

列出节点进程的快速选择视图

Your terminal should display this message:

您的终端应显示此消息:

Debugger listening on ws://127.0.0.1:9229/<...>
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

Debugger listening on ws://127.0.0.1:9229/0f06c9ce-35c1-4b12-95a8-003882ef97dd / For help, see: https://nodejs.org/en/docs/inspector / Debugger attached.

调试器监听 ws://127.0.0.1:9229/0f06c9ce-35c1-4b12-95a8-003882ef97dd / 如需帮助,请参阅:https://nodejs.org/en/docs/inspector / 调试器附件。

Then, inside your debug console, you can use the code from The Lazy Coder's answer:

然后,在您的调试控制台中,您可以使用The Lazy Coder's answer 中的代码:

process._getActiveHandles();
process._getActiveRequests();

Debug console

调试控制台