fix(proxy): import first_cleanup_run_time at module level so startup scheduling does not NameError

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Devin AI 2026-09-15 09:08:19 +00:00
parent 8844fe6120
commit b27d92bd62
2 changed files with 34 additions and 5 deletions

View file

@ -430,6 +430,7 @@ from litellm.proxy.db.db_transaction_queue.pod_lock_manager import PodLockManage
from litellm.proxy.db.db_transaction_queue.spend_log_cleanup import (
SPEND_LOG_CLEANUP_BOUND_SETTINGS,
SpendLogCleanup,
first_cleanup_run_time,
)
from litellm.proxy.db.db_transaction_queue.window_spend_update_queue import (
build_window_spend_transaction,
@ -6996,11 +6997,6 @@ class ProxyConfig:
autorouter_retention: Final = general_settings.get("maximum_autorouter_session_retention_period")
health_check_retention: Final = general_settings.get("maximum_health_check_retention_period")
if retention_period is not None or autorouter_retention is not None or health_check_retention is not None:
from litellm.proxy.db.db_transaction_queue.spend_log_cleanup import (
SpendLogCleanup,
first_cleanup_run_time,
)
spend_log_cleanup: Final = SpendLogCleanup()
cleanup_cron: Final = general_settings.get("maximum_spend_logs_cleanup_cron")

View file

@ -7618,6 +7618,39 @@ async def test_batch_cost_poller_is_confirmed_before_serving(monkeypatch):
assert probe_where["batch_processed"] is False
@pytest.mark.asyncio
async def test_startup_schedules_first_retention_cleanup_soon_after_boot(monkeypatch):
monkeypatch.delenv("STORE_MODEL_IN_DB", raising=False)
from litellm.proxy.proxy_server import ProxyStartupEvent
from litellm.proxy.utils import ProxyLogging
mock_prisma_client = MagicMock()
mock_prisma_client.db.litellm_config.find_first = AsyncMock(return_value=None)
mock_proxy_logging = MagicMock(spec=ProxyLogging)
mock_proxy_logging.slack_alerting_instance = MagicMock()
mock_proxy_logging.db_spend_update_writer = MagicMock()
with (
patch("litellm.proxy.proxy_server.proxy_config", _mock_scheduled_proxy_config()),
patch("litellm.proxy.proxy_server.store_model_in_db", False),
patch("litellm.proxy.proxy_server.get_secret_bool", return_value=False),
):
before = datetime.now(timezone.utc)
await ProxyStartupEvent.initialize_scheduled_background_jobs(
general_settings={"maximum_health_check_retention_period": "90d"},
prisma_client=mock_prisma_client,
proxy_budget_rescheduler_min_time=1,
proxy_budget_rescheduler_max_time=2,
proxy_batch_write_at=5,
proxy_logging_obj=mock_proxy_logging,
)
job = proxy_server_module.scheduler.get_job("spend_log_cleanup_job")
assert job is not None
assert 0 < (job.next_run_time - before).total_seconds() < 3600
assert job.trigger.interval.total_seconds() == 24 * 3600
@pytest.mark.asyncio
async def test_store_model_in_db_db_override_when_config_false():
"""