forked from mtusi2026/mtusi2026lab5
60 lines
2.9 KiB
Python
60 lines
2.9 KiB
Python
"""Удаляет только безопасно завершённый staging или старые бесхозные исходники."""
|
|
import argparse
|
|
from datetime import datetime,timezone
|
|
import json
|
|
import time
|
|
import uuid
|
|
from src.config import STAGING_RETENTION
|
|
from src.jobs import connection,event
|
|
from src.storage import ObjectStore,lock_attachment,unlock_attachment
|
|
|
|
|
|
def run(retention=STAGING_RETENTION,apply=False):
|
|
storage=ObjectStore();actions=[];now=datetime.now(timezone.utc)
|
|
try:
|
|
objects=list(storage.objects('staging/'))
|
|
with connection() as db:
|
|
for obj in objects:
|
|
parts=obj['Key'].split('/')
|
|
try:attachment_id=uuid.UUID(parts[1]);job_id=uuid.UUID(parts[2])
|
|
except (ValueError,IndexError):continue
|
|
if len(parts)!=3:continue
|
|
if not lock_attachment(db,attachment_id,wait=False):db.commit();continue
|
|
db.commit()
|
|
try:
|
|
job=db.execute('SELECT * FROM jobs WHERE id=%s',(job_id,)).fetchone()
|
|
current=storage.head(obj['Key'])
|
|
if current is None:continue
|
|
object_old=(now-current['LastModified']).total_seconds()>=retention
|
|
safe=False
|
|
if not job:safe=object_old
|
|
elif job['status']=='ready':safe=True
|
|
elif job['status'] in {'failed','superseded'}:
|
|
safe=object_old and (now-job['updated_at']).total_seconds()>=retention
|
|
if not safe:continue
|
|
actions.append(dict(key=obj['Key'],reason='committed_ready' if job and job['status']=='ready' else 'retention',applied=apply))
|
|
if apply:
|
|
storage.delete(obj['Key'])
|
|
if job:
|
|
db.execute('UPDATE jobs SET staged_deleted_at=clock_timestamp() WHERE id=%s',(job_id,))
|
|
event(db,job_id,'staging_cleaned');db.commit()
|
|
finally:db.rollback();unlock_attachment(db,attachment_id);db.commit()
|
|
finally:storage.client.close()
|
|
return dict(mode='apply' if apply else 'dry-run',retention_seconds=retention,objects_scanned=len(objects),actions=actions)
|
|
|
|
|
|
def main():
|
|
p=argparse.ArgumentParser();p.add_argument('--apply',action='store_true');p.add_argument('--loop',action='store_true')
|
|
p.add_argument('--retention',type=int,default=STAGING_RETENTION);args=p.parse_args()
|
|
if args.retention<1:p.error('Positive retention required')
|
|
while True:
|
|
try:print(json.dumps(run(args.retention,args.apply)),flush=True)
|
|
except Exception as exc:
|
|
print(json.dumps(dict(status='FAILED',reason=type(exc).__name__)),flush=True)
|
|
if not args.loop:raise SystemExit(1)
|
|
if not args.loop:break
|
|
time.sleep(60)
|
|
|
|
|
|
if __name__=='__main__':main()
|