forked from kemitix/git-next
66 lines
2.1 KiB
Rust
66 lines
2.1 KiB
Rust
//
|
|
use actix::prelude::*;
|
|
|
|
use actix::Recipient;
|
|
use anyhow::{Context, Result};
|
|
use notify::event::ModifyKind;
|
|
use notify::Watcher;
|
|
use tracing::error;
|
|
use tracing::info;
|
|
|
|
use std::path::PathBuf;
|
|
use std::sync::atomic::AtomicBool;
|
|
use std::sync::atomic::Ordering;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
#[derive(Debug, Message)]
|
|
#[rtype(result = "()")]
|
|
pub struct FileUpdated;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("io")]
|
|
Io(#[from] std::io::Error),
|
|
}
|
|
pub fn watch_file(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Arc<AtomicBool>> {
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
let shutdown = Arc::new(AtomicBool::default());
|
|
|
|
let mut handler = notify::recommended_watcher(tx).context("file watcher")?;
|
|
handler
|
|
.watch(&path, notify::RecursiveMode::NonRecursive)
|
|
.with_context(|| format!("Watch file: {path:?}"))?;
|
|
info!("Watching: {:?}", path);
|
|
let thread_shutdown = shutdown.clone();
|
|
actix_rt::task::spawn_blocking(move || {
|
|
loop {
|
|
if thread_shutdown.load(Ordering::Relaxed) {
|
|
drop(handler);
|
|
break;
|
|
}
|
|
for result in rx.try_iter() {
|
|
match result {
|
|
Ok(event) => match event.kind {
|
|
notify::EventKind::Modify(ModifyKind::Data(_)) => {
|
|
tracing::info!("File modified");
|
|
recipient.do_send(FileUpdated);
|
|
break;
|
|
}
|
|
notify::EventKind::Modify(_)
|
|
| notify::EventKind::Create(_)
|
|
| notify::EventKind::Remove(_)
|
|
| notify::EventKind::Any
|
|
| notify::EventKind::Access(_)
|
|
| notify::EventKind::Other => { /* do nothing */ }
|
|
},
|
|
Err(err) => {
|
|
error!(?err, "Watching file: {path:?}");
|
|
}
|
|
}
|
|
}
|
|
std::thread::sleep(Duration::from_millis(1000));
|
|
}
|
|
});
|
|
Ok(shutdown)
|
|
}
|