Closed gneworld closed 2 years ago
Hey @gneworld!
inotify-rs is only a thin layer on top of inotify, so the most likely explanation is that inotify is emitting multiple events, and there's little we can do about it.
I found this Stack Overflow answer: https://stackoverflow.com/questions/32377517/inotify-event-in-modify-occurring-twice-for-tftp-put/32424150#32424150
Maybe WatchMask::CLOSE_WRITE
is more suitable for your use case?
@hannobraun Thanks for your reply, while after change WatchMask::MODIFY to WatchMask::CLOSE_WRITE,there is nothing triggered when echo something into /tmp/file ...
Could you post your updated code?
@hannobraun Sorry, I made a mistake with a typo, after correct it, it works fine. But as said in Stack Overflow answer, ''' many logs and similar files must be monitored using IN_MODIFY - in such cases where these files are permanently open and thus no IN_CLOSE_WRITE can be emitted. ''' how to fix the issue that one write trigger twice events report under this log file case
I actually don't know under which exact circumstances multiple IN_MODIFY
events can be generated, but I would assume that it is outside of our control and we can't really do anything about that. How you react to it depends on the needs of your application.
If reacting to the event is cheap (for example, if you just want to store the time the log was last modified in a variable somewhere), then I don't think you need to do anything about it. Just handle each event. If handling the event is expensive, then you could deduplicate events by waiting for a short time before handling one (let's say 50 ms, or a whole second, whatever works for you), and then handle all IN_MODIFY
events that arrived during that time all at once.
I just watch a file's modification event in /tmp path as above sample code, when I execute "echo xxxx > /tmp/file" with only once, I randomly got two same modified events reported, In fact, I want to launch my app when specified file was modified, so two app processes launched because this issue
Then remember whether you already launched the app, and don't do it again?
So I should use the singleton pattern to design my app, thanks very much
I didn't say that. I didn't even suggest it. You can design your app however you want.
I'm going to close this issue now, since this doesn't seem to be an issue in inotify-rs.
NA