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

Vickrant picture Vickrant · Feb 17, 2014 · Viewed 24.7k times · Source

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.

Answer

The Lazy Coder picture The Lazy Coder · Sep 16, 2015

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.

It was processing a directory and putting data into 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.

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.exit at the end.

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.