rmi-backend/databus_warm_cron.py
cryptorugmunch cfd75fd1a0 fix(lint): drop ruff errors from 1470 to 0 across app/ and tests/
- Exclude generated SDK (sdks/python) and operational scripts from ruff lint
- Add targeted per-file ignores for ASYNC*, S310, S603, S607, S108, S314,
  S102, PIE810, SIM102 in scripts/
- Auto-fix safe categories (I001, F401, W292, F841, PIE790, RUF100, etc.)
- Bulk-fix S110 (try-except-pass), S112 (try-except-continue), S311 (random),
  S324 (md5/sha1), S301 (pickle) and similar lint categories
- Rename N806 non-lowercase locals, including ML X/y variables preserved
  with noqa for scikit-learn conventions
- Replace urllib.request calls with httpx.AsyncClient / httpx.Client (S310)
- Wrap blocking os.path/os calls in asyncio.get_running_loop().run_in_executor
- Replace subprocess.run with asyncio.create_subprocess_exec in async contexts
- Store asyncio.create_task return values in _background_tasks set (RUF006)
- Convert hardcoded subprocess binary names to absolute paths (S607) where
  appropriate; add noqa where path is config-driven (CAST_PATH, etc.)
- Parameterize SQL queries with placeholders and add noqa for sanitized inputs
- Fix all mechanical categories: SIM102, PIE810, TC001/2/3, S108, S314, S107,
  S306, S301, N802/N815/N817, S104, S605, S501, RUF022, UP031
- Add missing 'import asyncio' where referenced but not imported (F821)
- Fix E402 module-import-not-at-top by adding '# noqa: E402' for circular-import
  safe cases and code-defined imports
- Remove hardcoded Redis password in databus_warm_cron.py; use env vars

Tests:
- Add tests/unit/core/test_ai_router.py (8 tests): model resolution, chat
  completion with mocked httpx, fallback to OpenRouter, no-provider error,
  streaming
- Add tests/unit/core/test_tracing.py (7 tests): setup_otel disabled/enabled,
  shutdown_otel, span helpers, tracing-enabled route registration
- Add tests/unit/core/test_langfuse.py (2 tests): no-env init, noop flush
- Fix tests/unit/domain/scanner/test_service.py to import from the moved
  app.domains.scanners.core.service

Result: 'ruff check .' passes with 0 errors (was 1470).
Pytest: 808 passed, 1 skipped (no regressions).
2026-07-08 03:36:58 +07:00

70 lines
1.9 KiB
Python
Executable file

#!/usr/bin/env python3
"""
DataBus Cache Warmer - called by cron every 5 minutes.
Seeds top-10 most-missed chains if not present, then starts/verifies
the background warm loop.
"""
import asyncio
import os
import sys
os.environ.setdefault("REDIS_HOST", "localhost")
os.environ.setdefault("REDIS_PORT", "6379")
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.domains.databus.cache import get_cache
from app.domains.databus.core import databus
async def warm():
cache = get_cache()
await databus.initialize()
# Seed top-10 most-missed chains if L1 is cold
l1_keys = cache.l1.stats()["keys"]
if l1_keys < 5:
warm_types = [
"token_price",
"token_metadata",
"holder_data",
"wallet_labels",
"wallet_tokens",
"live_prices",
"market_overview",
"trending",
"news",
"alerts",
]
for dtype in warm_types:
ttl = cache.ttl_config.get(dtype, 60)
cache_key = f"databus:{dtype}:warm-{dtype}-001"
warmed_data = {
"source": "cache_warmer",
"data_type": dtype,
"status": "cached",
"data": {"note": f"Pre-warmed {dtype}"},
}
await cache.set(cache_key, warmed_data, ttl=ttl, data_type=dtype)
# Start warm loop if not already running
if not databus._warm_running:
await databus.start_cache_warm(interval_seconds=120)
print("[databus-warm] Background loop started")
else:
print("[databus-warm] Loop already running")
# Report
h = await cache.health()
keys = (
len(await cache.l2._redis.keys("databus:*"))
if cache.l2._available
else cache.l1.stats()["keys"]
)
print(
f"[databus-warm] L1 keys={cache.l1.stats()['keys']}, L2 keys~={keys}, hit_rate={h['combined_hit_rate']}%"
)
asyncio.run(warm())