forked from mtusi2026/mtusi2026lab4
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
"""Периодическая компенсация незавершённых загрузок; отдельный Compose-сервис."""
|
|
|
|
import argparse
|
|
import asyncio
|
|
from dataclasses import asdict
|
|
import json
|
|
|
|
from app.core.config import Settings
|
|
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.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) -> bool:
|
|
completed = True
|
|
try:
|
|
async with factory() as session:
|
|
result = await reconcile_attachments(session, storage, dry_run=not apply)
|
|
print(json.dumps(asdict(result)), 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()
|
|
s3_settings = S3Settings()
|
|
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)
|
|
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()
|