forked from MichMich/MMM-WatchDog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
44 lines (35 loc) · 1.31 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
// Create the timer object.
timer: null,
// Set the default config. Since the timer should not start
// before the config is set by the frontend, the default can
// be 1 seconds. The real default is set in MMM-WatchDog.js
config: {
timeout: 1
},
// Handle incomming messages.
socketNotificationReceived: function(notification, payload) {
// Incoming config. Store config and schedule restart.
if (notification === 'SET_CONFIG') {
this.config = payload;
this.scheduleRestart();
console.log("WatchDog started. Maximum timeout: " + this.config.timeout + "s.");
}
// Incoming PING. Reschedule restart.
if (notification === 'PING') {
this.scheduleRestart();
}
},
// Reschedule restart by clearing old timer, and setting a new timer.
scheduleRestart: function() {
clearTimeout(this.timer);
this.timer = setTimeout(this.restart, this.config.timeout * 1000);
},
// Quit Node process.
restart: function() {
var now = new Date();
console.error(now.toString() + ' - WatchDog: Heartbeat timeout. Frontend might have crashed. Exit now.');
process.exit(1);
}
});