Skip to content

Commit

Permalink
fix: save cache after bundled removal of session lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanabx committed Apr 27, 2024
1 parent 9cea4a6 commit 54d7d8c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
20 changes: 16 additions & 4 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ impl Daemon {
/// removes all entries and/or icons owned by `owner` for the session lifetime
async fn remove_session_owner(&mut self, owner: String) -> zbus::fdo::Result<()> {
let lifetime = Lifetime::Session(owner);
match self.entry_manager.lock().await.remove_lifetime(lifetime) {
Ok(_) => Ok(()),
let mut entry_lock = self.entry_manager.lock().await;
match entry_lock.remove_lifetime(lifetime) {
Ok(_) => {
if let Err(e) = entry_lock.save_cache() {
return Err(e.into());
}
Ok(())
}
Err(e) => {
log::error!("{:?}", e);
Err(e.into())
Expand All @@ -206,8 +212,14 @@ impl Daemon {
/// removes all entries and/or icons owned by `owner` for the persistent lifetime
async fn remove_persistent_owner(&mut self, owner: String) -> zbus::fdo::Result<()> {
let lifetime = Lifetime::Persistent(owner);
match self.entry_manager.lock().await.remove_lifetime(lifetime) {
Ok(_) => Ok(()),
let mut entry_lock = self.entry_manager.lock().await;
match entry_lock.remove_lifetime(lifetime) {
Ok(_) => {
if let Err(e) = entry_lock.save_cache() {
return Err(e.into());
}
Ok(())
}
Err(e) => {
log::error!("{:?}", e);
Err(e.into())
Expand Down
9 changes: 2 additions & 7 deletions src/entry_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ impl EntryManager {

pub fn remove_lifetime(&mut self, lifetime: Lifetime) -> Result<(), EntryManagerError> {
log::info!("Deleting lifetime {:?}", lifetime);
let mut changed = false;
if let Some(entries) = self.cache.entries.get(&lifetime) {
for entry in entries {
match entry.clone().delete_self() {
Expand All @@ -281,7 +280,6 @@ impl EntryManager {
}
}
self.cache.entries.remove(&lifetime);
changed = true;
}
if let Some(icons) = self.cache.icons.get(&lifetime) {
for icon in icons {
Expand All @@ -293,10 +291,6 @@ impl EntryManager {
}
}
self.cache.icons.remove(&lifetime);
changed = true;
}
if changed {
self.save_cache()?;
}
Ok(())
}
Expand All @@ -319,10 +313,11 @@ impl EntryManager {
{
self.remove_lifetime(lifetime.clone())?;
}
self.save_cache()?;
Ok(())
}

fn save_cache(&self) -> Result<(), EntryManagerError> {
pub fn save_cache(&self) -> Result<(), EntryManagerError> {
let conf_str = ron::ser::to_string_pretty(&self.cache, ron::ser::PrettyConfig::default())?;
fs::write(&self.config_file, conf_str)?;
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ async fn watch_processes(manager: Arc<Mutex<EntryManager>>) -> zbus::Result<()>
if manager_lock.remove_lifetime(Lifetime::Process(x)).is_err() {
log::error!("Something went wrong when removing lifetime with PID {}", x);
}
if manager_lock.save_cache().is_err() {
log::error!("Something went wrong when saving the cache to disk {}", x);
}
}
}
let keys_to_iter = manager_lock
Expand Down

0 comments on commit 54d7d8c

Please sign in to comment.