"""Switch existing PanoData jobs to final-video-only at a completed sample.""" from pathlib import Path import fcntl import importlib.util import json import os import re import shutil import signal import subprocess import sys import time ROOT=Path(__file__).resolve().parents[1] IO=ROOT/'pano_inference/inference_io.py' spec=importlib.util.spec_from_file_location('inference_io',IO) io=importlib.util.module_from_spec(spec);spec.loader.exec_module(io) LOGS=ROOT/'logs' def identity(pid): try: fields=Path(f'/proc/{pid}/stat').read_text().rsplit(')',1)[1].split() return None if fields[0]=='Z' else fields[19] except FileNotFoundError:return None def write_status(path,state,**kwargs): payload={'state':state,'updated_at':time.strftime('%Y-%m-%d %H:%M:%S %z'),**kwargs} tmp=path.with_suffix('.tmp');tmp.write_text(json.dumps(payload,indent=2));tmp.replace(path) print(json.dumps(payload),flush=True) def main(k): lock=(LOGS/f'panodata_slim_rollover_k{k}.lock').open('w') fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) request=json.loads((LOGS/f'panodata_slim_rollover_k{k}_request.json').read_text()) old=request['pid'];start=request['start_ticks'] path=LOGS/f'panodata_slim_rollover_k{k}_status.json' output=ROOT/f'outputs_panodata/LiveWorld-K{k}' name=request['current_sample'] write_status(path,'waiting_for_current_sample',pid=old,current_sample=name,gpu=request['gpu']) target=output/f'{name}.mp4' while identity(old)==start: if target.is_file() and io.complete_video(target,frames=81,fps=16,width=832,height=480): # The atomic final MP4 is already encoded and verified. Stop only our # old inference process; finished samples are skipped by the new runner. os.kill(old,signal.SIGTERM) break time.sleep(1) while identity(old)==start: time.sleep(.5) supervisor=request['supervisor_pid'] while identity(supervisor)==request['supervisor_start_ticks']: time.sleep(.5) work=output/'_work' if work.exists(): if work.is_symlink():raise RuntimeError(f'Refusing to clean symlink: {work}') shutil.rmtree(work) script='scripts/run_panodata_all.py' if k==1 else 'scripts/run_panodata_k21_all.py' logfile=LOGS/('panodata_all_launcher.log' if k==1 else 'panodata_k21_all_launcher.log') with logfile.open('ab') as log: child=subprocess.Popen([sys.executable,'-u',script],cwd=ROOT,stdin=subprocess.DEVNULL, stdout=log,stderr=subprocess.STDOUT,start_new_session=True) pidfile=LOGS/('panodata_all_launcher.pid' if k==1 else 'panodata_k21_all_launcher.pid') pidfile.write_text(str(child.pid)+'\n') write_status(path,'switched_to_final_video_only',supervisor_pid=child.pid,gpu=request['gpu'], completed_boundary_sample=name,workspace_retention=False) if __name__=='__main__': main(int(sys.argv[1]))