Spaces:
Running on T4
Running on T4
File size: 4,167 Bytes
2756f0f 7a3215f 2756f0f 7a3215f 2756f0f 7a3215f 2756f0f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | """The scheduled cleanup job."""
import uuid
from datetime import UTC, datetime, timedelta
from sqlalchemy import func, select
from app.core.database import get_session_factory
from app.integrations.storage import get_storage
from app.jobs.cleanup import run_cleanup
from app.modules.idempotency.models import IdempotencyKey
from app.modules.sessions.models import SwingSession
from app.modules.uploads.models import Upload
from tests.factories import create_session, make_account, mark_analyzed, set_session, upload
async def _keys(upload_ids) -> list[str]:
async with get_session_factory()() as db:
return list((await db.scalars(select(Upload.object_key).where(
Upload.id.in_([uuid.UUID(str(i)) for i in upload_ids])))).all())
async def _session_upload_ids(session_id) -> list:
async with get_session_factory()() as db:
s = await db.get(SwingSession, uuid.UUID(session_id))
return [s.video_upload_id, s.thumbnail_upload_id]
async def _cleanup(now: datetime) -> dict:
async with get_session_factory()() as db:
return await run_cleanup(db, get_storage(), now)
async def _count(model) -> int:
async with get_session_factory()() as db:
return await db.scalar(select(func.count()).select_from(model))
async def test_cleanup_removes_expired_rows_and_objects(client):
storage = get_storage()
player = await make_account()
h = player["headers"]
now = datetime.now(UTC)
stale_upload = await upload(client, h)
stale_key = (await _keys([stale_upload]))[0]
abandoned = await create_session(client, h)
abandoned_keys = await _keys(await _session_upload_ids(abandoned["id"]))
analysed = await create_session(client, h)
await mark_analyzed(analysed["id"], 60)
old_deleted = await create_session(client, h)
await mark_analyzed(old_deleted["id"], 50)
old_deleted_keys = await _keys(await _session_upload_ids(old_deleted["id"]))
recent_deleted = await create_session(client, {**h, "Idempotency-Key": "k1"})
await mark_analyzed(recent_deleted["id"], 50)
later = now + timedelta(hours=25)
await set_session(old_deleted["id"], deleted_at=later - timedelta(days=31))
await set_session(recent_deleted["id"], deleted_at=later - timedelta(days=29))
fresh_upload = await upload(client, h) # created "now", so not yet stale at `later`
async with get_session_factory()() as db:
await db.execute(Upload.__table__.update().where(Upload.id == uuid.UUID(fresh_upload))
.values(created_at=later - timedelta(hours=1)))
await db.commit()
counts = await _cleanup(later)
assert counts == {"unattached_uploads": 1, "pending_sessions": 1, "deleted_sessions": 1, "objects": 5,
"idempotency_keys": 1, "api_request_logs": 0}
for key in [stale_key, *abandoned_keys, *old_deleted_keys]:
assert await storage.head(key) is None, key
async with get_session_factory()() as db:
remaining = set((await db.scalars(select(SwingSession.id))).all())
assert remaining == {uuid.UUID(analysed["id"]), uuid.UUID(recent_deleted["id"])}
assert await db.get(Upload, uuid.UUID(fresh_upload)) is not None
assert await db.get(Upload, uuid.UUID(stale_upload)) is None
assert await _count(Upload) == 5 # two sessions with video + thumbnail, plus the fresh upload
assert await _count(IdempotencyKey) == 0
assert await _cleanup(later) == {"unattached_uploads": 0, "pending_sessions": 0, "deleted_sessions": 0,
"objects": 0, "idempotency_keys": 0, "api_request_logs": 0}
async def test_cleanup_keeps_recent_data(client):
player = await make_account()
await upload(client, player["headers"])
await create_session(client, {**player["headers"], "Idempotency-Key": "k2"})
counts = await _cleanup(datetime.now(UTC) + timedelta(hours=23))
assert counts == {"unattached_uploads": 0, "pending_sessions": 0, "deleted_sessions": 0, "objects": 0,
"idempotency_keys": 0, "api_request_logs": 0}
assert await _count(SwingSession) == 1 and await _count(Upload) == 3
|