Add a poll option to the consumedir script

This was available for docker only via a special entrypoint and is now
integrated into the script.
This commit is contained in:
eikek 2021-05-31 00:07:28 +02:00
parent b122d9eab0
commit d7363b9c8e

View File

@ -22,7 +22,7 @@ if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
fi fi
OPTIONS=omhdp:vrmi OPTIONS=omhdp:vrmi
LONGOPTS=once,distinct,help,delete,path:,verbose,recursive,dry,integration,iuser:,iheader: LONGOPTS=once,distinct,help,delete,path:,verbose,recursive,dry,integration,iuser:,iheader:,poll:
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
@ -36,7 +36,7 @@ eval set -- "$PARSED"
declare -a watchdir declare -a watchdir
help=n verbose=n delete=n once=n distinct=n recursive=n dryrun=n help=n verbose=n delete=n once=n distinct=n recursive=n dryrun=n
integration=n iuser="" iheader="" integration=n iuser="" iheader="" poll=""
while true; do while true; do
case "$1" in case "$1" in
-h|--help) -h|--help)
@ -84,6 +84,10 @@ while true; do
iheader="$2" iheader="$2"
shift 2 shift 2
;; ;;
--poll)
poll="$2"
shift 2
;;
--) --)
shift shift
break break
@ -108,6 +112,8 @@ showUsage() {
echo " -h | --help Prints this help text. (value: $help)" echo " -h | --help Prints this help text. (value: $help)"
echo " -m | --distinct Optional. Upload only if the file doesn't already exist. (value: $distinct)" echo " -m | --distinct Optional. Upload only if the file doesn't already exist. (value: $distinct)"
echo " -o | --once Instead of watching, upload all files in that dir. (value: $once)" echo " -o | --once Instead of watching, upload all files in that dir. (value: $once)"
echo " --poll <sec> Run the script periodically instead of watching a directory. This can be"
echo " used if watching via inotify is not possible."
echo " -r | --recursive Traverse the directory(ies) recursively (value: $recursive)" echo " -r | --recursive Traverse the directory(ies) recursively (value: $recursive)"
echo " -i | --integration Upload to the integration endpoint. It implies -r. This puts the script in" echo " -i | --integration Upload to the integration endpoint. It implies -r. This puts the script in"
echo " a different mode, where the first subdirectory of any given starting point" echo " a different mode, where the first subdirectory of any given starting point"
@ -355,11 +361,7 @@ checkSetup() {
done done
} }
runOnce() {
# warn if something seems not correctly configured
checkSetup
if [ "$once" = "y" ]; then
info "Uploading all files (except hidden) in '$watchdir'." info "Uploading all files (except hidden) in '$watchdir'."
MD="-maxdepth 1" MD="-maxdepth 1"
if [ "$recursive" = "y" ]; then if [ "$recursive" = "y" ]; then
@ -370,20 +372,37 @@ if [ "$once" = "y" ]; then
process "$file" "$dir" process "$file" "$dir"
done done
done done
}
# warn if something seems not correctly configured
checkSetup
if [ "$once" = "y" ]; then
runOnce
else else
REC="" REC=""
if [ "$recursive" = "y" ]; then if [ "$recursive" = "y" ]; then
REC="-r" REC="-r"
fi fi
$INOTIFY_CMD $REC -m --format '%w%f' -e close_write -e moved_to "${watchdir[@]}" | if [ -z "$poll" ]; then
while read pathfile; do $INOTIFY_CMD $REC -m --format '%w%f' -e close_write -e moved_to "${watchdir[@]}" |
if [[ "$(basename "$pathfile")" != .* ]]; then while read pathfile; do
dir=$(findDir "$pathfile") if [[ "$(basename "$pathfile")" != .* ]]; then
trace "The file '$pathfile' appeared below '$dir'" dir=$(findDir "$pathfile")
sleep 1 trace "The file '$pathfile' appeared below '$dir'"
process "$(realpath "$pathfile")" "$dir" sleep 1
else process "$(realpath "$pathfile")" "$dir"
trace "Skip hidden file $(realpath "$pathfile")" else
fi trace "Skip hidden file $(realpath "$pathfile")"
fi
done
else
echo "Running in polling mode: ${poll}s"
while [ : ]
do
runOnce
sleep $poll
done done
fi
fi fi