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

Modify() gets triggered only once #670

Open
thegeneralist01 opened this issue Jan 26, 2025 · 2 comments
Open

Modify() gets triggered only once #670

thegeneralist01 opened this issue Jan 26, 2025 · 2 comments

Comments

@thegeneralist01
Copy link

Once a file (only a single file is being watched) gets modified, the Modify event gets triggered. Any consecutive modifications of the watched file do not result in the Modify event getting triggered. Not sure whether this is an issue with my code or not – help will be appreciated. Here is the code:

let (tx, rx) = mpsc::channel::<notify::Result<notify::Event>>();

let mut watcher =
    match notify::RecommendedWatcher::new(tx.clone(), notify::Config::default()) {
        Ok(w) => w,
        Err(e) => {
            eprintln!("Failed to create watcher: {:?}", e);
            return;
        }
    };

if let Err(e) = watcher.watch(
    Path::new(&config_path.clone()),
    notify::RecursiveMode::Recursive,
) {
    eprintln!("Failed to watch config file: {:?}", e);
    return;
}

// Block forever, printing out events as they come in
for res in rx {
    if let Err(e) = res {
        println!("watch error: {:?}", e);
        continue;
    }

    let event = res.unwrap();
    println!("event: {:?}", event);

    match event.kind {
        notify::EventKind::Modify(_) => {
            // Refresh config
            let config_file = get_file(&config_path);
            let mut p = Parser::new(config_file);
            let cfg = p.parse();
            if let Ok(cfg) = cfg {
                let mut config_guard = config.lock().unwrap();
                *config_guard = cfg;
                println!("Config updated: {:#?}", config_guard);
            }
        }
        _ => {}
    }
}
@dfaust
Copy link
Member

dfaust commented Jan 29, 2025

What operating system are you using?

@thegeneralist01
Copy link
Author

@dfaust Linux, the distro is Mint

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