Files
mtusi2026lab5-fork/app/core/cleanup.py
T

67 lines
2.3 KiB
Python

"""Периодическая компенсация незавершённых загрузок; отдельный Compose-сервис."""
import argparse
import asyncio
from dataclasses import asdict
import json
from app.core.config import Settings
from app.core.jobs import JobSettings
from app.core.database import create_db_engine, create_session_factory
from app.core.s3 import S3Settings
from app.domain.attachments import reconcile_attachments
from app.domain.attachment_jobs import cleanup_staging
from app.errors.handlers import RECOVERABLE_STORAGE_ERRORS, log_recoverable_error
from app.storage.s3 import create_s3_storage
async def cleanup_once(factory, storage, apply: bool, jobs: JobSettings) -> bool:
completed = True
try:
async with factory() as session:
result = await reconcile_attachments(session, storage, dry_run=not apply)
staging = await cleanup_staging(session, storage, jobs, apply=apply)
print(
json.dumps({**asdict(result), "terminal_staging": staging}), flush=True
)
except RECOVERABLE_STORAGE_ERRORS as error:
log_recoverable_error("Reconciliation", error)
completed = False
return completed
async def run(once: bool, apply: bool) -> None:
settings = Settings() # type: ignore
s3_settings = S3Settings()
jobs = JobSettings()
engine = create_db_engine(settings)
factory = create_session_factory(engine)
storage = create_s3_storage(s3_settings)
try:
while True:
completed = await cleanup_once(factory, storage, apply, jobs)
if once:
if not completed:
raise SystemExit(1)
break
await asyncio.sleep(s3_settings.cleanup_interval_seconds)
finally:
await storage.aclose()
await engine.dispose()
def main() -> None:
parser = argparse.ArgumentParser(description="Сверка журнала загрузок MinIO/БД")
parser.add_argument("--once", action="store_true")
parser.add_argument(
"--apply",
action="store_true",
help="удалять безопасные кандидаты; по умолчанию dry-run",
)
args = parser.parse_args()
asyncio.run(run(args.once, args.apply))
if __name__ == "__main__":
main()