mirror of
https://github.com/deajan/obackup.git
synced 2026-02-10 17:10:55 +01:00
Refactor wait functions
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#### MINIMAL-FUNCTION-SET BEGIN ####
|
||||
|
||||
## FUNC_BUILD=2016080601
|
||||
## FUNC_BUILD=2016080701
|
||||
## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
|
||||
|
||||
#TODO: set _LOGGER_PREFIX in other apps, specially for osync daemon mode
|
||||
@@ -569,6 +569,7 @@ function WaitForTaskCompletion {
|
||||
local hard_max_time="${3}" # If program with pid $pid takes longer than $hard_max_time seconds, will stop execution, unless $hard_max_time equals 0.
|
||||
local caller_name="${4}" # Who called this function
|
||||
local exit_on_error="${5:-false}" # Should the function exit on subprocess errors
|
||||
local counting="{6:-true}" # Count time since function launch if true, script launch if false
|
||||
|
||||
Logger "${FUNCNAME[0]} called by [$caller_name]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
||||
__CheckArguments 5 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||
@@ -603,7 +604,12 @@ function WaitForTaskCompletion {
|
||||
done
|
||||
|
||||
Spinner
|
||||
exec_time=$(($SECONDS - $seconds_begin))
|
||||
if [ $counting == true ]; then
|
||||
exec_time=$(($SECONDS - $seconds_begin))
|
||||
else
|
||||
exec_time=$SECONDS
|
||||
fi
|
||||
|
||||
if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then
|
||||
if [ $log_ttime -ne $exec_time ]; then
|
||||
log_ttime=$exec_time
|
||||
@@ -623,10 +629,8 @@ function WaitForTaskCompletion {
|
||||
KillChilds $pid
|
||||
if [ $? == 0 ]; then
|
||||
Logger "Task stopped successfully" "NOTICE"
|
||||
#return 0
|
||||
else
|
||||
errrorcount=$((errorcount+1))
|
||||
#return 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
@@ -644,54 +648,14 @@ function WaitForTaskCompletion {
|
||||
fi
|
||||
}
|
||||
|
||||
function WaitForCompletion {
|
||||
local pid="${1}" # pid to wait for
|
||||
local soft_max_time="${2}" # If program with pid $pid takes longer than $soft_max_time seconds, will log a warning, unless $soft_max_time equals 0.
|
||||
local hard_max_time="${3}" # If program with pid $pid takes longer than $hard_max_time seconds, will stop execution, unless $hard_max_time equals 0.
|
||||
local caller_name="${4}" # Who called this function
|
||||
Logger "${FUNCNAME[0]} called by [$caller_name]" "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
||||
__CheckArguments 4 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||
function CleanUp {
|
||||
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||
|
||||
local soft_alert=0 # Does a soft alert need to be triggered, if yes, send an alert once
|
||||
local log_time=0 # local time instance for comparaison
|
||||
|
||||
local seconds_begin=$SECONDS # Seconds since the beginning of the script
|
||||
local exec_time=0 # Seconds since the beginning of this function
|
||||
|
||||
local retval=0 # return value of monitored pid process
|
||||
|
||||
while eval "$PROCESS_TEST_CMD" > /dev/null
|
||||
do
|
||||
Spinner
|
||||
if [ $((($SECONDS + 1) % $KEEP_LOGGING)) -eq 0 ]; then
|
||||
if [ $log_time -ne $SECONDS ]; then
|
||||
log_time=$SECONDS
|
||||
Logger "Current task still running." "NOTICE"
|
||||
fi
|
||||
fi
|
||||
if [ $SECONDS -gt $soft_max_time ]; then
|
||||
if [ $soft_alert -eq 0 ] && [ $soft_max_time != 0 ]; then
|
||||
Logger "Max soft execution time exceeded for script." "WARN"
|
||||
soft_alert=1
|
||||
SendAlert
|
||||
fi
|
||||
if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then
|
||||
Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR"
|
||||
KillChilds $pid
|
||||
if [ $? == 0 ]; then
|
||||
Logger "Task stopped successfully" "NOTICE"
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
sleep $SLEEP_TIME
|
||||
done
|
||||
wait $pid
|
||||
retval=$?
|
||||
Logger "${FUNCNAME[0]} ended for [$caller_name] with status $retval." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
||||
return $retval
|
||||
if [ "$_DEBUG" != "yes" ]; then
|
||||
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID"
|
||||
# Fix for sed -i requiring backup extension for BSD & Mac (see all sed -i statements)
|
||||
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID.tmp"
|
||||
fi
|
||||
}
|
||||
|
||||
#### MINIMAL-FUNCTION-SET END ####
|
||||
@@ -762,16 +726,6 @@ function urlDecode {
|
||||
printf '%b' "${url_encoded//%/\\x}"
|
||||
}
|
||||
|
||||
function CleanUp {
|
||||
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||
|
||||
if [ "$_DEBUG" != "yes" ]; then
|
||||
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID"
|
||||
# Fix for sed -i requiring backup extension for BSD & Mac (see all sed -i statements)
|
||||
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID.tmp"
|
||||
fi
|
||||
}
|
||||
|
||||
function GetLocalOS {
|
||||
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||
|
||||
@@ -823,19 +777,19 @@ function GetRemoteOS {
|
||||
cmd=$SSH_CMD' "uname -spio" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1'
|
||||
Logger "cmd: $cmd" "DEBUG"
|
||||
eval "$cmd" &
|
||||
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1"
|
||||
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-1" false true
|
||||
retval=$?
|
||||
if [ $retval != 0 ]; then
|
||||
cmd=$SSH_CMD' "uname -v" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1'
|
||||
Logger "cmd: $cmd" "DEBUG"
|
||||
eval "$cmd" &
|
||||
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2"
|
||||
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-2" false true
|
||||
retval=$?
|
||||
if [ $retval != 0 ]; then
|
||||
cmd=$SSH_CMD' "uname" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1'
|
||||
Logger "cmd: $cmd" "DEBUG"
|
||||
eval "$cmd" &
|
||||
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-3"
|
||||
WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-3" false true
|
||||
retval=$?
|
||||
if [ $retval != 0 ]; then
|
||||
Logger "Cannot Get remote OS type." "ERROR"
|
||||
@@ -888,7 +842,7 @@ function RunLocalCommand {
|
||||
|
||||
Logger "Running command [$command] on local host." "NOTICE"
|
||||
eval "$command" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 &
|
||||
WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]}
|
||||
WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]} false true
|
||||
retval=$?
|
||||
if [ $retval -eq 0 ]; then
|
||||
Logger "Command succeded." "NOTICE"
|
||||
@@ -923,7 +877,7 @@ function RunRemoteCommand {
|
||||
cmd=$SSH_CMD' "$command" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID'" 2>&1'
|
||||
Logger "cmd: $cmd" "DEBUG"
|
||||
eval "$cmd" &
|
||||
WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]}
|
||||
WaitForTaskCompletion $! 0 $hard_max_time ${FUNCNAME[0]} false true
|
||||
retval=$?
|
||||
if [ $retval -eq 0 ]; then
|
||||
Logger "Command succeded." "NOTICE"
|
||||
@@ -957,7 +911,7 @@ function RunBeforeHook {
|
||||
pids="$pids;$!"
|
||||
fi
|
||||
if [ "$pids" != "" ]; then
|
||||
WaitForTaskCompletion $pids 0 0 ${FUNCNAME[0]} false
|
||||
WaitForTaskCompletion $pids 0 0 ${FUNCNAME[0]} false true
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -976,7 +930,7 @@ function RunAfterHook {
|
||||
pids="$pids;$!"
|
||||
fi
|
||||
if [ "$pids" != "" ]; then
|
||||
WaitForTaskCompletion $pids 0 0 ${FUNCNAME[0]} false
|
||||
WaitForTaskCompletion $pids 0 0 ${FUNCNAME[0]} false true
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -987,7 +941,7 @@ function CheckConnectivityRemoteHost {
|
||||
|
||||
if [ "$REMOTE_HOST_PING" != "no" ] && [ "$REMOTE_OPERATION" != "no" ]; then
|
||||
eval "$PING_CMD $REMOTE_HOST > /dev/null 2>&1" &
|
||||
WaitForTaskCompletion $! 180 180 ${FUNCNAME[0]}
|
||||
WaitForTaskCompletion $! 180 180 ${FUNCNAME[0]} false true
|
||||
if [ $? != 0 ]; then
|
||||
Logger "Cannot ping $REMOTE_HOST" "ERROR"
|
||||
return 1
|
||||
@@ -1008,7 +962,7 @@ function CheckConnectivity3rdPartyHosts {
|
||||
for i in $REMOTE_3RD_PARTY_HOSTS
|
||||
do
|
||||
eval "$PING_CMD $i > /dev/null 2>&1" &
|
||||
WaitForTaskCompletion $! 360 360 ${FUNCNAME[0]}
|
||||
WaitForTaskCompletion $! 360 360 ${FUNCNAME[0]} false true
|
||||
if [ $? != 0 ]; then
|
||||
Logger "Cannot ping 3rd party host $i" "NOTICE"
|
||||
else
|
||||
@@ -1266,14 +1220,9 @@ function InitLocalOSSettings {
|
||||
## Ping command is not the same
|
||||
if [ "$LOCAL_OS" == "msys" ]; then
|
||||
FIND_CMD=$(dirname $BASH)/find
|
||||
# PROCESS_TEST_CMD assumes there is a variable $pid
|
||||
# Tested on MSYS and cygwin
|
||||
PROCESS_TEST_CMD='ps -a | awk "{\$1=\$1}\$1" | awk "{print \$1}" | grep $pid'
|
||||
PING_CMD='$SYSTEMROOT\system32\ping -n 2'
|
||||
else
|
||||
FIND_CMD=find
|
||||
# PROCESS_TEST_CMD assumes there is a variable $pid
|
||||
PROCESS_TEST_CMD='ps -p$pid'
|
||||
PING_CMD="ping -c 2 -i .2"
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user