File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments