Rust 使用notify示例设置轮询观察程序

Rust 使用notify示例设置轮询观察程序,rust,notify,Rust,Notify,notify文档对于原始事件和非公告事件非常有用。有人提到了一个民意调查观察员的实施,但我似乎无法设置这一点。有没有人能告诉我或者提供一些示例代码?是原始方法还是去毛刺方法 我有一个挂载驱动器,我正试图通过Linux观看,而inotify实例没有接收到任何事件,所以我唯一的选择是尝试轮询 这是我目前的脱发代码 extern crate notify; extern crate weighted_rs; use notify::{Watcher, RecursiveMode, watcher};

notify文档对于原始事件和非公告事件非常有用。有人提到了一个民意调查观察员的实施,但我似乎无法设置这一点。有没有人能告诉我或者提供一些示例代码?是原始方法还是去毛刺方法

我有一个挂载驱动器,我正试图通过Linux观看,而inotify实例没有接收到任何事件,所以我唯一的选择是尝试轮询

这是我目前的脱发代码

extern crate notify;
extern crate weighted_rs;

use notify::{Watcher, RecursiveMode, watcher};
use std::sync::mpsc::channel;
use std::time::Duration;
use std::path::Path;
use std::fs;
use weighted_rs::{SmoothWeight, Weight};

fn main() {
// Create a channel to receive the events.
let (tx, rx) = channel();

// Create a watcher object, delivering debounced events.
// The notification back-end is selected based on the platform.
let mut watcher = watcher(tx, Duration::from_secs(5)).unwrap();

// Add a path to be watched. All files and directories at that path 
and
// below will be monitored for changes.
//watcher.watch("/mnt/jobs_testwatch/", 
RecursiveMode::NonRecursive).unwrap();
watcher.watch("/mnt/jobs/", RecursiveMode::NonRecursive).unwrap();

let mut sw: SmoothWeight<&str> = SmoothWeight::new();
sw.add("w1", 1);
sw.add("w2", 1);
sw.add("w3", 1);

loop {
    match rx.recv() {
        Err(e) => println!("watch error: {:?}", e),
       Ok(event) => {
       println!("{:?}", event);
       match event{
           notify::DebouncedEvent::NoticeWrite(_) => {}
           notify::DebouncedEvent::NoticeRemove(_) => {}
           notify::DebouncedEvent::Write(_) => {},
           notify::DebouncedEvent::Chmod(_) => {},
           notify::DebouncedEvent::Remove(_) => {}
           notify::DebouncedEvent::Rename(_, _) => {}
           notify::DebouncedEvent::Rescan => {}
           notify::DebouncedEvent::Error(_, _) => {},
           notify::DebouncedEvent::Create(x) => {
               println!("{:?}", x);
               let path_gen = Path::new(&x);
               let file_stem = path_gen.file_stem().unwrap();
               let extension = path_gen.extension();
               //println!("filestem = {:?}, extension = {:?}", 
file_stem, extension);
               if extension != None {
                //println!("event = {:?} -- Path = {:?}", op, path);    
                //println!("filename =>{:?}, extension =>{:?}", 
file_stem, extension.unwrap());
                //println!("");
                //let xml_string =  OsStr::new("xml");
                match extension.unwrap().to_str(){
                    Some("xml")=>{
                        println!("found {:?}.", extension.unwrap());
                        let s = sw.next().unwrap();
                        println!("moving to /{} ...", s);
                        let movepath = format! 
        ("/mnt/jobs/lbalancewatchers/{}/{}.xml",s, 
        file_stem.to_str().unwrap());
                        let foo = fs::read_to_string(&x);
                        match foo{
                            Ok(_) => {
                             let a = fs::rename(&x, &movepath);
                             if let Err(e) = a {
                                 println!("error moving file --> 
{}", e)
                             }
                             println!("moved {:?} to {:?}", 
&file_stem, &movepath)},

                             //println!("{:#?}", foo.unwrap())},
                            Err(_) => println!("")
                        }
                             
                        //println!("{:#?}", foo)
                     }
                    _ => {println!("{:?} is NOT an xml" , 
extension)}
                }
            };
            
            
            },
               
            
       }
    }

}
}
外部板条箱通知;
外部板条箱称重;
使用notify::{Watcher,RecursiveMode,Watcher};
使用std::sync::mpsc::channel;
使用std::time::Duration;
使用std::path::path;
使用std::fs;
使用加权参数:{SmoothWeight,Weight};
fn main(){
//创建一个接收事件的通道。
let(tx,rx)=信道();
//创建一个观察者对象,传递取消公告的事件。
//根据平台选择通知后端。
让mut watcher=watcher(tx,Duration::from_secs(5)).unwrap();
//添加要监视的路径。该路径上的所有文件和目录
和
//将监控下面的更改。
//watcher.watch(“/mnt/jobs\u testwatch/”,
递归模式::非递归)。展开();
watcher.watch(“/mnt/jobs/”,RecursiveMode::NonRecursive).unwrap();
让mut sw:SmoothWeight=SmoothWeight::new();
西南添加(“w1”,1);
西南添加(“w2”,1);
软件添加(“w3”,1);
环路{
匹配rx.recv(){
Err(e)=>println!(“监视错误:{:?}”,e),
正常(事件)=>{
println!(“{:?}”,事件);
比赛项目{
notify::DebouncedEvent::NoticeWrite()=>{}
notify::DebouncedEvent::NoticeRemove()=>{}
notify::DebouncedEvent::Write()=>{},
notify::DebouncedEvent::Chmod()=>{},
notify::DebouncedEvent::Remove()=>{}
notify::DebouncedEvent::Rename(,)=>{}
notify::DebouncedEvent::Rescan=>{}
notify::DebouncedEvent::Error(,)=>{},
通知::取消公告事件::创建(x)=>{
println!(“{:?}”,x);
让path_gen=path::new(&x);
让file_stem=path_gen.file_stem().unwrap();
设extension=path_gen.extension();
//println!(“文件项={:?},扩展={:?}”,
文件(系统,扩展名);
如果扩展名为!=无{
//println!(“事件={:?}--Path={:?}”,op,Path);
//println!(“文件名=>{:?},扩展名=>{:?}”,
文件_-stem,扩展名.unwrap());
//println!(“”);
//让xml_string=OsStr::new(“xml”);
将extension.unwrap()与_str()匹配{
一些(“xml”)=>{
println!(“找到{:?}.”,扩展名.unwrap());
设s=sw.next().unwrap();
println!(“移动到/{}…”,s);
让movepath=format!
(“/mnt/jobs/lbalancewatchers/{}/{}.xml”,s,
文件“stem.to”str().unwrap());
让foo=fs::读取字符串(&x);
火柴{
好(41;)=>{
设a=fs::rename(&x,&movepath);
如果let Err(e)=a{
println!(“移动文件时出错-->
{},e)
}
println!(“将{:?}移动到{:?}),
&文件_-stem,&movepath)},
//println!(“{:#?}”,foo.unwrap())},
Err()=>println!(“”)
}
//println!(“{:?}”,foo)
}
_=>{println!(“{:?}不是xml”,
扩展)}
}
};
},
}
}
}
}

}

它是什么类型的底座?“Inotify只报告用户空间程序通过文件系统API触发的事件。因此,它不会捕获网络文件系统上发生的远程事件……此外,各种伪文件系统(如/proc、/sys和/dev/pts)都不能使用Inotify进行监视。Inotify API不报告文件访问和修改这可能是因为mmap(2)、msync(2)和munmap(2)。“--你试过了吗?我们的硬件安装人员。我想这是桑巴舞。真正奇怪的是,notify中的原始观察程序拾取事件,但在windows完成编写之前,它拾取事件的速度太快,导致整个事件出错。失言的守望者一无所获。我想如果原汁原味的得到它们,脱口而出的人会得到,但显然不会。我认为投票选项可能是一个解决办法,但无论我怎么努力,我似乎都无法设置它。抱歉,我对拉斯蒂有些陌生。我还没有尝试过民意调查者……我似乎找不到一个有效的例子。我肯定错过了一些东西,但作为一个生锈的新手,我似乎遇到了麻烦。我已经将整个脚本添加到了原始脚本中,这样你就可以看到我目前的处境。它基本上会提取xml文件,然后将它们负载平衡到所有运行javascript watchers的其他文件夹中