Clean up stale discovery files from dead processes.
()
| 111 | |
| 112 | /// Clean up stale discovery files from dead processes. |
| 113 | pub fn cleanup_stale_registrations() { |
| 114 | let dir = discovery_dir(); |
| 115 | let entries = match std::fs::read_dir(dir) { |
| 116 | Ok(e) => e, |
| 117 | Err(_) => return, |
| 118 | }; |
| 119 | |
| 120 | for entry in entries.flatten() { |
| 121 | let path = entry.path(); |
| 122 | if path.extension().and_then(|e| e.to_str()) != Some("json") { |
| 123 | continue; |
| 124 | } |
| 125 | let content = match std::fs::read_to_string(&path) { |
| 126 | Ok(c) => c, |
| 127 | Err(_) => continue, |
| 128 | }; |
| 129 | let info: VmmInstanceInfo = match serde_json::from_str(&content) { |
| 130 | Ok(i) => i, |
| 131 | Err(_) => continue, |
| 132 | }; |
| 133 | // Check if the process is still alive |
| 134 | let alive = Path::new(&format!("/proc/{}", info.pid)).exists(); |
| 135 | if !alive { |
| 136 | info!( |
| 137 | "removing stale discovery file for pid {} at {}", |
| 138 | info.pid, |
| 139 | path.display() |
| 140 | ); |
| 141 | let _ = std::fs::remove_file(&path); |
| 142 | } |
| 143 | } |
| 144 | } |
no test coverage detected