prevent NodeJS program from exiting
Asked Answered
U

3

19

I am creating NodeJS based crawler, which is working with node-cron package and I need to prevent entry script from exiting since application should run forever as cron and will execute crawlers at certain periods with logs.

In the web application, server will listen and will prevent from terminating, but in serverless apps, it will exit the program after all code is executed and won't wait for crons.

Should I write while(true) loop for that? What is best practices in node for this purpose?

Thanks in advance!

Unproductive answered 23/5, 2017 at 14:22 Comment(0)
D
21

Because nodejs is single thread a while(true) will not work. It will just grab the whole CPU and nothing else can ever run.

nodejs will stay running when anything is alive that could run in the future. This includes open TCP sockets, listening servers, timers, etc...

To answer more specifically, we need to see your code and see how it is using node-cron, but you could keep your nodejs process running by just adding a simple setInterval() such as this:

setInterval(function() {
    console.log("timer that keeps nodejs processing running");
}, 1000 * 60 * 60);

But, node-cron itself uses timers so it appears that if you are using node-cron properly and you correctly have tasks scheduled to run in the future, then your nodejs process should not stop. So, I suspect that your real problem is that you aren't correctly scheduling a task for the future with node-cron. We could help you with that issue only if you show us your actual code that uses node-cron.

Dabster answered 23/5, 2017 at 15:52 Comment(1)
You are right, my crons have not been running, since I have used wrong path for module, though it was silently exiting and I could not see errors, since for paths I am using globUnproductive
P
35

You can begin reading from the process' standard input:

import process from 'process';

process.stdin.resume();

// do your thing

This will prevent the process from immediate termination.

This is mentioned in the official documentation.

However, this could prevent your process from being gracefully killed.

Poulenc answered 15/6, 2018 at 9:58 Comment(2)
This works for me. Also, if you want to exit your app on Ctrl + C, you can add this: process.on('SIGINT', () => { process.exit(); });Understructure
Ctrl + C works fine without any signal handlers.Mitziemitzl
D
21

Because nodejs is single thread a while(true) will not work. It will just grab the whole CPU and nothing else can ever run.

nodejs will stay running when anything is alive that could run in the future. This includes open TCP sockets, listening servers, timers, etc...

To answer more specifically, we need to see your code and see how it is using node-cron, but you could keep your nodejs process running by just adding a simple setInterval() such as this:

setInterval(function() {
    console.log("timer that keeps nodejs processing running");
}, 1000 * 60 * 60);

But, node-cron itself uses timers so it appears that if you are using node-cron properly and you correctly have tasks scheduled to run in the future, then your nodejs process should not stop. So, I suspect that your real problem is that you aren't correctly scheduling a task for the future with node-cron. We could help you with that issue only if you show us your actual code that uses node-cron.

Dabster answered 23/5, 2017 at 15:52 Comment(1)
You are right, my crons have not been running, since I have used wrong path for module, though it was silently exiting and I could not see errors, since for paths I am using globUnproductive
M
3

Building off @jfriend00's answer I did this, so it's killable

var running = true;

function killProcess() {
    running = false;
}

process.on('SIGTERM', killProcess);
process.on('SIGINT', killProcess);
process.on('uncaughtException', function(e) {
    console.log('[uncaughtException] app will be terminated: ', e.stack);
    killProcess();
});

function run() {
    setTimeout(function() {
        if (running) run();
    }, 10);
}

run();
Modicum answered 23/3, 2021 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.