fix: trend_windows now upserts instead of accumulating (7.5GB→4MB), add competitive signal retention cleanup

This commit is contained in:
Celes Renata
2026-04-16 14:32:24 +00:00
parent 58a8726306
commit 6bab199159
3 changed files with 64 additions and 0 deletions
+29
View File
@@ -475,6 +475,7 @@ async def main() -> None:
logger.info("Scheduler started (tick=%ds)", SCHEDULER_TICK)
recovery_counter = 0
cleanup_counter = 0
try:
while True:
try:
@@ -486,6 +487,11 @@ async def main() -> None:
if recovery_counter >= 20:
recovery_counter = 0
await recover_stale_documents(pool, rds)
# Run signal cleanup periodically (~25 minutes)
cleanup_counter += 1
if cleanup_counter >= CLEANUP_CYCLE_INTERVAL:
cleanup_counter = 0
await cleanup_old_signals(pool)
finally:
await release_lock(rds, "scheduler_cycle")
except Exception:
@@ -542,5 +548,28 @@ async def recover_stale_documents(pool: asyncpg.Pool, rds: aioredis.Redis) -> in
return enqueued
# How often to run competitive signal cleanup (every ~100 cycles = ~25 minutes)
CLEANUP_CYCLE_INTERVAL: int = 100
# Keep competitive signals for this many days
COMPETITIVE_SIGNAL_RETENTION_DAYS: int = 30
async def cleanup_old_signals(pool: asyncpg.Pool) -> int:
"""Delete competitive signal records older than the retention window.
Prevents the competitive_signal_records table from growing unbounded.
Returns the number of rows deleted.
"""
result = await pool.execute(
"DELETE FROM competitive_signal_records WHERE computed_at < NOW() - INTERVAL '1 day' * $1",
COMPETITIVE_SIGNAL_RETENTION_DAYS,
)
# result is like "DELETE 1234"
count = int(result.split()[-1]) if result else 0
if count > 0:
logger.info("Cleaned up %d old competitive signal records", count)
return count
if __name__ == "__main__":
asyncio.run(main())