Legoktm has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/404996 )

Change subject: Add basic health check end point
......................................................................

Add basic health check end point

Change-Id: I3103cf7e996097296326c2c1f399dd4d6a225b66
---
M app.py
A templates/health.html
2 files changed, 55 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/codesearch 
refs/changes/96/404996/1

diff --git a/app.py b/app.py
index d64dddc..72fcae6 100644
--- a/app.py
+++ b/app.py
@@ -23,6 +23,7 @@
 import os
 import re
 import requests
+import subprocess
 import traceback
 
 app = Flask(__name__)
@@ -86,6 +87,46 @@
     return redirect(url_for('index', backend='search'))
 
 
+def parse_systemctl_show(output):
+    """
+    turn the output of `systemctl show` into
+    a dictionary
+    """
+    data = {}
+    for line in output.splitlines():
+        sp = line.split('=', 1)
+        data[sp[0]] = sp[1]
+
+    return data
+
+
+@app.route('/_health')
+def health():
+    status = {}
+    for backend, port in BACKENDS.items():
+        # First try to hit the hound backend, if it's up, we're good
+        try:
+            requests.get('http://localhost:%s/api/v1/search' % port)
+            status[backend] = 'up'
+        except requests.exceptions.ConnectionError:
+            # See whether the systemd unit is running
+            try:
+                show = subprocess.check_output(
+                    ['systemctl', 'show', 'hound-%s' % backend]
+                )
+                info = parse_systemctl_show(show.decode())
+                if info['MainPID'] == '0':
+                    status[backend] = 'down'
+                else:
+                    # No webservice, but hound is running, so it's probably
+                    # just starting up still
+                    status['backend'] = 'starting up'
+            except subprocess.CalledProcessError:
+                status[backend] = 'unknown'
+
+    return render_template('health.html', status=status)
+
+
 @app.route('/<backend>/')
 def index(backend):
     if backend not in BACKENDS:
diff --git a/templates/health.html b/templates/health.html
new file mode 100644
index 0000000..007c285
--- /dev/null
+++ b/templates/health.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>MediaWiki code search: health check</title>
+</head>
+<body>
+<h1>health check</h1>
+
+{% for backend in status %}
+<br /><a href="{{url_for('index', backend=backend)}}">{{backend}}</a> - 
{{status[backend]}}
+{% endfor %}
+</body>
+</html>

-- 
To view, visit https://gerrit.wikimedia.org/r/404996
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3103cf7e996097296326c2c1f399dd4d6a225b66
Gerrit-PatchSet: 1
Gerrit-Project: labs/codesearch
Gerrit-Branch: master
Gerrit-Owner: Legoktm <lego...@member.fsf.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to