generated from john/python-template
fix: derive worker shutdown wait from timeout budget
Co-authored-by: Copilot App <[email protected]>
This commit is contained in:
co-authored by
Copilot App
parent
86cdb4035c
commit
f9261a1af3
@@ -60,6 +60,9 @@ async def _lifespan(app: FastAPI):
|
||||
worker_consumer_lifespan(
|
||||
session_factory=app.state.runtime.session_factory,
|
||||
poll_interval_seconds=1.0,
|
||||
shutdown_timeout_seconds=(
|
||||
settings.worker_provider_timeout_seconds + settings.worker_shutdown_grace_seconds
|
||||
),
|
||||
)
|
||||
)
|
||||
app.state.worker_stop_event = stop_event
|
||||
|
||||
@@ -116,6 +116,7 @@ class Settings(BaseSettings):
|
||||
worker_provider_timeout_seconds: float = Field(default=30.0, gt=0.0)
|
||||
worker_stale_job_seconds: float = Field(default=30.0, gt=0.0)
|
||||
worker_retry_backoff_seconds: float = Field(default=1.0, ge=0.0)
|
||||
worker_shutdown_grace_seconds: float = Field(default=5.0, ge=0.0)
|
||||
worker_min_transcription_chars: int = Field(default=0, ge=0)
|
||||
worker_min_transcription_lines: int = Field(default=0, ge=0)
|
||||
worker_fail_on_finish_reason_length: bool = False
|
||||
|
||||
@@ -123,6 +123,7 @@ async def worker_consumer_lifespan(
|
||||
*,
|
||||
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
||||
poll_interval_seconds: float = 1.0,
|
||||
shutdown_timeout_seconds: float = 2.0,
|
||||
) -> AsyncGenerator[tuple[asyncio.Event, WorkerNotifier, WorkerHealth]]:
|
||||
"""Start and stop the worker consumer loop for app lifespan."""
|
||||
stop_event = asyncio.Event()
|
||||
@@ -146,7 +147,7 @@ async def worker_consumer_lifespan(
|
||||
stop_event.set()
|
||||
worker_notifier.notify()
|
||||
try:
|
||||
await asyncio.wait_for(worker_task, timeout=2.0)
|
||||
await asyncio.wait_for(worker_task, timeout=shutdown_timeout_seconds)
|
||||
except TimeoutError:
|
||||
worker_task.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
|
||||
Reference in New Issue
Block a user