forked from mtusi2026/mtusi2026lab5
46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
"""Exercise staging retention while producer/worker are explicitly stopped."""
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import time
|
|
import uuid
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
|
from src.storage import BUCKET,db,s3
|
|
from src.cleanup import sweep
|
|
from src.operator import replay
|
|
|
|
|
|
def main():
|
|
queued,failed = sys.argv[1:3]
|
|
client = s3()
|
|
with db() as conn:
|
|
rows = conn.execute('SELECT * FROM attachments WHERE id=ANY(%s::uuid[])',([queued,failed],)).fetchall()
|
|
assert len(rows)==2 and all(x['status']=='queued' for x in rows)
|
|
conn.execute("UPDATE attachments SET status='failed',error_code='invalid_image',updated_at=clock_timestamp()-interval '10 seconds' WHERE id=%s",(failed,))
|
|
conn.execute("UPDATE jobs SET status='failed',error_code='invalid_image' WHERE attachment_id=%s",(failed,))
|
|
old_key = 'staging/'+str(uuid.uuid4())
|
|
client.put_object(Bucket=BUCKET,Key=old_key,Body=b'orphan')
|
|
time.sleep(2)
|
|
fresh_key = 'staging/'+str(uuid.uuid4())
|
|
client.put_object(Bucket=BUCKET,Key=fresh_key,Body=b'fresh')
|
|
os.environ['STAGING_RETENTION_SECONDS']='1'
|
|
removed = sweep()
|
|
keys = {x['Key'] for x in client.list_objects_v2(Bucket=BUCKET,Prefix='staging/').get('Contents',[])}
|
|
assert 'staging/'+queued in keys
|
|
assert 'staging/'+failed not in keys and old_key not in keys and fresh_key in keys
|
|
replay_denied = False
|
|
try:
|
|
replay(failed)
|
|
except Exception:
|
|
replay_denied = True
|
|
assert replay_denied
|
|
client.delete_object(Bucket=BUCKET,Key=fresh_key)
|
|
print(json.dumps({'result':'PASS','removed':removed,'active_preserved':queued,'expired_failed':failed,
|
|
'old_orphan_removed':True,'fresh_orphan_preserved':True,'expired_replay_denied':True}))
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|