cmvr-visual/backend/app/services/retention.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

51 lines
2.1 KiB
Python

"""Conservative first-release GC: inventory only, then explicit opt-in deletion.
Unknown/untracked files are never deleted. Asset references protect live snapshots.
"""
from datetime import timedelta
from sqlalchemy import select
from app.models import Asset, JobAsset, utcnow
from app.models import AssetReference, SharedAsset
from app.adapters.storage import confined
async def collect_garbage(db, settings, dry_run=True, grace_days=14):
report = {"dry_run": dry_run, "candidates": [], "deleted": [], "protected": []}
async with db.session() as session:
rows = (
await session.scalars(
select(Asset).where(Asset.deleted_at < utcnow() - timedelta(days=grace_days))
)
).all()
for asset in rows:
referenced = await session.scalar(
select(JobAsset.job_id).where(JobAsset.asset_id == asset.id).limit(1)
)
snapshot = await session.scalar(
select(AssetReference.id).where(AssetReference.asset_id == asset.id).limit(1)
)
publication = await session.scalar(
select(SharedAsset.id)
.where(SharedAsset.asset_id == asset.id, SharedAsset.withdrawn_at.is_(None))
.limit(1)
)
if referenced or snapshot or publication or (not asset.ready):
report["protected"].append(asset.id)
continue
report["candidates"].append(asset.id)
if not dry_run:
from app.core.maintenance import maintenance_active
if not maintenance_active(settings):
raise RuntimeError("Physical GC requires maintenance")
for key in (asset.storage_key, asset.thumbnail_key):
if key:
path = confined(settings.asset_root, key)
if path.is_file():
path.unlink()
asset.ready = False
report["deleted"].append(asset.id)
if not dry_run:
await session.commit()
return report