forked from mtusi2026/mtusi2026lab4
38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
"""Old upload intent reconciliation; dry run unless --apply, never deletes fresh objects."""
|
|
import argparse
|
|
import json
|
|
import time
|
|
from src.db import make_pool
|
|
from src.storage import s3, BUCKET
|
|
|
|
|
|
def sweep(apply=False):
|
|
pool = make_pool()
|
|
with pool.connection() as conn:
|
|
rows = conn.execute("SELECT i.object_key FROM object_intents i LEFT JOIN attachments a "
|
|
"ON a.object_key=i.object_key WHERE a.id IS NULL "
|
|
"AND i.created_at < clock_timestamp()-interval '1 hour' FOR UPDATE OF i SKIP LOCKED").fetchall()
|
|
for row in rows:
|
|
if apply:
|
|
s3.delete_object(Bucket=BUCKET,Key=row['object_key'])
|
|
conn.execute('DELETE FROM object_intents WHERE object_key=%s',(row['object_key'],))
|
|
pool.close()
|
|
return {'old_unreferenced_intents':len(rows),'applied':apply}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser=argparse.ArgumentParser()
|
|
parser.add_argument('--apply',action='store_true')
|
|
parser.add_argument('--loop',action='store_true')
|
|
args=parser.parse_args()
|
|
while True:
|
|
try:
|
|
print(json.dumps(sweep(args.apply)),flush=True)
|
|
except Exception as exc:
|
|
print(json.dumps({'cleanup_error':type(exc).__name__}),flush=True)
|
|
if not args.loop:
|
|
raise SystemExit(1)
|
|
if not args.loop:
|
|
break
|
|
time.sleep(60)
|