diff --git a/src/state.rs b/src/state.rs index 06c02dd..79df30a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -19,13 +19,13 @@ use std::{ error::Error, - sync::{Arc, Mutex} + sync::{Arc, Mutex, MutexGuard} }; use tokio::fs; use chrono::{ DateTime, Utc }; -use tracing::{ info, debug }; +use tracing::{ warn, info, debug }; use serde_yaml::mapping::Mapping as FeedReaderState; @@ -48,6 +48,13 @@ impl FeedReaderStateDb { Ok(db.clone()) } + fn lock_state(&self) -> MutexGuard { + self.state.lock().unwrap_or_else(|e| { + warn!("State db mutex has been poisoned, continuing..."); + e.into_inner() + }) + } + pub async fn set( &self, uri: &str, @@ -56,7 +63,7 @@ impl FeedReaderStateDb { { debug!("Updating feed reader state"); - self.state.lock().unwrap().insert(uri.into(), dt.timestamp().into()); + self.lock_state().insert(uri.into(), dt.timestamp().into()); } self.persist().await.unwrap(); @@ -65,7 +72,7 @@ impl FeedReaderStateDb { #[tracing::instrument(ret, level="debug")] pub fn get(&self, uri: &str) -> Option> { debug!("Retrieving state for feed {}", uri); - match self.state.lock().unwrap().get(uri) { + match self.lock_state().get(uri) { Some(t) => DateTime::from_timestamp((*t).as_i64().unwrap(), 0), None => None }