Skip to content

Commit 81a3463

Browse files
committed
update
1 parent c1a7a5b commit 81a3463

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

apps/monitor/src/plugins/processes.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { execSync } = require('child_process');
2+
const { log, ses } = require('../node-common')(['log', 'ses']);
3+
4+
let notified = false;
5+
6+
/**
7+
* Check running processes.
8+
*
9+
* @param {object} args - Plugin args.
10+
*/
11+
module.exports = async (args) => {
12+
const { PROCESSES = [] } = args;
13+
if (!PROCESSES.length) throw new Error('No processes to monitor');
14+
15+
const stoppedProcesses = [];
16+
17+
try {
18+
// Get processes
19+
PROCESSES.forEach((p) => {
20+
let running = false;
21+
try {
22+
running = execSync(`ps -e | grep ${p}`).toString().split('\n').length > 0;
23+
} catch (e) {
24+
/* Failed code */
25+
}
26+
27+
log.debug(`Process ${p} running: ${running}`);
28+
29+
if (!running) stoppedProcesses.push(p);
30+
});
31+
32+
// Send notification once
33+
if (stoppedProcesses.length && !notified) {
34+
await ses.notify(`Processes stopped: ${stoppedProcesses.join(', ')}`);
35+
notified = true;
36+
}
37+
38+
// Reset if recovers
39+
if (!stoppedProcesses.length && notified) {
40+
notified = false;
41+
}
42+
} catch (e) {
43+
log.error('Failed to check running processes');
44+
console.log(e);
45+
46+
await ses.notify(`Failed to check running processes: ${e.stack}`);
47+
}
48+
};

0 commit comments

Comments
 (0)