Suppress or prevent duplicate inotifywait events?

纵饮孤独 提交于 2019-12-01 12:37:37

问题


Currently inotifywait is watching git server folders. End it emits only when specific file modified. The problem is, when changes are pushed to git server, inotifywait triggers few times. I don’t know why.

So how can I do the next: prevent inotifywait from making duplicates?

I was thinking about the algorithm: when triggered first time->sleep script so it won’t scan next changes for 5 seconds->resume script. But it sounds idiotic... Can you help me achieving this? Thanks!!


回答1:


As I mentioned in your other question, you can setup first a post-receive hook which would checkout the repo for you whenever there is a push done to the Git server.

Not only can you test your inotify function when monitoring those files changed on checkout, but you could even consider not using inotify at all, and using the hook to trigger your notification.
A post-receive hook can list files, and you can then trigger your notification only for certain files.




回答2:


Here is how I solved it for my own needs. I am monitoring a path so that it will auto-compile for syntax errors. And I was bothered with the duplicate events emitted by inotifywait. Add the syncheck function into your .bashrc:

syncheck() {
  declare -A fileMap
  inotifywait --exclude .swp --format '%w%f' -e modify,create -m -r -q $* | \
  while read FILE; do
    es=$(date +'%s')
    case "$FILE" in
    *.hs)
    if [[ ${fileMap[$FILE]} != $es ]];then
        sdiff=$((es-${fileMap[$FILE]:-0}))
        fileMap[$FILE]=$es
        ((sdiff < 3)) && continue
        ghc -fno-code $FILE
        echo "---------------------------------------------"
    fi
    ;;
    esac
  done
}

How to use:

cd ~/src/proj/
. ~/.bashrc
syncheck .

In another terminal, modify or create a Haskell file in ~/src/proj/ location. The syncheck while-loop will detect this and auto-compile for syntax errors.

The key idea to this duplicate events suppression solution is Unix`s epoch seconds and bash dictionary.



来源:https://stackoverflow.com/questions/45265750/suppress-or-prevent-duplicate-inotifywait-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!