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:
24
web/app.py
24
web/app.py
@@ -90,16 +90,28 @@ with app.app_context():
|
||||
print(f"⚠️ System: Failed to clean up stuck runs: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import threading
|
||||
# --- HUEY CONSUMER ---
|
||||
# Start the Huey task consumer in a background thread whenever the app loads.
|
||||
# Guard against the Werkzeug reloader spawning a second consumer in the child process,
|
||||
# and against test runners or importers that should not start background workers.
|
||||
import threading as _threading
|
||||
|
||||
# Start Huey consumer in background thread
|
||||
def run_huey():
|
||||
def _start_huey_consumer():
|
||||
try:
|
||||
from huey.consumer import Consumer
|
||||
consumer = Consumer(huey, workers=1, worker_type='thread', loglevel=20)
|
||||
print("✅ System: Huey task consumer started.")
|
||||
consumer.run()
|
||||
except Exception as e:
|
||||
print(f"⚠️ System: Huey consumer failed to start: {e}")
|
||||
|
||||
t = threading.Thread(target=run_huey, daemon=True)
|
||||
t.start()
|
||||
_is_reloader_child = os.environ.get('WERKZEUG_RUN_MAIN') == 'true'
|
||||
_is_testing = os.environ.get('FLASK_TESTING') == '1'
|
||||
|
||||
if not _is_reloader_child and not _is_testing:
|
||||
_huey_thread = _threading.Thread(target=_start_huey_consumer, daemon=True, name="huey-consumer")
|
||||
_huey_thread.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host='0.0.0.0', port=5000, debug=False)
|
||||
|
||||
Reference in New Issue
Block a user