import subprocess,pathlib,os,json,datetime root=pathlib.Path('/opt/lingniu-go-native/backups/mileage-all-20260915-18');root.mkdir(parents=True,exist_ok=True) os.umask(0o077) units=['lingniu-go-stat-writer.service','lingniu-go-stats-backfill.timer','lingniu-vehicle-open-stat.timer'] state={u:subprocess.call(['systemctl','is-active','--quiet',u])==0 for u in units} assert subprocess.call(['systemctl','is-active','--quiet','lingniu-go-stats-backfill.service'])!=0,'Backfill running' (root/'service-state.json').write_text(json.dumps(state)) report={'startedAt':datetime.datetime.now().isoformat(),'days':[],'release':str(pathlib.Path('/opt/lingniu-go-native/current').resolve())} try: subprocess.check_call(['systemctl','stop']+units) subprocess.check_call(['python3','/tmp/mileage-all-backup-20260918.py']) subprocess.check_call(['gzip','-t',str(root/'mileage-tables.sql.gz')]) for day in ['2026-09-15','2026-09-16','2026-09-17','2026-09-18']: env=dict(os.environ) env.update(BACKFILL_DATE_FROM=day,BACKFILL_DATE_TO=day,BACKFILL_PROTOCOLS='GB32960,JT808,YUTONG_MQTT',BACKFILL_VINS='',BACKFILL_METHOD='last_diff',BACKFILL_DRY_RUN='false',BACKFILL_RESET='false',BACKFILL_LIMIT='0',BACKFILL_GPS_FALLBACK='true',BACKFILL_BASELINE_LOOKBACK_DAYS='7') print('START '+day,flush=True) log=root/(day+'.log') with log.open('w') as f: result=subprocess.run(['/opt/lingniu-go-native/current/stats-backfill'],env=env,stdout=f,stderr=subprocess.STDOUT,timeout=2700) assert result.returncode==0,'Failed '+day+', see '+str(log) report['days'].append(day);(root/'progress.json').write_text(json.dumps(report)) print('DONE '+day,flush=True) report['complete']=True finally: for unit,active in state.items(): if active:subprocess.check_call(['systemctl','start',unit]) report['finishedAt']=datetime.datetime.now().isoformat() (root/'result.json').write_text(json.dumps(report,indent=2)) print(json.dumps(report),flush=True)