(session_log_dir: PathBuf, startup_trace_id: &str)
| 86 | } |
| 87 | |
| 88 | pub fn initialize_run_state(session_log_dir: PathBuf, startup_trace_id: &str) { |
| 89 | let logs_root = session_log_dir |
| 90 | .parent() |
| 91 | .map(Path::to_path_buf) |
| 92 | .unwrap_or_else(|| session_log_dir.clone()); |
| 93 | let run_state_path = logs_root.join(RUN_STATE_FILE); |
| 94 | let previous = detect_previous_unexpected_exit(&run_state_path); |
| 95 | let _ = PREVIOUS_UNEXPECTED_EXIT.set(previous); |
| 96 | |
| 97 | let context = RunContext { |
| 98 | logs_root, |
| 99 | session_log_dir: session_log_dir.clone(), |
| 100 | run_state_path, |
| 101 | }; |
| 102 | |
| 103 | let state = RunState { |
| 104 | app_version: env!("CARGO_PKG_VERSION").to_string(), |
| 105 | pid: std::process::id(), |
| 106 | started_at: Utc::now().to_rfc3339(), |
| 107 | updated_at: Utc::now().to_rfc3339(), |
| 108 | session_log_dir: session_log_dir.to_string_lossy().to_string(), |
| 109 | clean_shutdown: false, |
| 110 | shutdown_at: None, |
| 111 | exit_reason: None, |
| 112 | startup_trace_id: startup_trace_id.to_string(), |
| 113 | }; |
| 114 | |
| 115 | if let Err(error) = write_json_file(&context.run_state_path, &state) { |
| 116 | eprintln!("Warning: Failed to write run state: {}", error); |
| 117 | } |
| 118 | |
| 119 | let _ = CURRENT_RUN_CONTEXT.set(context); |
| 120 | } |
| 121 | |
| 122 | pub fn previous_unexpected_exit() -> Option<UnexpectedExitInfo> { |
| 123 | PREVIOUS_UNEXPECTED_EXIT.get().cloned().flatten() |
no test coverage detected