Auto-commit: Fix spinning logs — API timeouts + reliable Huey consumer start

Root causes of indefinite spinning during book create/generate:

1. ai/models.py — ResilientModel.generate_content() had no timeout: a
   stalled Gemini API call would block the thread forever. Now injects
   request_options={"timeout": 180} into every call. Also removed the
   dangerous init_models(force=True) call inside the retry handler, which
   was making a second network call during an existing API failure.

2. ai/setup.py — genai.list_models() calls in get_optimal_model(),
   select_best_models(), and init_models() had no timeout. Added
   request_options={"timeout": 30} to all three calls so model init
   fails fast rather than hanging indefinitely.

3. web/app.py — Huey task consumer only started inside
   `if __name__ == "__main__":`, meaning tasks queued via flask run,
   gunicorn, or other WSGI runners were never executed (status stuck at
   "queued" forever). Moved consumer start to module level with a
   WERKZEUG_RUN_MAIN guard to prevent double-start under the reloader.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-21 02:16:39 -05:00
parent 85f1290f02
commit 1f799227d9
3 changed files with 36 additions and 18 deletions

View File

@@ -7,9 +7,12 @@ from core import config, utils
from ai import models
_LIST_MODELS_TIMEOUT = {"timeout": 30}
def get_optimal_model(base_type="pro"):
try:
available = [m for m in genai.list_models() if 'generateContent' in m.supported_generation_methods]
available = [m for m in genai.list_models(request_options=_LIST_MODELS_TIMEOUT) if 'generateContent' in m.supported_generation_methods]
candidates = [m.name for m in available if base_type in m.name]
if not candidates: return f"models/gemini-1.5-{base_type}"
@@ -56,7 +59,7 @@ def select_best_models(force_refresh=False):
try:
utils.log("SYSTEM", "Refreshing AI model list from API...")
all_models = list(genai.list_models())
all_models = list(genai.list_models(request_options=_LIST_MODELS_TIMEOUT))
raw_model_names = [m.name for m in all_models]
utils.log("SYSTEM", f"Found {len(all_models)} raw models from Google API.")
@@ -155,7 +158,7 @@ def init_models(force=False):
if not skip_validation:
utils.log("SYSTEM", "Validating credentials...")
try:
list(genai.list_models(page_size=1))
list(genai.list_models(page_size=1, request_options=_LIST_MODELS_TIMEOUT))
utils.log("SYSTEM", "✅ Gemini API Key is valid.")
except Exception as e:
if os.path.exists(cache_path):