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

can fsnotify watch file change for more than one time? #94

Closed
Jam-Lin opened this issue Sep 8, 2015 · 10 comments · Fixed by #496
Closed

can fsnotify watch file change for more than one time? #94

Jam-Lin opened this issue Sep 8, 2015 · 10 comments · Fixed by #496

Comments

@Jam-Lin
Copy link

Jam-Lin commented Sep 8, 2015

i find fsnotify can only watch file change for one time. when i change the file twice. the watcher.Event chan is close, so i can not get the change event.

@nathany
Copy link
Contributor

nathany commented Oct 13, 2015

If you are watching an individual file and it moves, it may no longer be being watched. Better to watch the directory the file is in.

If you believe there is a bug, can you please provide more details, such as operating system you are using and a code snippet to reproduce the issue? Thanks.

(sorry for the late reply)

@kkirsche
Copy link

kkirsche commented Oct 4, 2016

I'm experiencing similar behavior to this:

EDIT: well...removing that token...

~/g/g/s/g/k/notify-git ❯❯❯ notify-git -v -p value
DEBU[0000] Git repository path: /Users/user/git/test
DEBU[0000] File to be monitored: rr1.txt
DEBU[0000] Waiting in goroutine...
DEBU[0005] Event received.                               event="/Users/user/git/test/rr1.txt": RENAME
DEBU[0005] Message sent to action

I'd expect the goroutine to receive a second event:

go func(commitNeeded chan time.Time) {
        for {
            logrus.Debugln("Waiting in goroutine...")
            select {
            case event := <-watcher.Events:
                logrus.WithField("event", event).Debugln("Event received.")
                changedTime := time.Now()
                commitNeeded <- changedTime
                logrus.Debugln("Message sent")
            case caseErr := <-watcher.Errors:
                logrus.WithError(caseErr).Errorln("Received error from file watcher.")
            }
        }
    }(commitNeeded)

@nathany
Copy link
Contributor

nathany commented Oct 4, 2016

@kkirsche sorry, I'm not sure I follow what's happening in your example.

moving/renaming doesn't work so well with the underlying OS notification systems #26

btw, which operating system are you testing on?

@kkirsche
Copy link

kkirsche commented Oct 4, 2016

@nathany

OS: OS X El Capitan
Moving / Renaming seems to be the only type of event I can get to trigger. Vim triggers it, textedit triggers it. Is there anyway to actually hook into more "normal" use cases to watch the file?

@kkirsche
Copy link

kkirsche commented Oct 4, 2016

Hm, apparently it was cause I was triggering off of any event rather than just watches. I got it fixed with the example_test.go code, but it wasn't clear.

@nathany
Copy link
Contributor

nathany commented Oct 4, 2016

@kkirsche Glad you got it working. Do you have any suggestions for improving the example code or documentation?

@kkirsche
Copy link

kkirsche commented Oct 4, 2016

For me, the biggest issue was that I'm running this as a longer running daemon, so cleanup was something that was a problem. It seemed as though I was leaving the watcher active (gocode process on OS X) which needed to have the following:

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
    for _ = range c {
        fmt.Println("")
        logrus.Infoln("Cleaning up...")
        doneWatch <- true
        done <- true
    }
}()

This then sent the messages to close the watcher and close any open channels. Once I added that, I didn't run into any more gocode running after ctrl+c ended the application running.

The second thing for me which was unclear from the example was what the purpose of the done channel was since it was a bit too simplistic for me to really be able to use as a template to really base my code on. I made a more generic version to show what I was using the watch event for if it helps understand the situation.

https://github.com/kkirsche/notify-git/blob/master/cmd/root.go

Lastly, I had to exit the goroutine on the above done event, or else the watcher would catch a ton (near infinite it seemed) errors / "" events. For this reason I'd have the done channel also exit the background goroutine in case they do any debug messaging to avoid flooding stdout.

I guess my takeaways were that it would be nice to include cleanup code in the example, and provide a more "complete" implementation that leverages the done channel.

Potentially it'd also be helpful to have a wiki page of known programs that don't trigger write events to avoid the "hm, why doesn't vim editing the file work?"

Does that help?

@nathany nathany added the doc label Oct 4, 2016
@tscott0
Copy link

tscott0 commented Mar 17, 2017

Potentially it'd also be helpful to have a wiki page of known programs that don't trigger write events to avoid the "hm, why doesn't vim editing the file work?"

@nathany, I was also using vim to test and it wasn't working. I would instead see RENAME, CHMOD, REMOVE events. It would be great to perhaps have a list of known applications that perhaps don't interract with fsnotify as you might expect, this could become a FAQ page or similar.

Examples of how you might trigger specific events might be useful. While these may seem obvious and would be platform-specific, it would be great to show what happens when you chmod, touch, append and delete (rm) a file from the command line.

@Shashankft9
Copy link

Hey I am also facing the same issue, if anyone could help with any resolutions for this?
@kkirsche can you please highlight in your example, which part helped you in receiving the second event as well? Is this somehow related to the cleanup you are doing?

@ArpithaRao
Copy link

ArpithaRao commented Sep 14, 2021

Hello, with fsnotify new watcher for a single file, the event is triggered only when the file is copied from elsewhere to the interested directory with WRITE mode, for file thats moved with CREATE mode and edited its not working in either modes. Is this expected? Are there any resolutions for these?

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

Successfully merging a pull request may close this issue.

7 participants