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,11 +82,13 @@ 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
.watcher() .watcher()
@ -104,15 +106,11 @@ 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, if let Err(e) = manager.reload_config().await {
Some(_) => { error!("Configuration error: {e}");
if let Err(e) = manager.reload_config().await { } else {
error!("Configuration error: {e}"); info!("Sucessfully applied configuration change");
} else {
info!("Sucessfully applied configuration change");
}
}
} }
} }
} }