-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
179 lines (145 loc) · 4.94 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
// Dependencies
const pmx = require('pmx');
const pm2 = require('pm2');
const ProcessWatchdog = require('./class-process-watchdog');
// Get PM2 module options
const moduleConfig = pmx.initModule((err, conf) => {
if (err) {
console.error('PMX initialization failed. ', err.message || err);
return;
}
});
// Extending the console with a custom method
console.debug = function(msg) { console.org.log(msg); };
console.trace = function(msg) { console.org.log(msg); };
// Initialising the console output formatter
require('console-stamp')(console, {
extend: {
debug: 5,
trace: 6,
},
include: ['trace', 'debug', 'log', 'info', 'warm', 'error'],
level: moduleConfig.debug,
});
console.trace('Module is starting.');
// Connect to local PM2
pm2.connect((err) => {
if (err) {
console.error('PM2 connect failed. ', err.message || err);
return;
}
console.trace('Module connected to PM2.');
// start the watchdog
addWatchdogOfExistingProcesses();
});
/**
* Creates and starts watchdogs for all existing PM2 processes.
*/
function addWatchdogOfExistingProcesses() {
console.trace('Getting list of processes.');
// Gets list of process managed by pm2
pm2.list(
/**
*
* @param {Error|null} err
* @param {ProcessDescription[]} apps
*/
function(err, apps) {
if (err) {
console.error(`PM2 get process list failed. ${err.message || err}`);
pm2.disconnect();
return;
}
apps.forEach((/*ProcessDescription*/ processDescription) => {
if (processDescription.pm2_env.axm_options.isModule) {
console.trace(`Process ${processDescription.name} - is PM2 module. Ignoring it`);
return;
// TODO not neccesary
} // Ignore PM2 modules
if (processDescription.pm2_env.status === 'online') {
startOrRestartWatchdog(processDescription);
} else {
console.trace(`Process ${processDescription.name} - is not online, no watdog yet`);
}
});
});
}
// ----- APP INITIALIZATION -----
// Start listening on the PM2 BUS
pm2.launchBus(function(err, bus) {
// Listen for PM2 events
bus.on('process:event',
/**
*
* @param {Object} data
* @param {string} data.event - 'start'|'stop'|'restart'|'restart overlimit'|'delete'|'exit'|'online'.
* See {@link https://github.com/rrrene/PM2/blob/master/doc/EVENTS.md}
* @param {Pm2Env} data.process
*/
function(data) {
if (data.process.axm_options.isModule) { return; } // Ignore PM2 modules
console.debug(`Process ${data.process.name} - event ${data.event}`);
switch (data.event) {
case 'start':
case 'online':
// Start/update watchdog
if (data.process.status === 'online') {
startOrRestartWatchdog(data.process);
} else {
console.debug(`WARNING: Unexpexpected status of process when starting: ${data.process.status}`);
}
break;
case 'stop':
case 'exit':
// Kill the existing watchdog
removeWatchdog(data.process);
break;
// case 'restart overlimit':
// case 'delete':
// case 'restart':
}
},
);
});
/**
* Key is `pm_id` of each PM2 process
* @type {Map<number,ProcessWatchdog>}
*/
const watchdogs = new Map();
/**
* @param {Pm2Env} pm2Env
*/
function startOrRestartWatchdog(pm2Env) {
const watchedUrl = moduleConfig[`url-${pm2Env.name}`];
if (!watchedUrl) {
console.trace(`Process ${pm2Env.name} - no watchdog configuration`);
return;
}
removeWatchdog(pm2Env);
let watchdog = watchdogs.get(pm2Env.pm_id);
// Create the new watchdog of new process
watchdog = new ProcessWatchdog(pm2Env, {
watchedUrl: watchedUrl,
watchedUrlAuth: moduleConfig[`urlauth-${pm2Env.name}`],
checkingInterval: parseInt(moduleConfig.checking_interval, 10) || 10,
failsToRestart: moduleConfig.fails_to_restart,
checkingTimeout: parseInt(moduleConfig.checking_timeout) || 5000,
});
// Store new watchdog to watchdogs list
watchdogs.set(pm2Env.pm_id, watchdog);
// Start watchdog
watchdog.start();
}
/**
* @param {Pm2Env} pm2Env
*/
function removeWatchdog(pm2Env) {
const watchdog = watchdogs.get(pm2Env.pm_id);
if (watchdog) {
watchdogs.delete(pm2Env.pm_id);
watchdog.stop();
} else {
console.trace(`Process ${pm2Env.name} - no watchdog exists for removal`);
}
}