Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 244
Vickrant Avatar asked Feb 17 '14 14:02

Vickrant


People also ask

How do you exit a JavaScript script?

If you're in the REPL (i.e. after running node on the command line), you can type . exit to exit.

How do I exit node js from command prompt?

Now, you can execute mynodejs-app from command prompt as shown below. To exit from the REPL terminal, press Ctrl + C twice or write . exit and press Enter.

How do I exit a node js script?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.


2 Answers

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.

like image 175
The Lazy Coder Avatar answered Oct 21 '22 00:10

The Lazy Coder


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.

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.

like image 43
awinder Avatar answered Oct 21 '22 01:10

awinder