3

I am building Node.js scripts that will be ran as CRON jobs. (full terminal scripts). These scripts fetch data from all around using APIs and MongoDB (native driver) is used. I don't didn't use db.close() statement and because of that script will never end by itself (or at least it looks like that way from the terminal), to finish it, it is necessary to press CTRL+C to cancel.

Back then when I was writing these scripts, someone from Stack overflow told me that it is not required to close connection anyway. So I let it be.

Now I wonder, do these scripts actually are still running? And as these would be ran as CRON jobs, with small intervals, does that mean that these scripts will eventually kill RAM from the server? Does that mean, there will be thousands scripts running and waiting for db.close() statement?

Example code:

MongoClient.connect(mongoUrl, (err, db) => {
  if (err) {
    console.log(err);
    return;
  }

  var usersCollection = db.collection('users').find();

  usersCollection.on('data', (doc) => {

    console.log(doc);

  });
Kunok
  • 8,089
  • 8
  • 48
  • 89

1 Answers1

3

Node scripts exit by themselves only when nothing listens for events any more.

With your scripts you probably know when nothing needs to be done after the main purpose of your script is achieved. For example, when the purpose of your script is to execute a certain code, print a summary etc. then you can add process.exit(0); after that command to make sure that the script finishes when it should.

In the case like this one when you don't have a one line of summary after which you can exit, you can listen for a certain event and exit when when it arrives. For example:

usersCollection.on('end', process.exit);

In addition to a process.exit in the right spot, you can also set a timeout to terminate the script after a certain time. For example:

setTimeout(process.exit, 20*1000);

will terminate the script after 20 seconds. This can be added just in case that somethings goes wrong with the database or the connection and you never get the 'end' event, or it takes too long to wait for it.

Sometimes adding process.exit(0); for scripts like this may be more convenient than closing all the database connections that may be open at that time or other things that may prevent your script from terminating.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Are you sure that is good idea? If I understand this properly, If I use `process.exit(0)`; after `console.log(doc)`, it will get only first doc and then kill script? – Kunok Sep 13 '16 at 08:55
  • @Kunok you're right. I didn't read your script carefully. you should add `process.exit(0);` when you're done doing what your script needs to be done, not befor that of course. – rsp Sep 13 '16 at 08:57
  • I am using Node for a week only and I am still not too comfortable with it, would it be a good idea to set timeout to kill script? For example if script is executed every 10 mins, let it die before new CRON job execution? – Kunok Sep 13 '16 at 09:02
  • You can set a timeout instead of (or in addition to) an explicit process.exit that I wrote about, to make extra sure that your scripts terminate before the new ones are started. `setTimeout(process.exit, 60*1000);` will make it exit after a minute. – rsp Sep 13 '16 at 09:05
  • By the way, is there an event that can detect when `on('data~` method reached the end ? – Kunok Sep 13 '16 at 09:06
  • @Kunok there should be such an event - I would guess called `on('end',...` - which module are you using? – rsp Sep 13 '16 at 09:08
  • What do you mean by module? Only thing I required is `googleapis`, everything else is probably native Node.js 6.5.0. I use native mongodb drvier for mongo database. – Kunok Sep 13 '16 at 09:09
  • I was asking about the mongo driver. Ok, so I would guess: `usersCollection.on("end", process.exit);` should do the trick. – rsp Sep 13 '16 at 09:10
  • Thanks, please include that into answer too. – Kunok Sep 13 '16 at 09:11
  • 1
    @Kunok ok, I added it to the answer. I hope it helps. – rsp Sep 13 '16 at 09:16