1
+ import sys
2
+ import os
3
+ import logging
4
+ import subprocess
5
+ import time
6
+
7
+ from supervisor .childutils import listener
8
+
9
+ def main (args ):
10
+ logging .basicConfig (stream = sys .stderr , level = logging .DEBUG , format = '%(asctime)s %(levelname)s %(filename)s: %(message)s' )
11
+ logger = logging .getLogger ("supervisord-watchdog" )
12
+ debug_mode = True if 'DEBUG' in os .environ else False
13
+
14
+ while True :
15
+ logger .info ("Listening for events..." )
16
+ headers , body = listener .wait (sys .stdin , sys .stdout )
17
+ body = dict ([pair .split (":" ) for pair in body .split (" " )])
18
+
19
+ logger .debug ("Headers: %r" , repr (headers ))
20
+ logger .debug ("Body: %r" , repr (body ))
21
+ logger .debug ("Args: %r" , repr (args ))
22
+
23
+ if debug_mode : continue
24
+
25
+ try :
26
+ if headers ["eventname" ] == "PROCESS_STATE_FATAL" :
27
+ logger .info ("Process entered FATAL state..." )
28
+ if not args or body ["processname" ] in args :
29
+ logger .error ("Killing off supervisord instance ..." )
30
+ res = subprocess .call (["/usr/bin/pkill" , "-15" , "supervisord" ], stdout = sys .stderr )
31
+ logger .info ("Sent TERM signal to init process" )
32
+ time .sleep ( 5 )
33
+ logger .critical ("Why am I still alive? Send KILL to all processes..." )
34
+ res = subprocess .call (["/usr/bin/pkill" , "-9" , "supervisord" ], stdout = sys .stderr )
35
+ except Exception as e :
36
+ logger .critical ("Unexpected Exception: %s" , str (e ))
37
+ listener .fail (sys .stdout )
38
+ exit (1 )
39
+ else :
40
+ listener .ok (sys .stdout )
41
+
42
+ if __name__ == '__main__' :
43
+ main (sys .argv [1 :])
0 commit comments