fix: avoid worker fetch noise when server is offline

This commit is contained in:
Luca Sacchi Ricciardi
2026-04-25 16:30:46 +02:00
parent 1c76515d8c
commit 6739b84b9a
3 changed files with 23 additions and 6 deletions
+2
View File
@@ -126,6 +126,8 @@ async def get_models(host: Optional[str] = Query(default=None)):
status_code=502,
detail="Impossible connettersi a Ollama"
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error fetching models: {e}")
raise HTTPException(
+7 -4
View File
@@ -49,7 +49,9 @@ async function fetchModels() {
try {
const response = await fetch(buildApiUrl(`${API_BASE}/models`));
if (!response.ok) throw new Error("Failed to load models");
if (!response.ok) {
return null;
}
const data = await response.json();
const models = data.models || [];
@@ -108,7 +110,7 @@ async function fetchRunningModels() {
try {
const response = await fetch(buildApiUrl(`${API_BASE}/models/running`));
if (!response.ok) {
throw new Error("Failed to load running models");
return null;
}
const data = await response.json();
@@ -137,8 +139,9 @@ async function syncData() {
}
const health = await fetchHealth();
const modelsData = await fetchModels();
const runningData = await fetchRunningModels();
const isOnline = health?.ollama_status === "online";
const modelsData = isOnline ? await fetchModels() : null;
const runningData = isOnline ? await fetchRunningModels() : null;
if (modelsData && modelsData.models.length > 0) {
modelsData.showByModel = await fetchAllModelsShow(modelsData.models);
+14 -2
View File
@@ -3,6 +3,7 @@ Test API endpoints
"""
import pytest
import requests
from unittest.mock import patch, MagicMock
def test_health_check(client):
@@ -107,10 +108,21 @@ def test_get_running_models_ollama_offline(client):
def test_get_models_ollama_offline(client):
"""Test getting models when Ollama is offline"""
with patch("requests.get") as mock_get:
mock_get.side_effect = Exception("Connection refused")
mock_get.side_effect = requests.exceptions.ConnectionError("Connection refused")
response = client.get("/api/v1/models")
assert response.status_code == 500
assert response.status_code == 502
def test_get_models_returns_502_when_upstream_is_unavailable(client):
"""Non-200 upstream response should remain a 502, not be converted to 500."""
with patch("requests.get") as mock_get:
mock_response = MagicMock()
mock_response.status_code = 503
mock_get.return_value = mock_response
response = client.get("/api/v1/models")
assert response.status_code == 502
def test_get_specific_model(client, mock_models_response):
"""Test getting specific model"""