feat: add automatic event cleanup based on retention setting
Hourly background task deletes events older than configured retention period (default 90 days). Reads event_retention_days from settings table. Includes 2 tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""Automatic event cleanup based on retention settings."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from database import get_connection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def cleanup_old_events():
|
||||
"""Delete events older than the configured retention period. Runs hourly."""
|
||||
while True:
|
||||
try:
|
||||
conn = get_connection()
|
||||
row = conn.execute(
|
||||
"SELECT value FROM settings WHERE key='event_retention_days'"
|
||||
).fetchone()
|
||||
days = int(row["value"]) if row else 90
|
||||
|
||||
result = conn.execute(
|
||||
"DELETE FROM events WHERE created_at < datetime('now', ? || ' days')",
|
||||
(f"-{days}",),
|
||||
)
|
||||
conn.commit()
|
||||
if result.rowcount > 0:
|
||||
logger.info(
|
||||
"Cleaned up %d events older than %d days",
|
||||
result.rowcount,
|
||||
days,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Cleanup failed")
|
||||
|
||||
await asyncio.sleep(3600)
|
||||
Reference in New Issue
Block a user