Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive Modify Event After using notify-debouncer-full #671

Open
wocan23 opened this issue Jan 29, 2025 · 2 comments
Open

Receive Modify Event After using notify-debouncer-full #671

wocan23 opened this issue Jan 29, 2025 · 2 comments

Comments

@wocan23
Copy link

wocan23 commented Jan 29, 2025

My core code as below

    let mut debouncer = new_debouncer(Duration::from_secs(2), None, move |result: DebounceEventResult| {
        match result {
            Ok(events) => events.iter().for_each(|event|{
                match &event.kind {
                    EventKind::Any =>{},
                    EventKind::Create(_)=>{
                        info!("收到创建事件{}",event.paths.first().unwrap().to_str().unwrap());
                    } ,
                    EventKind::Modify(_)=>{
                        info!("收到修改事件{}",event.paths.first().unwrap().to_str().unwrap());
                    } ,

                    EventKind::Remove(_)=>{
                        
                    } ,
                    EventKind::Access(_)=>{},
                    EventKind::Other=>{},
                }

            }),
            Err(errors) => errors.iter().for_each(|err| error!("{err:?}")),
        }
    }).unwrap();

when I create a file , I will reveive modify event;

INFO [file_listener::watch::watch] 收到创建事件E:\test\filelisten\src\fddfd - 副本 (5).xml
INFO [file_listener::watch::watch] 收到修改事件E:\test\filelisten\src\fddfd - 副本 (5).xml

My cargo.toml config as below:

name = "file-listener"
version = "0.1.0"
edition = "2021"

[dependencies]
notify-debouncer-full = "0.5.0"
notify = "8.0.0"
@dfaust
Copy link
Member

dfaust commented Jan 29, 2025

What operating system are you using? And how are you creating the file?

You are receiving a Create and then a Modify event. Looks good to me.

@wocan23
Copy link
Author

wocan23 commented Jan 29, 2025

Operating system version: Windows 10 Professional

I create the file by two ways:
1: Ctrl+C and Ctrl+V on watching the dir.
A file named a.xml on the "E:\test\filelisten\src" ,when I do Ctrl+C/V, will create a new file named a - 副本.xml,I will gain:

    INFO [file_listener] 收到创建事件E:\test\filelisten\src\a - 副本.xml
    INFO [file_listener] 收到修改事件E:\test\filelisten\src\a - 副本.xml

2: Copy file named 'b.xml' from the other dir, I will gain:

INFO [file_listener] 收到创建事件E:\test\filelisten\src\b.xml
INFO [file_listener] 收到修改事件E:\test\filelisten\src\b.xml

I simplify the code, and the whole code as below
main.rs

use std::time::Duration;

use std::path::Path;

use log::{error,info};
use notify_debouncer_full::{notify::*, new_debouncer, DebounceEventResult};
use flexi_logger::{Logger, FileSpec, Criterion, Naming, Cleanup, WriteMode, Duplicate, detailed_format};


pub fn init_log(log_lvl : &str){
    println!("日志级别[{}]",&log_lvl);
    // 初始化日志记录,配置输出到文件,设置文件大小限制和滚动日志
    let _ = Logger::try_with_str(&log_lvl)
    .unwrap()
    .log_to_file(
        FileSpec::default()
            .directory("logs") 
            .basename("file_listener") 
            .suffix("log")     
    )
    .rotate(
        Criterion::Age(flexi_logger::Age::Day), 
        Naming::Timestamps,        
        Cleanup::KeepLogFiles(3),   
    )
    .write_mode(WriteMode::BufferAndFlush) 
    .duplicate_to_stdout(Duplicate::All)  
    .format_for_files(detailed_format)    
    .start()
    .unwrap();
}

pub fn start_watch(){

    let mut debouncer = new_debouncer(Duration::from_secs(2), None, move |result: DebounceEventResult| {
        match result {
            Ok(events) => events.iter().for_each(|event|{
                match &event.kind {
                    EventKind::Any =>{},
                    EventKind::Create(_)=>{
                        info!("收到创建事件{}",event.paths.first().unwrap().to_str().unwrap());
                    } ,
                    EventKind::Modify(_)=>{
                        info!("收到修改事件{}",event.paths.first().unwrap().to_str().unwrap());
                    } ,

                    EventKind::Remove(_)=>{
                        
                    } ,
                    EventKind::Access(_)=>{},
                    EventKind::Other=>{},
                }

            }),
            Err(errors) => errors.iter().for_each(|err| error!("{err:?}")),
        }
    }).unwrap();


    debouncer.watch(Path::new("E:\\test\\filelisten\\src"), RecursiveMode::Recursive).unwrap();


    loop {
        std::thread::sleep(Duration::from_millis(100));
    }
}

fn main() {
    file_listener::log::log::init_log("info");
    // 开启监听
    start_watch();

    loop {
        std::thread::sleep(Duration::from_millis(1000));
    }
}

cargo.toml

[package]
name = "file-listener"
version = "0.1.0"
edition = "2021"

[dependencies]
log = "0.4"
flexi_logger = "0.22"
notify-debouncer-full = "0.5.0"
notify = "8.0.0"

in addition, I use notify-debouncer-mini, don't receive Modify Event, but can't gain event kind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants