Simplified batch runner, also less error prone

This commit is contained in:
deajan 2016-12-01 23:14:22 +01:00
parent 6f94429f52
commit 3f278173c1
1 changed files with 31 additions and 44 deletions

View File

@ -3,7 +3,7 @@ SUBPROGRAM=[prgname]
PROGRAM="$SUBPROGRAM-batch" # Batch program to run osync / obackup instances sequentially and rerun failed ones PROGRAM="$SUBPROGRAM-batch" # Batch program to run osync / obackup instances sequentially and rerun failed ones
AUTHOR="(L) 2013-2016 by Orsiris de Jong" AUTHOR="(L) 2013-2016 by Orsiris de Jong"
CONTACT="http://www.netpower.fr - ozy@netpower.fr" CONTACT="http://www.netpower.fr - ozy@netpower.fr"
PROGRAM_BUILD=2016120101 PROGRAM_BUILD=2016120102
## Runs an osync /obackup instance for every conf file found ## Runs an osync /obackup instance for every conf file found
## If an instance fails, run it again if time permits ## If an instance fails, run it again if time permits
@ -66,6 +66,7 @@ function CheckEnvironment {
SUBPROGRAM_EXECUTABLE=/usr/local/bin/$SUBPROGRAM.sh SUBPROGRAM_EXECUTABLE=/usr/local/bin/$SUBPROGRAM.sh
else else
Logger "Could not find [/usr/local/bin/$SUBPROGRAM.sh]" "CRITICAL" Logger "Could not find [/usr/local/bin/$SUBPROGRAM.sh]" "CRITICAL"
( >&2 echo "Could not find [/usr/local/bin/$SUBPROGRAM.sh]" )
exit 1 exit 1
fi fi
else else
@ -85,56 +86,42 @@ function Batch {
local confFile local confFile
local result local result
## Check for CONF_FILE_PATH local i
if [ -d "$CONF_FILE_PATH" ]; then
## Get list of .conf files
for confFile in $CONF_FILE_PATH/*.conf
do
if [ -f "$confFile" ]; then
if [ "$runList" == "" ]; then
runList="$confFile"
else
runList=$runList" $confFile"
fi
fi
done
elif [ -f "$CONF_FILE_PATH" ] && [ "${CONF_FILE_PATH##*.}" == "conf" ]; then
runList="$CONF_FILE_PATH"
fi
if [ "$runList" == "" ]; then # Using -e because find will accept directories or files
if [ ! -e "$CONF_FILE_PATH" ]; then
Logger "Cannot find conf file path [$CONF_FILE_PATH]." "CRITICAL" Logger "Cannot find conf file path [$CONF_FILE_PATH]." "CRITICAL"
Usage Usage
fi else
# Ugly hack to read files into an array while preserving special characters
runList=()
while IFS= read -d $'\0' -r file; do runList+=("$file"); done < <(find "$CONF_FILE_PATH" -iname "*.conf" -print0)
while ([ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ]) && [ "$runList" != "" ] && [ $runs -le $MAX_RUNS ] while ([ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ]) && [ "${#runList[@]}" -gt 0 ] && [ $runs -le $MAX_RUNS ]; do
do runAgainList=()
Logger "Sequential run n°$runs of $SUBPROGRAM instances for:" "NOTICE" Logger "Sequential run n°$runs of $SUBPROGRAM instances for:" "NOTICE"
Logger "$runList" "NOTICE" for confFile in "${runList[@]}"; do
for confFile in $runList Logger "$confFile" "NOTICE"
do done
for confFile in "${runList[@]}"; do
$SUBPROGRAM_EXECUTABLE "$confFile" --silent $opts & $SUBPROGRAM_EXECUTABLE "$confFile" --silent $opts &
wait $! wait $!
result=$? result=$?
if [ $result != 0 ]; then if [ $result != 0 ]; then
if [ $result == 1 ] || [ $result == 128 ]; then # Do not handle exit code 127 because it is already handled here if [ $result == 1 ] || [ $result == 128 ]; then # Do not handle exit code 128 because it is already handled here
Logger "Run instance $(basename $confFile) failed with exit code [$result]." "ERROR" Logger "Instance $(basename $confFile) failed with exit code [$result]." "ERROR"
if [ "$runAgainList" == "" ]; then runAgainList+=("$confFile")
runAgainList="$confFile"
else
runAgainList=$runAgainList" $confFile"
fi
elif [ $result == 2 ]; then elif [ $result == 2 ]; then
Logger "Run instance $(basename $confFile) finished with warnings." "WARN" Logger "Instance $(basename $confFile) finished with warnings." "WARN"
fi fi
else else
Logger "Run instance $(basename $confFile) succeed." "NOTICE" Logger "Instance $(basename $confFile) succeed." "NOTICE"
fi fi
done done
runList="$runAgainList" runList=("${runAgainList[@]}")
runAgainList=""
runs=$(($runs + 1)) runs=$(($runs + 1))
done done
fi
} }
function Usage { function Usage {