From ffcb66a8d19bd3c5eafbf7c26bdedba0317adb56 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Tue, 1 Mar 2022 21:06:06 +0100 Subject: [PATCH] more example --- examples/watch_simple.rs | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 examples/watch_simple.rs diff --git a/examples/watch_simple.rs b/examples/watch_simple.rs new file mode 100644 index 00000000..795de7fe --- /dev/null +++ b/examples/watch_simple.rs @@ -0,0 +1,79 @@ +use notify::DebouncedEvent::{Create, Remove, Rename}; +use notify::{watcher, RecursiveMode, Watcher}; +use std::{ + sync::{ + mpsc::{channel, Receiver}, + {Arc, Mutex}, + }, + thread::sleep, + time::Duration, +}; + +use tokio::runtime::Builder; + +async fn watch(receiver: Receiver, stop: Arc>) { + loop { + if *stop.lock().unwrap() { + break; + } + + match receiver.recv() { + Ok(event) => match event { + Create(new_path) => { + println!("Create new file: {:?}", new_path); + } + Remove(old_path) => { + println!("Remove file: {:?}", old_path); + } + Rename(old_path, new_path) => { + println!("Rename file: {:?} to {:?}", old_path, new_path); + } + _ => (), + }, + Err(e) => { + println!("{:?}", e); + } + } + + sleep(Duration::from_secs(1)); + } +} + +fn main() { + let path = "/home/jb/Videos/tv-media/ADtv/01 - Intro".to_string(); + let stop = Arc::new(Mutex::new(false)); + + let (sender, receiver) = channel(); + let mut watcher = watcher(sender, Duration::from_secs(2)).unwrap(); + watcher.watch(path.clone(), RecursiveMode::Recursive).unwrap(); + + let runtime = Builder::new_multi_thread() + .worker_threads(1) + .thread_name("file_watcher") + .enable_all() + .build() + .expect("Creating Tokio runtime"); + + if true { + runtime.spawn(watch(receiver, Arc::clone(&stop))); + } + + let mut count = 0; + + loop { + println!("task: {count}"); + sleep(Duration::from_secs(1)); + + count += 1; + + if count == 5 { + break; + } + } + + *stop.lock().unwrap() = true; + + watcher.unwatch(path).unwrap(); + + println!("after loop"); +}