Refactor model download handling to use background tasks

- Moved model download logic into a separate asynchronous function to allow non-blocking HTTP responses.
- Improved error handling by tracking download status and reporting errors without interrupting the main request flow.
- The frontend is now expected to poll the progress endpoint for download status updates.
This commit is contained in:
Jamie Pine
2026-01-29 02:42:02 -08:00
parent e23118f610
commit 8d730621bc
+16 -16
View File
@@ -1209,22 +1209,22 @@ async def trigger_model_download(request: models.ModelDownloadRequest):
config = model_configs[request.model_name] config = model_configs[request.model_name]
try: async def download_in_background():
# Start tracking download """Download model in background without blocking the HTTP request."""
task_manager.start_download(request.model_name) try:
await asyncio.to_thread(config["load_func"])
# Trigger download by loading the model (which will download if not cached) task_manager.complete_download(request.model_name)
# Run in background to avoid blocking except Exception as e:
await asyncio.to_thread(config["load_func"]) task_manager.error_download(request.model_name, str(e))
# Mark download as complete # Start tracking download
task_manager.complete_download(request.model_name) task_manager.start_download(request.model_name)
return {"message": f"Model {request.model_name} download started"} # Start download in background task (don't await)
except Exception as e: asyncio.create_task(download_in_background())
# Mark download as failed
task_manager.error_download(request.model_name, str(e)) # Return immediately - frontend should poll progress endpoint
raise HTTPException(status_code=500, detail=str(e)) return {"message": f"Model {request.model_name} download started"}
@app.delete("/models/{model_name}") @app.delete("/models/{model_name}")