Refactor code and trigger on more changes

This commit is contained in:
David Coppit
2015-06-24 06:40:20 -04:00
parent 892e9574ec
commit 06b0533ff0

View File

@@ -3,16 +3,66 @@
CONFIG_FILE=$1 CONFIG_FILE=$1
NAME=$(basename $CONFIG_FILE .conf) NAME=$(basename $CONFIG_FILE .conf)
#-----------------------------------------------------------------------------------------------------------------------
function ts { function ts {
echo [`date '+%b %d %X' $NAME: `] echo [`date '+%b %d %X'`] $NAME:
} }
echo "$(ts) Starting monitor for $CONFIG_FILE" #-----------------------------------------------------------------------------------------------------------------------
tr -d '\r' < $CONFIG_FILE > /tmp/$NAME.conf function is_change_event {
EVENT="$1"
FILE="$2"
. /tmp/$NAME.conf # echo "EVENT=$EVENT"
# echo "FILE=$FILE"
# File events
if [ "$EVENT" == "ATTRIB" ]
then
echo "$(ts) Detected file attribute change: $FILE"
elif [ "$EVENT" == "CLOSE_WRITE,CLOSE" ]
then
EVENT=CLOSE_WRITE
echo "$(ts) Detected new file: $FILE"
elif [ "$EVENT" == "MOVED_TO" ]
then
echo "$(ts) Detected file moved into dir: $FILE"
elif [ "$EVENT" == "MOVED_FROM" ]
then
echo "$(ts) Detected file moved out of dir: $FILE"
elif [ "$EVENT" == "DELETE" ]
then
echo "$(ts) Detected deleted file: $FILE"
# Directory events
elif [ "$EVENT" == "ATTRIB,ISDIR" ]
then
echo "$(ts) Detected dir attribute change: $FILE"
elif [ "$EVENT" == "CREATE,ISDIR" ]
then
echo "$(ts) Detected new dir: $FILE"
elif [ "$EVENT" == "MOVED_TO,IS_DIR" ]
then
echo "$(ts) Detected dir moved into dir: $FILE"
elif [ "$EVENT" == "MOVED_FROM,IS_DIR" ]
then
echo "$(ts) Detected dir moved out of dir: $FILE"
elif [ "$EVENT" == "DELETE,ISDIR" ]
then
echo "$(ts) Detected deleted dir: $FILE"
else
return 1
fi
return 0
}
#-----------------------------------------------------------------------------------------------------------------------
function check_config {
if [[ ! -d "$WATCH_DIR" ]]; then if [[ ! -d "$WATCH_DIR" ]]; then
echo "$(ts) WATCH_DIR specified in $CONFIG_FILE must be a directory." echo "$(ts) WATCH_DIR specified in $CONFIG_FILE must be a directory."
exit 1 exit 1
@@ -37,8 +87,11 @@ if [ -z "$COMMAND" ]; then
echo "$(ts) COMMAND must be defined in $CONFIG_FILE" echo "$(ts) COMMAND must be defined in $CONFIG_FILE"
exit 1 exit 1
fi fi
}
to_seconds () { #-----------------------------------------------------------------------------------------------------------------------
function to_seconds {
readarray elements < <(echo $1 | sed 's/:/\n/g' | tac) readarray elements < <(echo $1 | sed 's/:/\n/g' | tac)
SECONDS=0 SECONDS=0
@@ -52,43 +105,9 @@ to_seconds () {
echo "$SECONDS" echo "$SECONDS"
} }
SETTLE_DURATION=$(to_seconds $SETTLE_DURATION) #-----------------------------------------------------------------------------------------------------------------------
MAX_WAIT_TIME=$(to_seconds $MAX_WAIT_TIME)
MIN_PERIOD=$(to_seconds $MIN_PERIOD)
pipe=$(mktemp -u) function wait_for_events_to_stabilize {
mkfifo $pipe
echo "$(ts) Waiting for changes..."
inotifywait -m -q --format '%e %f' /media >$pipe &
last_run_time=0
while true
do
if read RECORD
then
EVENT=$(echo "$RECORD" | cut -d' ' -f 1)
FILE=$(echo "$RECORD" | cut -d' ' -f 2-)
# echo "$RECORD"
# echo " EVENT=$EVENT"
# echo " FILE=$FILE"
if [ "$EVENT" == "CREATE,ISDIR" ]
then
echo "$(ts) Detected new directory: $FILE"
elif [ "$EVENT" == "CLOSE_WRITE,CLOSE" ]
then
echo "$(ts) Detected new file: $FILE"
elif [ "$EVENT" == "MOVED_TO" ]
then
echo "$(ts) Detected moved file: $FILE"
else
continue
fi
# Monster up as many events as possible, until we hit the either the settle duration, or the max wait threshold.
start_time=$(date +"%s") start_time=$(date +"%s")
while true while true
@@ -107,6 +126,12 @@ do
break break
fi fi
done done
}
#-----------------------------------------------------------------------------------------------------------------------
function wait_for_minimum_period {
last_run_time=$1
time_since_last_run=$(($(date +"%s")-$last_run_time)) time_since_last_run=$(($(date +"%s")-$last_run_time))
if [ $time_since_last_run -lt $MIN_PERIOD ] if [ $time_since_last_run -lt $MIN_PERIOD ]
@@ -125,6 +150,47 @@ do
time_since_last_run=$(($(date +"%s")-$last_run_time)) time_since_last_run=$(($(date +"%s")-$last_run_time))
done done
}
#-----------------------------------------------------------------------------------------------------------------------
echo "$(ts) Starting monitor for $CONFIG_FILE"
tr -d '\r' < $CONFIG_FILE > /tmp/$NAME.conf
. /tmp/$NAME.conf
check_config
SETTLE_DURATION=$(to_seconds $SETTLE_DURATION)
MAX_WAIT_TIME=$(to_seconds $MAX_WAIT_TIME)
MIN_PERIOD=$(to_seconds $MIN_PERIOD)
pipe=$(mktemp -u)
mkfifo $pipe
echo "$(ts) Waiting for changes to $WATCH_DIR..."
inotifywait -m -q --format '%e %f' $WATCH_DIR >$pipe &
last_run_time=0
while true
do
if read RECORD
then
EVENT=$(echo "$RECORD" | cut -d' ' -f 1)
FILE=$(echo "$RECORD" | cut -d' ' -f 2-)
if ! is_change_event "$EVENT" "$FILE"
then
continue
fi
# Monster up as many events as possible, until we hit the either the settle duration, or the max wait threshold.
wait_for_events_to_stabilize
# Wait until it's okay to run the command again, monstering up events as we do so
wait_for_minimum_period $last_run_time
echo "$(ts) Running command" echo "$(ts) Running command"
$COMMAND $COMMAND