43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
"""Remove Arena check records older than the retention window."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sqlite3
|
|
import sys
|
|
import time
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
DB_PATH = os.environ.get("DB_PATH", "/data/arena.db")
|
|
RETENTION_DAYS = int(os.environ.get("CHECK_RETENTION_DAYS", "30"))
|
|
|
|
|
|
def cleanup(db_path: str = DB_PATH, retention_days: int = RETENTION_DAYS) -> int:
|
|
if retention_days < 1:
|
|
raise ValueError("CHECK_RETENTION_DAYS must be >= 1")
|
|
cutoff = (datetime.now(timezone.utc) - timedelta(days=retention_days)).isoformat()
|
|
db = sqlite3.connect(db_path, timeout=30)
|
|
try:
|
|
db.execute("PRAGMA busy_timeout=30000")
|
|
before = db.total_changes
|
|
db.execute("BEGIN IMMEDIATE")
|
|
db.execute("DELETE FROM checks WHERE checked_at < ?", (cutoff,))
|
|
removed = db.total_changes - before
|
|
db.commit()
|
|
# Keep the persistent WAL bounded without a full VACUUM/long lock.
|
|
db.execute("PRAGMA wal_checkpoint(PASSIVE)")
|
|
print(f"[cleanup] removed={removed} cutoff={cutoff} retention_days={retention_days}")
|
|
return removed
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
cleanup()
|
|
except Exception as exc:
|
|
print(f"[cleanup] ERROR: {exc}", file=sys.stderr)
|
|
raise
|