Skip to content

Commit 4d22f61

Browse files
author
rok
committed
adding a watchdog to gete to know when supervisord worker dies
1 parent a644412 commit 4d22f61

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ WORKDIR /etc/supervisor/conf.d
2020
COPY laravel-worker.conf.tpl /etc/supervisor/conf.d/laravel-worker.conf.tpl
2121
COPY laravel-horizon.conf.tpl /etc/supervisor/conf.d/laravel-horizon.conf.tpl
2222
COPY custom-php.ini.tpl /opt/etc/custom-php.ini.tpl
23+
COPY supervisor-watchdog /usr/local/bin/supervisor-watchdog
2324

2425
# Copy scripts
2526
COPY init.sh /usr/local/bin/init.sh

laravel-horizon.conf.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ command=php /var/www/app/artisan horizon
77
autostart=true
88
autorestart=true
99
stdout_events_enabled=1
10+
11+
[eventlistener:supervisord-watchdog]
12+
command=/usr/local/bin/supervisord-watchdog
13+
events=PROCESS_STATE_FATAL

laravel-worker.conf.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ autorestart=true
99
numprocs=1
1010
startretries=10
1111
stdout_events_enabled=1
12+
13+
[eventlistener:supervisord-watchdog]
14+
command=/usr/local/bin/supervisord-watchdog
15+
events=PROCESS_STATE_FATAL

supervisor-watchdog

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

0 commit comments

Comments
 (0)