import json
import os
import subprocess
import time
from run import R,REPO,snapshot
os.umask(0o077)
assert json.loads((R/'ledger.json').read_text())['exit']==2
assert not list(R.glob('*/video-*.mp4'))
assert not (R/'retry.json').exists()
before=snapshot()
record=dict(reason='explicit Reference module.to(cuda); prior failure before any input inference',attempt=2,
            correction_failures=1,status='RUNNING')
(R/'retry.json').write_text(json.dumps(record,indent=2))
cmd=['python3','-m','model_tooling.fire_video','--input','/data/playlist/uploads/260521_화재추론샘플_시뮬레이션영상',
     '--output',str(R),'--mode','compare','--fps','10','--max-frames','200','--max-output-gib','1','--timeout','600']
with (R/'retry-launch.log').open('x') as log:
    p=subprocess.run(cmd,cwd=REPO,stdout=log,stderr=log,timeout=650)
assert snapshot()==before
record.update(exit=p.returncode,status='COMPLETE' if p.returncode==0 else 'FAILED',finished_utc=time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),service_unchanged=True)
(R/'retry.json').write_text(json.dumps(record,indent=2))
print(json.dumps(record))
