forked from mtusi2026/mtusi2026lab5
38 lines
2.5 KiB
Python
38 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Publish a queued job into Redis Streams, restart broker with AOF volume, then consume."""
|
|
import hashlib, json, os, subprocess, time
|
|
from pathlib import Path
|
|
from client import Client, arguments, check, run
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
def compose(*args,pause=None,output=False):
|
|
env=os.environ.copy()
|
|
if pause is not None: env['WORKER_PAUSE_CONSUME']=str(pause)
|
|
cmd=['docker','compose',*args]
|
|
return subprocess.check_output(cmd,cwd=ROOT,env=env,text=True) if output else subprocess.run(cmd,cwd=ROOT,env=env,check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
|
|
|
def stream_len(): return int(compose('exec','-T','broker','redis-cli','XLEN','attachment-jobs',output=True).strip())
|
|
|
|
def main():
|
|
a=arguments('Broker volume restart with one published, unconsumed job')
|
|
compose('up','-d','--force-recreate','worker',pause=1)
|
|
try:
|
|
c=Client(a.base_url,a.ca_file); alice,bob,_,chat=c.bootstrap(); before=stream_len(); raw=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
|
path='/api/v1/chats/'+chat['id']+'/attachments?filename=broker-restart.png'
|
|
att=c.call('POST',path,202,actor=alice,raw=raw,headers={'Content-Type':'image/png'},schema='Attachment')['json']
|
|
deadline=time.monotonic()+20
|
|
while stream_len()<=before and time.monotonic()<deadline: time.sleep(.1)
|
|
published=stream_len(); check(published>before,'outbox relay did not publish the queued job')
|
|
status=c.call('GET','/api/v1/attachments/'+att['id'],actor=bob,schema='Attachment')['json']['status']
|
|
check(status=='queued','paused worker unexpectedly consumed the job')
|
|
time.sleep(1.2); compose('stop','worker'); compose('restart','broker')
|
|
after=stream_len(); check(after>=published,'Redis AOF volume lost the published stream entry')
|
|
compose('up','-d','--force-recreate','worker',pause=0)
|
|
ready=c.wait_ready(bob,att,wait_seconds=90)
|
|
check(ready['status']=='ready' and ready['sha256']==hashlib.sha256(raw).hexdigest(),'job did not finish after broker restart')
|
|
result={'attachment_id':att['id'],'status_before_broker_restart':status,'stream_length_before_restart':published,'stream_length_after_restart':after,'final_status':ready['status'],'original_sha256_matches':True,'result':'PASS'}
|
|
(ROOT/'evidence/broker-restart.json').write_text(json.dumps(result,indent=2)+'\n'); print(json.dumps(result,indent=2))
|
|
finally: compose('up','-d','--force-recreate','worker',pause=0)
|
|
|
|
if __name__=='__main__': run(main)
|