feat: burn-after-read and expiry cleaner
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""Periodic cleanup of expired and burned items."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models import Item
|
||||
from app.services import items as items_service
|
||||
|
||||
|
||||
def run_once(db: Session, *, now: datetime | None = None) -> int:
|
||||
"""Delete expired rows (+ files) and burned rows (leftover files).
|
||||
|
||||
Returns the number of item rows removed.
|
||||
"""
|
||||
now = now or datetime.utcnow()
|
||||
rows = list(
|
||||
db.scalars(
|
||||
select(Item).where(
|
||||
or_(
|
||||
Item.burned.is_(True),
|
||||
(Item.expires_at.is_not(None) & (Item.expires_at <= now)),
|
||||
)
|
||||
)
|
||||
).all()
|
||||
)
|
||||
for item in rows:
|
||||
items_service.delete_item_file(item)
|
||||
db.delete(item)
|
||||
if rows:
|
||||
db.commit()
|
||||
return len(rows)
|
||||
Reference in New Issue
Block a user