Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions c2corg_api/tests/views/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ class Client:
r = client.get('/health', status=200)
assert r.json['es'] == 'ok'
assert r.json['es_indexed_docs'] == 42


def test_es_down(monkeypatch, client):
# the documents are still served without ES: the health check passes (the
# load balancer keeps the server) and a search answers 503
from elasticsearch import Elasticsearch

from c2corg_api.search import elasticsearch_config

unreachable = Elasticsearch(['localhost:1'], max_retries=0, timeout=1)
monkeypatch.setitem(elasticsearch_config, 'client', unreachable)

r = client.get('/health', status=200)
assert r.json['es'] == 'error'
assert r.json['es_indexed_docs'] is None

r = client.get('/outings?act=skitouring', status=503)
assert r.json['errors'][0]['name'] == 'search unavailable'
7 changes: 7 additions & 0 deletions c2corg_api/web/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

from elasticsearch.exceptions import ConnectionError as ElasticsearchConnectionError
from fastapi.responses import JSONResponse
from starlette.middleware.base import BaseHTTPMiddleware

Expand Down Expand Up @@ -37,6 +38,12 @@ class UnexpectedErrorMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
try:
return await call_next(request)
except ElasticsearchConnectionError as e:
# ES unreachable: only the search is unavailable, say so
log.error(f'Search unavailable: {e}')
return error_response(
[('body', 'search unavailable', 'please retry later')], 503
)
except Exception as e:
log.exception(f'Unexpected error: {e}')
return error_response(
Expand Down
7 changes: 4 additions & 3 deletions c2corg_api/web/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def health(request: Request, response: Response):
with request_session() as session:
if not _add_database_status(status, session):
response.status_code = 500
if not _add_es_status(status):
response.status_code = 500
_add_es_status(status)
_add_redis_status(status)
if _add_maintenance_mode_status(status):
response.status_code = 404
Expand All @@ -70,6 +69,9 @@ def _add_database_status(status, session):


def _add_es_status(status):
# an ES failure does not fail the health check: the documents are read from
# PostgreSQL, only the search is degraded, and every API shares the same ES
# cluster, so the load balancer would take them all out of its pool
es_docs = None
success = False
try:
Expand All @@ -84,7 +86,6 @@ def _add_es_status(status):
log.exception('Getting indexed docs count failed')
status['es'] = 'ok' if success else 'error'
status['es_indexed_docs'] = es_docs
return success


def _add_redis_status(status):
Expand Down