Skip to content

Fix/translation startup - #22

Open
Ayan-josh-05 wants to merge 2 commits into
chore/dockerize-appfrom
fix/translation-startup
Open

Fix/translation startup#22
Ayan-josh-05 wants to merge 2 commits into
chore/dockerize-appfrom
fix/translation-startup

Conversation

@Ayan-josh-05

Copy link
Copy Markdown

Fix Translation Health Checks and Gateway Request Timeouts

Summary

Fixed two startup and reliability issues found during end-to-end testing of the containerized stack:

  1. The translation service's /health endpoint could block for several minutes while Ollama loaded the model.
  2. The gateway used a flat 120-second timeout for all backend requests, causing legitimate long-running OCR, translation, and field-mapping requests to fail with a 503.

The fixes make translation health checks non-blocking and introduce realistic, per-service gateway timeouts.

What's Fixed

1. Translation /health no longer blocks on Ollama cold loads

Previously, /health called Ollama synchronously. During a cold model load, this could block for 2+ minutes, causing orchestrators or load balancers polling the endpoint to time out instead of receiving a quick "not ready" response.

Now, a background monitor task checks Ollama independently, while /health simply returns the latest observed state immediately:

  • ok — model is loaded and responding
  • initializing — Ollama is reachable, but the model is still loading
  • unreachable — Ollama is not reachable

The monitor:

  • Retries every OLLAMA_HEALTH_RETRY_SECONDS (default: 5s)
  • Performs up to OLLAMA_HEALTH_MAX_FAST_RETRIES (default: 12) consecutive fast retries
  • Backs off to OLLAMA_HEALTH_BACKOFF_SECONDS (default: 120s) after repeated failures
  • Continues retrying indefinitely
  • Rechecks a healthy Ollama instance every OLLAMA_HEALTH_RECHECK_SECONDS (default: 30s)

The first background ping also warms up the model automatically during startup.

Commit: 226bdd7

2. Gateway no longer uses a flat 120s timeout

Previously, the gateway used a shared 120-second httpx timeout for all backend requests. This was too short for operations such as:

  • Cold Ollama model loads
  • Multi-page OCR
  • Long-running translation
  • Field mapping

The frontend already allows up to 5 minutes for these requests, but the gateway could terminate them first and return a spurious 503 while the backend was still processing.

Added separate, environment-configurable timeouts for each service:

  • OCR_REQUEST_TIMEOUT_SECONDS
  • TRANSLATION_REQUEST_TIMEOUT_SECONDS
  • FIELD_MAPPING_REQUEST_TIMEOUT_SECONDS

All default to 300 seconds (5 minutes) and are passed through _proxy().

Health-check proxy requests retain a separate 5-second timeout, since health checks are now expected to respond immediately.

Commit: b0b1860

How to Test

1. Start the stack

cd lending-poc
docker compose up --build

2. Test translation health

Hit the translation service's /health immediately after startup.

Expected behavior:

  • Responds immediately with status: "initializing"
  • Eventually changes to status: "ok" once the background monitor successfully pings Ollama

The endpoint should no longer hang during the initial model load.

3. Test long-running gateway requests

After a cold start, trigger an OCR, translation, or field-mapping request through the gateway before Ollama has warmed up.

Expected behavior:

  • The request is allowed to run for up to 300 seconds
  • The gateway does not prematurely return a 503 at the old 120-second limit
  • The request completes successfully if the backend finishes within the configured timeout

4. Optional configuration testing

The following variables can be overridden through .env to verify the tuning behavior:

OLLAMA_HEALTH_RETRY_SECONDS
OLLAMA_HEALTH_MAX_FAST_RETRIES
OLLAMA_HEALTH_BACKOFF_SECONDS
OLLAMA_HEALTH_RECHECK_SECONDS

OCR_REQUEST_TIMEOUT_SECONDS
TRANSLATION_REQUEST_TIMEOUT_SECONDS
FIELD_MAPPING_REQUEST_TIMEOUT_SECONDS

Result

Translation health checks are now fast and non-blocking, while the gateway can accommodate legitimately long-running backend operations without prematurely returning 503 errors.

Ayan-josh-05 and others added 2 commits August 27, 2026 15:57
…zing/unreachable status

Previously /health called Ollama synchronously and blocked for the full
2+ minute cold model load. A background monitor now pings Ollama with an
untimed chat() call on its own task, and /health just reads the last
observed state instantly. Retries fast on failure, backs off after 12
consecutive failures, and keeps reconfirming "ok" so a later Ollama
outage is caught too. As a side effect, the monitor's first ping also
warms up the model automatically on startup.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…120s

The gateway proxied every backend call through one shared 120s httpx
timeout, but OCR/translation/field-mapping can legitimately run for
minutes (cold Ollama model loads, multi-page OCR) — the frontend already
budgets 5 minutes for these same calls. The gateway was giving up first,
returning a spurious 503 while the backend was still working. Added
OCR_REQUEST_TIMEOUT_SECONDS / TRANSLATION_REQUEST_TIMEOUT_SECONDS /
FIELD_MAPPING_REQUEST_TIMEOUT_SECONDS (default 300s each, env-overridable)
and threaded them through _proxy(); health-check proxying keeps a short
5s timeout separately.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant