Replaced channel with notifu

This commit is contained in:
Antoine Gersant 2024-10-12 16:08:27 -07:00
parent f955eb75c5
commit 8100dfceae

View file

@ -8,7 +8,7 @@ use log::{error, info};
use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_full::{Debouncer, FileIdMap}; use notify_debouncer_full::{Debouncer, FileIdMap};
use regex::Regex; use regex::Regex;
use tokio::sync::{futures::Notified, mpsc::unbounded_channel, Notify, RwLock}; use tokio::sync::{futures::Notified, Notify, RwLock};
use crate::app::Error; use crate::app::Error;
@ -82,10 +82,12 @@ impl Manager {
pub async fn new(config_file_path: &Path, auth_secret: auth::Secret) -> Result<Self, Error> { pub async fn new(config_file_path: &Path, auth_secret: auth::Secret) -> Result<Self, Error> {
tokio::fs::File::create_new(config_file_path).await.ok(); tokio::fs::File::create_new(config_file_path).await.ok();
let (sender, mut receiver) = unbounded_channel::<()>(); let notify = Arc::new(Notify::new());
let mut debouncer = let mut debouncer = notify_debouncer_full::new_debouncer(Duration::from_secs(1), None, {
notify_debouncer_full::new_debouncer(Duration::from_secs(1), None, move |_| { let notify = notify.clone();
sender.send(()).ok(); move |_| {
notify.notify_waiters();
}
})?; })?;
debouncer debouncer
@ -104,9 +106,7 @@ impl Manager {
let manager = manager.clone(); let manager = manager.clone();
async move { async move {
loop { loop {
match receiver.recv().await { notify.notified().await;
None => break,
Some(_) => {
if let Err(e) = manager.reload_config().await { if let Err(e) = manager.reload_config().await {
error!("Configuration error: {e}"); error!("Configuration error: {e}");
} else { } else {
@ -114,8 +114,6 @@ impl Manager {
} }
} }
} }
}
}
}); });
manager.reload_config().await?; manager.reload_config().await?;