Skip to content

fix(health): keep the API in the pool when Elasticsearch is down (#2667) - #2699

Merged
lbesson merged 1 commit into
masterfrom
fix/health-es-resilience
Nov 24, 2026
Merged

lbesson merged 1 commit into
masterfrom
fix/health-es-resilience

Conversation

@eddy-geek

Copy link
Copy Markdown
Contributor

Closes #2667

What I reproduced (ES stopped on the local dev stack)

Request Before After
GET /health 500 200, with "es":"error" and "es_indexed_docs":null
GET /outings/{id}?cook=fr (what the outing page calls) 200 200
GET /outings?act=…, /search?q=… 500 "unexpected error" 503 "search unavailable"

Opening a document never needed ES: those reads come from PostgreSQL. The real problem was /health. HAProxy probes it, and it answered 500 whenever ES failed. Every API container uses the same ES cluster, so they all fail the probe together and HAProxy removes the whole pool. After that, every page breaks, including /outings/1964309. The KeyError in the issue was one such ES failure (already fixed by #2666), but any ES outage had the same effect.

Changes

  1. web/health.py: an ES failure no longer turns /health into a 500. This matches how Redis was already handled. ES is still reported as "es": "error" in the body and the exception is still logged. Only PostgreSQL (500) and maintenance mode (404) still take a server out of the pool.
  2. web/errors.py: UnexpectedErrorMiddleware now catches elasticsearch.exceptions.ConnectionError (this also covers connection timeouts). It answers 503 search unavailable / please retry later and logs one error line instead of a full traceback. Other errors still answer 500.
  3. tests/views/test_health.py: new test_es_down points the client at an unreachable ES. It checks that /health answers 200 with es == 'error', and that a filtered /outings listing answers 503.

The 5 health tests pass, and ruff check and format are clean. I didn't run the full test suite.

Notes

  • Monitoring: /health no longer turns red when ES is down. Alerting should read es in the JSON body instead of the HTTP status.
  • Scope of the 503: only ES being unreachable or timing out. Other ES errors (missing index, malformed query) still give a 500 on purpose, since they're bugs rather than outages.

## Changes

1. health.py: an ES failure no longer turns /health into a 500. This
   matches how Redis was already handled. ES is still reported as
   "es": "error" in the body and the exception is still logged. Only
   PostgreSQL (500) and maintenance mode (404) still take a server out of
   the pool.
2. errors.py: UnexpectedErrorMiddleware now catches
   elasticsearch.exceptions.ConnectionError (this also covers connection
   timeouts). It answers 503 "search unavailable / please retry later" and
   logs one error line instead of a full traceback. Other errors still
   answer 500.
3. test_health.py: new test_es_down points the client at an unreachable
   ES. It checks that /health answers 200 with es == 'error', and that a
   filtered /outings listing answers 503.

Closes #2667
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@lbesson
lbesson merged commit a9a6695 into master Nov 24, 2026
7 checks passed
@lbesson
lbesson deleted the fix/health-es-resilience branch November 24, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: api backend is not resilient to ES errors

2 participants