Rendered WaitForTaskCompletion global output thread safe

This commit is contained in:
deajan 2016-12-03 10:40:30 +01:00
parent 3128cb1841
commit a87c44b9dc
2 changed files with 24 additions and 23 deletions

View File

@ -4,7 +4,7 @@ PROGRAM="osync" # Rsync based two way sync engine with fault tolerance
AUTHOR="(C) 2013-2016 by Orsiris de Jong" AUTHOR="(C) 2013-2016 by Orsiris de Jong"
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr" CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
PROGRAM_VERSION=1.2-beta3 PROGRAM_VERSION=1.2-beta3
PROGRAM_BUILD=2016120301 PROGRAM_BUILD=2016120303
IS_STABLE=no IS_STABLE=no
# Execution order #__WITH_PARANOIA_DEBUG # Execution order #__WITH_PARANOIA_DEBUG
@ -614,7 +614,7 @@ function WriteLockFiles {
TARGET_LOCK_FILE_EXISTS=true TARGET_LOCK_FILE_EXISTS=true
WaitForTaskCompletion "$initiatorPid;$targetPid" 720 1800 $SLEEP_TIME $KEEP_LOGGING true true false ${FUNCNAME[0]} WaitForTaskCompletion "$initiatorPid;$targetPid" 720 1800 $SLEEP_TIME $KEEP_LOGGING true true false ${FUNCNAME[0]}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
IFS=';' read -r -a pidArray <<< "$WAIT_FOR_TASK_COMPLETION" IFS=';' read -r -a pidArray <<< "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_${FUNCNAME[0]}\")"
for pid in "${pidArray[@]}"; do for pid in "${pidArray[@]}"; do
pid=${pid%:*} pid=${pid%:*}
if [ "$pid" == "$initiatorPid" ]; then if [ "$pid" == "$initiatorPid" ]; then
@ -1415,7 +1415,7 @@ function Sync {
WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]} WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]}
if [ $? != 0 ]; then if [ $? != 0 ]; then
IFS=';' read -r -a pidArray <<< "$WAIT_FOR_TASK_COMPLETION" IFS=';' read -r -a pidArray <<< "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_${FUNCNAME[0]}\")"
initiatorFail=false initiatorFail=false
targetFail=false targetFail=false
for pid in "${pidArray[@]}"; do for pid in "${pidArray[@]}"; do
@ -1460,7 +1460,7 @@ function Sync {
WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]} WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]}
if [ $? != 0 ]; then if [ $? != 0 ]; then
IFS=';' read -r -a pidArray <<< "$WAIT_FOR_TASK_COMPLETION" IFS=';' read -r -a pidArray <<< "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_${FUNCNAME[0]}\")"
initiatorFail=false initiatorFail=false
targetFail=false targetFail=false
for pid in "${pidArray[@]}"; do for pid in "${pidArray[@]}"; do
@ -1584,7 +1584,7 @@ function Sync {
WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]} WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]}
if [ $? != 0 ]; then if [ $? != 0 ]; then
IFS=';' read -r -a pidArray <<< "$WAIT_FOR_TASK_COMPLETION" IFS=';' read -r -a pidArray <<< "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_${FUNCNAME[0]}\")"
initiatorFail=false initiatorFail=false
targetFail=false targetFail=false
for pid in "${pidArray[@]}"; do for pid in "${pidArray[@]}"; do
@ -1630,7 +1630,7 @@ function Sync {
WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]} WaitForTaskCompletion "$initiatorPid;$targetPid" $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]}
if [ $? != 0 ]; then if [ $? != 0 ]; then
IFS=';' read -r -a pidArray <<< "$WAIT_FOR_TASK_COMPLETION" IFS=';' read -r -a pidArray <<< "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_${FUNCNAME[0]}\")"
initiatorFail=false initiatorFail=false
targetFail=false targetFail=false
for pid in "${pidArray[@]}"; do for pid in "${pidArray[@]}"; do
@ -1781,7 +1781,7 @@ function SoftDelete {
pids="$pids;$!" pids="$pids;$!"
fi fi
WaitForTaskCompletion $pids $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]} WaitForTaskCompletion $pids $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]}
if [ $? != 0 ]; then # WIP Check for env variable as hard max flag that stops the script instead of just screaming for errors if [ $? != 0 ] && [ "$(eval echo \"\$HARD_MAX_EXEC_TIME_REACHED_${FUNCNAME[0]}\")" == true ]; then
exit 1 exit 1
fi fi
fi fi
@ -1799,7 +1799,7 @@ function SoftDelete {
pids="$pids;$!" pids="$pids;$!"
fi fi
WaitForTaskCompletion $pids $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]} WaitForTaskCompletion $pids $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME $SLEEP_TIME $KEEP_LOGGING false true false ${FUNCNAME[0]}
if [ $? != 0 ]; then if [ $? != 0 ] && [ "$(eval echo \"\$HARD_MAX_EXEC_TIME_REACHED_${FUNCNAME[0]}\")" == true ]; then
exit 1 exit 1
fi fi
fi fi

View File

@ -1,6 +1,6 @@
#### MINIMAL-FUNCTION-SET BEGIN #### #### MINIMAL-FUNCTION-SET BEGIN ####
## FUNC_BUILD=2016120302 ## FUNC_BUILD=2016120303
## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## To use in a program, define the following variables: ## To use in a program, define the following variables:
@ -184,7 +184,7 @@ function Logger {
fi fi
elif [ "$level" == "PARANOIA_DEBUG" ]; then #__WITH_PARANOIA_DEBUG elif [ "$level" == "PARANOIA_DEBUG" ]; then #__WITH_PARANOIA_DEBUG
if [ "$_PARANOIA_DEBUG" == "yes" ]; then #__WITH_PARANOIA_DEBUG if [ "$_PARANOIA_DEBUG" == "yes" ]; then #__WITH_PARANOIA_DEBUG
_Logger "$prefix$value" "$prefix$value" #__WITH_PARANOIA_DEBUG _Logger "$prefix$value" "$prefix\e[35m$value\e[0m" #__WITH_PARANOIA_DEBUG
return #__WITH_PARANOIA_DEBUG return #__WITH_PARANOIA_DEBUG
fi #__WITH_PARANOIA_DEBUG fi #__WITH_PARANOIA_DEBUG
else else
@ -569,9 +569,8 @@ function joinString {
} }
# Time control function for background processes, suitable for multiple synchronous processes # Time control function for background processes, suitable for multiple synchronous processes
# Fills a global variable called WAIT_FOR_TASK_COMPLETION that contains list of failed pids in format pid1:result1;pid2:result2 # Fills a global variable called WAIT_FOR_TASK_COMPLETION_$callerName that contains list of failed pids in format pid1:result1;pid2:result2
# Also sets a global variable called HARD_MAX_EXEC_TIME_REACHED to true if hardMaxTime is reached # Also sets a global variable called HARD_MAX_EXEC_TIME_REACHED_$callerName to true if hardMaxTime is reached
# Warning: Don't imbricate this function into another run if you plan to use the global variable output
# Standard wait $! emulation would be WaitForTaskCompletion $! 0 0 1 0 true false true false "${FUNCNAME[0]}" # Standard wait $! emulation would be WaitForTaskCompletion $! 0 0 1 0 true false true false "${FUNCNAME[0]}"
@ -613,8 +612,9 @@ function WaitForTaskCompletion {
IFS=';' read -a pidsArray <<< "$pids" IFS=';' read -a pidsArray <<< "$pids"
pidCount=${#pidsArray[@]} pidCount=${#pidsArray[@]}
WAIT_FOR_TASK_COMPLETION="" # Set global var default
HARD_MAX_EXEC_TIME_REACHED=false eval "WAIT_FOR_TASK_COMPLETION_$callerName=\"\""
eval "HARD_MAX_EXEC_TIME_REACHED_$callerName=false"
while [ ${#pidsArray[@]} -gt 0 ]; do while [ ${#pidsArray[@]} -gt 0 ]; do
newPidsArray=() newPidsArray=()
@ -661,7 +661,7 @@ function WaitForTaskCompletion {
if [ $noErrorLog != true ]; then if [ $noErrorLog != true ]; then
SendAlert true SendAlert true
fi fi
HARD_MAX_EXEC_TIME_REACHED=true eval "HARD_MAX_EXEC_TIME_REACHED_$callerName=true"
return $errorcount return $errorcount
fi fi
@ -680,10 +680,11 @@ function WaitForTaskCompletion {
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
errorcount=$((errorcount+1)) errorcount=$((errorcount+1))
Logger "${FUNCNAME[0]} called by [$callerName] finished monitoring [$pid] with exitcode [$retval]." "DEBUG" Logger "${FUNCNAME[0]} called by [$callerName] finished monitoring [$pid] with exitcode [$retval]." "DEBUG"
if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then # Welcome to variable variable bash hell
WAIT_FOR_TASK_COMPLETION="$pid:$retval" if [ "$(eval echo \"\$WAIT_FOR_TASK_COMPLETION_$callerName\")" == "" ]; then
eval "WAIT_FOR_TASK_COMPLETION_$callerName=\"$pid:$retval\""
else else
WAIT_FOR_TASK_COMPLETION=";$pid:$retval" eval "WAIT_FOR_TASK_COMPLETION_$callerName=\";$pid:$retval\""
fi fi
fi fi
fi fi