Skip to content

Commit

Permalink
add status endpoint to wsgi app
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry committed Aug 3, 2021
1 parent 8b6e0c8 commit 8dd6010
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
5 changes: 5 additions & 0 deletions biothings/web/handlers/_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ async def fields(biothings, args):
await biothings.metadata.refresh(None)
mappings = biothings.metadata.get_mappings(None)
return biothings.pipeline.formatter.transform_mapping(mappings)

@route("/status")
@handle_es_conn
async def status(biothings, args):
return await biothings.health.async_check()
16 changes: 2 additions & 14 deletions biothings/web/handlers/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,10 @@ async def get(self):
async def _check(self, dev=False):

try: # some db connections support async operations
response = await self.biothings.health.async_check(info=dev)
response = await self.biothings.health.async_check(dev)
except (AttributeError, NotImplementedError):
response = self.biothings.health.check()

if not dev:
return {
# this endpoint might be accessed frequently,
# keep the default response minimal. This is
# especially useful when the document payload
# is very large. Also useful when the automated
# healch check only support GET requests.
"success": True,
"status": response.get("status")
}

return dict(response)
return response

class FrontPageHandler(BaseHandler):

Expand Down
40 changes: 29 additions & 11 deletions biothings/web/services/health.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
from elasticsearch import Elasticsearch, AsyncElasticsearch
from collections import UserDict

class HCResult(UserDict):
pass # Health Check Result
class _HCResult():

MIN_RES_FLD = 'status'

def __init__(self, verbose=False):
self._verbose = verbose
self._content = dict(success=True)

def update(self, dic):
if self._verbose:
self._content.update(dic)
elif self.MIN_RES_FLD in dic:
self._content[self.MIN_RES_FLD] = dic[self.MIN_RES_FLD]

def to_dict(self):
return dict(self._content)

class DBHealth():

Expand Down Expand Up @@ -57,34 +71,38 @@ def __init__(self, client, payload=None):
# "id": "1017"
# }

async def async_check(self, **kwargs):
async def async_check(self, verbose=False):
assert isinstance(self.client, AsyncElasticsearch)
response = HCResult()

# this endpoint might be accessed frequently,
# keep the default response minimal by default.

response = _HCResult(verbose)
response.update(await self.client.cluster.health())

if kwargs.get('info'):
if verbose: # request additional information
response.update(await self.client.info())

if self.payload:
document = await self.client.get(**self.payload)
response['payload'] = self.payload
response['document'] = document
response.update({'payload': self.payload})
response.update({'document': document})

return response
return response.to_dict()

def check(self):
assert isinstance(self.client, Elasticsearch)
return HCResult(self.client.cluster.health())
return self.client.cluster.health()

class MongoHealth(DBHealth):

def check(self, **kwargs):
# typical response: {'ok': 1.0}
return HCResult(self.client.command('ping'))
return self.client.command('ping')

class SQLHealth(DBHealth):

def check(self, **kwargs):
# https://docs.sqlalchemy.org/en/13/core/connections.html
# #sqlalchemy.engine.Connection.closed
return HCResult(closed=self.client.closed)
return dict(closed=self.client.closed)

0 comments on commit 8dd6010

Please sign in to comment.