fix(api): reserve db pool for serving readbacks
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled

This commit is contained in:
ogt
2026-07-09 19:11:39 +08:00
parent 2983f755b2
commit f156b17b3e
2 changed files with 154 additions and 41 deletions

View File

@@ -257,6 +257,51 @@ def test_api_lifespan_closes_worker_redis_pool_after_signal_worker() -> None:
assert source.index("close_worker_redis_pool") < source.index("close_redis_pool")
def test_api_background_tasks_default_off_when_db_pool_has_no_spare_slot(monkeypatch) -> None:
from src import main as api_main
monkeypatch.delenv(api_main.API_BACKGROUND_TASKS_ENV, raising=False)
monkeypatch.setattr(api_main.settings, "DATABASE_POOL_SIZE", 1)
monkeypatch.setattr(api_main.settings, "DATABASE_MAX_OVERFLOW", 0)
assert api_main.should_start_api_background_tasks() is False
def test_api_background_tasks_default_on_when_db_pool_has_spare_slot(monkeypatch) -> None:
from src import main as api_main
monkeypatch.delenv(api_main.API_BACKGROUND_TASKS_ENV, raising=False)
monkeypatch.setattr(api_main.settings, "DATABASE_POOL_SIZE", 2)
monkeypatch.setattr(api_main.settings, "DATABASE_MAX_OVERFLOW", 0)
assert api_main.should_start_api_background_tasks() is True
def test_api_background_tasks_env_override_controls_serving_budget(monkeypatch) -> None:
from src import main as api_main
monkeypatch.setattr(api_main.settings, "DATABASE_POOL_SIZE", 1)
monkeypatch.setattr(api_main.settings, "DATABASE_MAX_OVERFLOW", 0)
monkeypatch.setenv(api_main.API_BACKGROUND_TASKS_ENV, "true")
assert api_main.should_start_api_background_tasks() is True
monkeypatch.setenv(api_main.API_BACKGROUND_TASKS_ENV, "false")
assert api_main.should_start_api_background_tasks() is False
def test_api_lifespan_routes_background_loops_through_serving_budget_gate() -> None:
import inspect
from src import main as api_main
source = inspect.getsource(api_main.lifespan)
assert "schedule_api_background_task(run_incident_analysis_sweeper())" in source
assert "schedule_api_background_task(run_km_backfill_reconciler_loop())" in source
assert "signal_worker_skipped" in source
assert "platform_worker_skipped" in source
def test_init_db_backfills_all_optional_knowledge_read_columns() -> None:
import inspect