Skip to content

Commit 7dfbb90

Browse files
author
rok
committed
added watchdog
1 parent f65c5e9 commit 7dfbb90

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
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.py /opt/supervisor-watchdog.py
2324

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

init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ if [ "$LARAVEL_HORIZON" = true ]; then
2727
cp /etc/supervisor/conf.d/laravel-horizon.conf.tpl /etc/supervisor/supervisord.conf
2828
fi
2929

30-
supervisord --nodaemon --configuration /etc/supervisor/supervisord.conf
30+
exec supervisord --nodaemon --configuration /etc/supervisor/supervisord.conf

laravel-horizon.conf.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ autostart=true
1010
autorestart=true
1111
stdout_events_enabled=1
1212
redirect_stderr=true
13+
14+
[eventlistener:supervisord-watchdog]
15+
command=/usr/bin/python /opt/supervisord-watchdog.py
16+
events=PROCESS_STATE_FATAL

laravel-worker.conf.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ numprocs=1
1212
startretries=10
1313
stdout_events_enabled=1
1414
redirect_stderr=true
15+
16+
[eventlistener:supervisord-watchdog]
17+
command=/usr/bin/python /opt/supervisord-watchdog.py
18+
events=PROCESS_STATE_FATAL

supervisor-watchdog.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)