From e2d659d99275b10f21e7eb48dfcc62f2e141aa16 Mon Sep 17 00:00:00 2001 From: DoroWu Date: Sun, 22 Apr 2018 23:00:48 +0800 Subject: [PATCH] feat: add Docker HEALTHCHECK (issue #69) --- Dockerfile | 1 + image/usr/local/lib/web/backend/vnc/app.py | 8 ++++++++ image/usr/local/lib/web/backend/vnc/state.py | 3 --- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4c37358..c4ce792 100644 --- a/Dockerfile +++ b/Dockerfile @@ -90,4 +90,5 @@ EXPOSE 80 WORKDIR /root ENV HOME=/home/ubuntu \ SHELL=/bin/bash +HEALTHCHECK --interval=30s --timeout=5s CMD curl --fail http://127.0.0.1/api/health ENTRYPOINT ["/startup.sh"] diff --git a/image/usr/local/lib/web/backend/vnc/app.py b/image/usr/local/lib/web/backend/vnc/app.py index 09665a6..7397a74 100644 --- a/image/usr/local/lib/web/backend/vnc/app.py +++ b/image/usr/local/lib/web/backend/vnc/app.py @@ -8,6 +8,7 @@ from flask import ( request, Response, jsonify, + abort, ) from gevent import subprocess as gsp, spawn, sleep from geventwebsocket.exceptions import WebSocketError @@ -35,6 +36,13 @@ def apistate(): }) +@app.route('/api/health') +def apihealth(): + if state.health: + return 'success' + abort(503, 'unhealthy') + + @app.route('/api/reset') def reset(): if 'w' in request.args and 'h' in request.args: diff --git a/image/usr/local/lib/web/backend/vnc/state.py b/image/usr/local/lib/web/backend/vnc/state.py index 80515aa..c547811 100644 --- a/image/usr/local/lib/web/backend/vnc/state.py +++ b/image/usr/local/lib/web/backend/vnc/state.py @@ -27,9 +27,6 @@ class State(object): self._event.set() def _update_health(self): - if self._health: - return - health = True output = gsp.check_output(['supervisorctl', 'status']) for line in output.strip().split('\n'):