"""启动前清空状态:SQLite、watch 旧 JSON、测量/行为运行时目录。 由 start.sh / start_fresh.sh 在 uvicorn 之前调用,使 FishMeasure 与 FishAction 均在无缓存下重新推理。 """ from __future__ import annotations from pathlib import Path from app.db import clear_runtime_compute_dirs, remove_sqlite_database_files from app.settings import get_settings def _rm_legacy_json(path: Path | None) -> None: if path is None: return try: if path.is_file(): path.unlink() print(f"[prestart-fresh] removed legacy JSON {path}", flush=True) except OSError as e: print(f"[prestart-fresh] skip {path}: {e}", flush=True) def run_prestart_fresh() -> None: s = get_settings() remove_sqlite_database_files(s) print( f"[prestart-fresh] removed SQLite at {s.sqlite_path} (and -wal/-shm if present).", flush=True, ) clear_runtime_compute_dirs(s) print( "[prestart-fresh] cleared compute dirs: " f"measure_output={s.measure_output_root}, " f"action_output={s.action_output_root}, " f"media={s.media_root}, stream_tmp={s.stream_tmp_dir}", flush=True, ) if s.measure_watch_use_state_file: if s.measure_watch_state_file is not None: _rm_legacy_json(s.measure_watch_state_file) elif s.measure_watch_dir is not None: _rm_legacy_json( s.measure_watch_dir / ".fishmeasure_watch_processed.json" ) if s.action_watch_use_state_file: if s.action_watch_state_file is not None: _rm_legacy_json(s.action_watch_state_file) elif s.action_watch_dir is not None: _rm_legacy_json( s.action_watch_dir / ".fishaction_watch_processed.json" ) print("[prestart-fresh] done.", flush=True) def main() -> None: run_prestart_fresh() if __name__ == "__main__": main()