Implement graceful shutdown for the server and enhance process management on Windows

- Added a new `/shutdown` endpoint to allow graceful server shutdown via HTTP.
- Implemented process tree management functions to handle child processes during shutdown on Windows.
- Updated the `stop_server` function to attempt graceful shutdown before forcefully terminating processes.
- Enhanced error handling and logging for shutdown operations.
This commit is contained in:
Jamie Pine
2026-01-29 02:58:21 -08:00
parent e23118f610
commit 2a3afec2ca
5 changed files with 419 additions and 18 deletions
+15 -1
View File
@@ -18,6 +18,9 @@ import tempfile
import io
from pathlib import Path
import uuid
import asyncio
import signal
import os
from . import database, models, profiles, history, tts, transcribe, config, export_import, channels, stories, __version__
from .database import get_db, Generation as DBGeneration, VoiceProfile as DBVoiceProfile
@@ -50,13 +53,24 @@ async def root():
return {"message": "voicebox API", "version": __version__}
@app.post("/shutdown")
async def shutdown():
"""Gracefully shutdown the server."""
async def shutdown_async():
await asyncio.sleep(0.1) # Give response time to send
os.kill(os.getpid(), signal.SIGTERM)
asyncio.create_task(shutdown_async())
return {"message": "Shutting down..."}
@app.get("/health", response_model=models.HealthResponse)
async def health():
"""Health check endpoint."""
from huggingface_hub import hf_hub_download, constants as hf_constants
from pathlib import Path
import os
tts_model = tts.get_tts_model()
# Check for GPU availability (CUDA or MPS)