pscand-core: improve crash detection with stopped status check

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Icd4c2f29c321df38fc22a98d8110fa836a6a6964
This commit is contained in:
raf 2026-02-18 23:29:23 +03:00
commit 2fe05c6466
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -27,7 +27,7 @@ pub enum LogLevel {
}
impl LogLevel {
pub fn from_str(s: &str) -> Self {
pub fn parse(s: &str) -> Self {
match s.to_lowercase().as_str() {
"debug" => LogLevel::Debug,
"info" => LogLevel::Info,
@ -488,7 +488,7 @@ impl Heartbeat {
.unwrap_or(0);
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent)?;
fs::create_dir_all(parent)?
}
let mut file = fs::File::create(&self.path)?;
@ -528,8 +528,8 @@ impl CrashDetector {
pub fn write_state(&self, stats: &RuntimeStats) -> std::io::Result<()> {
if let Some(parent) = self.state_file.parent() {
fs::create_dir_all(parent)?;
}
fs::create_dir_all(parent)?
};
let json = serde_json::to_string_pretty(stats).unwrap_or_default();
fs::write(&self.state_file, json)
}
@ -560,13 +560,21 @@ impl CrashDetector {
return false;
}
if let Ok(content) = fs::read_to_string(&self.state_file) {
if let Ok(state) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(status) = state.get("status").and_then(|v| v.as_str()) {
if status == "stopped" {
return false;
}
}
if let Some(content) = fs::read_to_string(&self.state_file)
.and_then(|c| {
serde_json::from_str::<serde_json::Value>(&c)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
})
.ok()
.and_then(|state| {
state
.get("status")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
})
{
if content == "stopped" {
return false;
}
}