"""Remove expired upload reservations and their S3 objects.""" import argparse import logging import time from .attachments import AttachmentStorage from .config import Config from .db import Database from .errors import Unavailable from .store import Store from .job_lifecycle import clean_staging, clean_abandoned_outputs GRACE_SECONDS = 15 * 60 INTERVAL_SECONDS = 60 BATCH_SIZE = 25 def clean_batch(store, storage): count = 0 while count < BATCH_SIZE and store.clean_pending_attachment(GRACE_SECONDS, storage.delete): count += 1 return count def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--once', action='store_true', help='Process one bounded batch and exit') args = parser.parse_args() logging.basicConfig(level=logging.INFO) config = Config.from_env() database = Database(config) store = Store(config, database, None) storage = AttachmentStorage() try: database.open() while True: try: deleted = clean_batch(store, storage) for cleanup in (clean_staging, clean_abandoned_outputs): for _ in range(BATCH_SIZE): if not cleanup(database, storage.delete): break deleted += 1 if deleted or args.once: logging.info('Expired attachment reservations removed: %s', deleted) except Unavailable: logging.warning('Attachment cleanup deferred because a dependency is unavailable') if args.once: return 1 if args.once: return 0 time.sleep(INTERVAL_SECONDS) finally: database.close() if __name__ == '__main__': raise SystemExit(main())