Commit graph

8 commits

Author SHA1 Message Date
yucheng
4e388e6aea refactor(proxy): inject scheduled job shutdown timeouts
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-21 19:37:28 +00:00
yucheng
9c411dd6f2 refactor(proxy): make scheduled job shutdown timeouts configurable via env
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-21 19:35:29 +00:00
yucheng
0b2d52edc2 Revert "style(proxy): format cleanup shutdown tests"
This reverts commit 39a14f39e5.
2026-09-20 08:21:35 +00:00
yucheng
39a14f39e5 style(proxy): format cleanup shutdown tests
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-20 08:20:47 +00:00
Yucheng He
39a199d9a2 fix(proxy): let in-flight scheduled jobs finish before cancelling them at shutdown
Cancelling every in-flight job the moment shutdown reached the scheduler
dropped the rows a write job had already popped: flush_gateway_requests
drains its accumulator before committing and does not restore it on
CancelledError, and update_spend requeues its batch only after the
shutdown drain had already run.

Shutdown now waits up to JOB_FINISH_TIMEOUT_SECONDS for in-flight jobs
to finish on their own, cancels the ones still running, and does both
before the shutdown flushes so a requeued batch is still written. The
cleanup run never finishes inside the grace, so it is still cancelled
and still records outcome="aborted".

Resolves LIT-6990
2026-09-18 20:00:48 +00:00
Yucheng He
8ce8dd9b3b fix(proxy): pause the scheduler at shutdown start and keep cleanup progress per run
Review follow-ups on #41213:

- Pause the scheduler as the first shutdown step so a job whose fire time
  falls inside the shutdown window does not start only to be cancelled.
  Jobs already running keep the whole window and are cancelled and
  awaited before the database disconnects, as before.
- Keep the cleanup run's progress in a task-scoped ContextVar rather than
  on the cleaner instance, so two runs overlapping on one cleaner
  (APSCHEDULER_MAX_INSTANCES above 1 without a Redis lock) each report
  their own rows and batches on cancellation.
- Drop the module docstrings the repository comment policy does not
  allow; the rationale lives in the PR description.
2026-09-18 20:00:47 +00:00
Yucheng He
c1c566db87 fix(proxy): record aborted outcome when spend-log cleanup is cancelled at shutdown
cleanup_old_spend_logs only caught Exception, so a run cut short by
CancelledError recorded no outcome and logged nothing. Under uvicorn the
job was never cancelled at all: uvicorn re-raises the captured SIGTERM as
soon as the lifespan shutdown returns, before asyncio cancels outstanding
tasks, so an in-flight scheduler job simply died with the process.

The cleanup now handles CancelledError by logging elapsed time, rows
deleted and batch count at error level, recording outcome="aborted", and
re-raising. The lifespan shutdown stops the scheduler and awaits the jobs
it cancels while the database is still connected, so that handler runs
under uvicorn too, and the pod lock is released instead of orphaned.

Resolves LIT-6990
2026-09-18 20:00:19 +00:00
Yassin Kortam
3a1c6bba97
feat(proxy): native /health/drain preStop hook for graceful shutdown (#29439) 2026-06-02 16:30:44 -07:00