/
liveness.py
28 lines (22 loc) · 935 Bytes
/
liveness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from tornado import web
from .document import _cleanup_doc
class LivenessHandler(web.RequestHandler):
def initialize(self, applications):
self.applications = applications
async def get(self):
endpoint = self.get_argument("endpoint", default=None)
self.set_header('Content-Type', 'application/json')
if endpoint is not None and endpoint not in self.applications:
raise web.HTTPError(400, f"Endpoint {endpoint!r} does not exist.")
elif endpoint is None:
self.write({self.request.path: True})
return
app = self.applications[endpoint]
try:
doc = app.create_document()
_cleanup_doc(doc)
self.write({endpoint: True})
except Exception as e:
raise web.HTTPError(
500, f"Endpoint {endpoint!r} could not be served. Application raised error: {e}"
) from e