diff options
-rw-r--r-- | fatcat_scholar/search.py | 14 | ||||
-rw-r--r-- | fatcat_scholar/web.py | 10 |
2 files changed, 20 insertions, 4 deletions
diff --git a/fatcat_scholar/search.py b/fatcat_scholar/search.py index be362b7..026c030 100644 --- a/fatcat_scholar/search.py +++ b/fatcat_scholar/search.py @@ -415,3 +415,17 @@ def do_fulltext_search( query_wall_time_ms=int(query_delta.total_seconds() * 1000), results=results, ) + +def es_scholar_index_exists() -> bool: + """ + Checks if the configured back-end elasticsearch index exists. + Intended to be used in health checks. + """ + try: + resp = es_client.indices.exists(settings.ELASTICSEARCH_QUERY_FULLTEXT_INDEX) + except elasticsearch.exceptions.RequestError as e_raw: + if e_raw.status_code == 404: + return False + else: + raise e_raw + return resp diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py index 4887aa6..9664474 100644 --- a/fatcat_scholar/web.py +++ b/fatcat_scholar/web.py @@ -21,7 +21,7 @@ from starlette.exceptions import HTTPException as StarletteHTTPException from fatcat_scholar.config import settings, GIT_REVISION from fatcat_scholar.hacks import Jinja2Templates, parse_accept_lang -from fatcat_scholar.search import process_query, FulltextQuery, FulltextHits +from fatcat_scholar.search import process_query, FulltextQuery, FulltextHits, es_scholar_index_exists from fatcat_scholar.schema import ScholarDoc @@ -107,15 +107,17 @@ async def root_head() -> Any: return Response() @api.get("/_health", operation_id="get_health") -async def health() -> Any: +def health_get() -> Any: """ Checks that connection back to elasticsearch index is working. """ + if not es_scholar_index_exists(): + raise HTTPException(status_code=503) return Response() @api.head("/_health", include_in_schema=False) -async def health_head() -> Any: - return Response() +def health_head() -> Any: + return health_get() class HitsModel(BaseModel): count_returned: int |