Suppress or prevent duplicate inotifywait events?

前端 未结 2 1241
臣服心动
臣服心动 2021-01-16 03:13

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,

相关标签:
2条回答
  • 2021-01-16 03:56

    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.

    0 讨论(0)
  • 2021-01-16 03:57

    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.

    0 讨论(0)
提交回复
热议问题