amingilani amingilani - 20 days ago 9
Node.js Question

Why does my heartbeat process die as soon as it starts?

I have a simple heartbeat process that should run forever and continue beating, but it dies as soon I start it.

$ node heartbeat.js
Starting heartbeats
$


My heartbeat script is quite straightforward.

var heartbeats = require('heartbeats');

// tell me that the script is starting
console.log('Starting heartbeats');

// create a heart that beats every second
var heart = heartbeats.createHeart(1000);

// every heartbeat, tell me the beat number
heart.createEvent(1, function(heartbeat, last){
console.log('beat number ' + heart.age);
});


However adding
process.stdin.resume();
at the end keeps it running forever:

$ node heartbeat.js
Starting heartbeats
beat number 1
beat number 2
beat number 3
...


Why does it do this?

Answer

I'm the author of the module, and @SergeyLapin is correct. Heartbeats is not currently meant to keep your app alive. I figured, this would be the expected behavior for most people.

However you can keep your app alive by adding at the end of your code:

heart.interval.ref();

I will modify the API and add a new feature to ensure that if there are events, that the app is kept alive at least as long as there are events.