3
0
mirror of https://github.com/rvalitov/zabbix-php-fpm.git synced 2023-11-05 03:30:27 +01:00

[add] parallel and non parallel tasks are independent

This commit is contained in:
Ramil Valitov 2020-09-21 15:11:00 +03:00
parent 44f3f5d567
commit 8423a7a1a1
No known key found for this signature in database
GPG Key ID: 2AB186DABDFE1914
2 changed files with 63 additions and 49 deletions

View File

@ -134,7 +134,7 @@ function restoreUserParameters() {
function AddSleepToConfig() { function AddSleepToConfig() {
PARAMS_FILE=$(getUserParameters) PARAMS_FILE=$(getUserParameters)
sudo sed -i 's#.*zabbix_php_fpm_discovery.*#UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh sleep $1#' "$PARAMS_FILE" sudo sed -i 's#.*zabbix_php_fpm_discovery.*#UserParameter=php-fpm.discover[*],sudo /etc/zabbix/zabbix_php_fpm_discovery.sh sleep max_tasks 1 $1#' "$PARAMS_FILE"
travis_fold_start "AddSleepToConfig" "ⓘ New UserParameter file" travis_fold_start "AddSleepToConfig" "ⓘ New UserParameter file"
sudo cat "$PARAMS_FILE" sudo cat "$PARAMS_FILE"
travis_fold_end travis_fold_end
@ -733,6 +733,7 @@ testZabbixDiscoverSleep() {
#Add sleep #Add sleep
AddSleepToConfig AddSleepToConfig
StartTimer
testZabbixDiscoverReturnsData testZabbixDiscoverReturnsData
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
@ -750,17 +751,6 @@ testDiscoverScriptRunDuration() {
printSuccess "${FUNCNAME[0]}" printSuccess "${FUNCNAME[0]}"
} }
testZabbixDiscoverRunDuration() {
#Add sleep
AddSleepToConfig
DATA=$(zabbix_get -s 127.0.0.1 -p 10050 -k php-fpm.discover["/php-fpm-status"])
printElapsedTime
assertExecutionTime
printSuccess "${FUNCNAME[0]}"
}
testDiscoverScriptDoubleRun() { testDiscoverScriptDoubleRun() {
DATA_FIRST=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") DATA_FIRST=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status")
DATA_SECOND=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status") DATA_SECOND=$(sudo -u zabbix sudo "/etc/zabbix/zabbix_php_fpm_discovery.sh" "debug" "sleep" "/php-fpm-status")

View File

@ -196,6 +196,22 @@ function PrintDebug() {
fi fi
} }
function getPoolManagerByCode() {
local MANAGER_CODE=$1
local $PROCESS_MANAGER
if [[ $MANAGER_CODE -eq 1 ]]; then
PROCESS_MANAGER="dynamic"
elif [[ $MANAGER_CODE -eq 2 ]]; then
PROCESS_MANAGER="static"
elif [[ $MANAGER_CODE -eq 3 ]]; then
PROCESS_MANAGER="ondemand"
else
PROCESS_MANAGER=""
fi
echo $PROCESS_MANAGER
}
# Encodes input data to JSON and saves it to result string # Encodes input data to JSON and saves it to result string
# Input arguments: # Input arguments:
# - pool name # - pool name
@ -812,47 +828,55 @@ for POOL_ITEM in "${PENDING_LIST[@]}"; do
# shellcheck disable=SC2016 # shellcheck disable=SC2016
POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}') POOL_SOCKET=$(echo "$POOL_ITEM" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}')
if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]]; then if [[ -n "$POOL_NAME" ]] && [[ -n "$POOL_SOCKET" ]]; then
PARALLEL_TASKS=$((PARALLEL_TASKS + 1)) if [[ $MAX_PARALLEL_TASKS -eq 1 ]]; then
if [[ $PARALLEL_TASKS -le $MAX_PARALLEL_TASKS ]]; then #We work in standard mode (non-parallel)
PrintDebug "Starting processing task for pool $POOL_NAME $POOL_SOCKET, subprocess #$PARALLEL_TASKS..." ProcessPool "$POOL_NAME" "$POOL_SOCKET"
ProcessPool "$POOL_NAME" "$POOL_SOCKET" & EXIT_CODE=$?
TASK_PID=$! DeletePoolFromPendingList "$POOL_NAME" "$POOL_SOCKET"
TASK_LIST+=("$POOL_NAME$ARRAY_SEPARATOR$POOL_SOCKET$ARRAY_SEPARATOR$TASK_PID")
fi
if [[ $PARALLEL_TASKS -gt $MAX_PARALLEL_TASKS ]] || [[ $LAST_PENDING_ITEM == "$POOL_ITEM" ]]; then PROCESS_MANAGER=$(getPoolManagerByCode $EXIT_CODE)
#Wait till all tasks complete if [[ -n $PROCESS_MANAGER ]]; then
if [[ $PARALLEL_TASKS -gt $MAX_PARALLEL_TASKS ]]; then UpdatePoolInCache "$POOL_NAME" "$POOL_SOCKET" "$PROCESS_MANAGER"
PrintDebug "Max number of parallel tasks reached ($MAX_PARALLEL_TASKS), waiting till they finish..." fi
else else
PrintDebug "No more parallel tasks to start, waiting for running tasks to finish..." #We work in parallel mode
PARALLEL_TASKS=$((PARALLEL_TASKS + 1))
if [[ $PARALLEL_TASKS -le $MAX_PARALLEL_TASKS ]]; then
PrintDebug "Starting processing task for pool $POOL_NAME $POOL_SOCKET, subprocess #$PARALLEL_TASKS..."
ProcessPool "$POOL_NAME" "$POOL_SOCKET" &
TASK_PID=$!
TASK_LIST+=("$POOL_NAME$ARRAY_SEPARATOR$POOL_SOCKET$ARRAY_SEPARATOR$TASK_PID")
fi fi
for TASK_LINE in "${TASK_LIST[@]}"; do
# shellcheck disable=SC2016
POOL_NAME=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}')
# shellcheck disable=SC2016
POOL_SOCKET=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}')
# shellcheck disable=SC2016
TASK_PID=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $3}')
wait $TASK_PID
EXIT_CODE=$?
PrintDebug "Finished parallel task PID $TASK_PID for pool \"$POOL_NAME\" at $POOL_SOCKET"
DeletePoolFromPendingList "$POOL_NAME" "$POOL_SOCKET"
if [[ $EXIT_CODE -ge 1 ]] && [[ $EXIT_CODE -le 3 ]]; then if [[ $PARALLEL_TASKS -gt $MAX_PARALLEL_TASKS ]] || [[ $LAST_PENDING_ITEM == "$POOL_ITEM" ]]; then
if [[ $EXIT_CODE -eq 1 ]]; then #Wait till all tasks complete
PROCESS_MANAGER="dynamic" if [[ $PARALLEL_TASKS -gt $MAX_PARALLEL_TASKS ]]; then
elif [[ $EXIT_CODE -eq 2 ]]; then PrintDebug "Max number of parallel tasks reached ($MAX_PARALLEL_TASKS), waiting till they finish..."
PROCESS_MANAGER="static" else
else PrintDebug "No more parallel tasks to start, waiting for running tasks to finish..."
PROCESS_MANAGER="ondemand"
fi
UpdatePoolInCache "$POOL_NAME" "$POOL_SOCKET" "$PROCESS_MANAGER"
fi fi
done for TASK_LINE in "${TASK_LIST[@]}"; do
PrintDebug "All previously started parallel tasks are complete" # shellcheck disable=SC2016
PARALLEL_TASKS=0 POOL_NAME=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $1}')
TASK_LIST=() # shellcheck disable=SC2016
POOL_SOCKET=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $2}')
# shellcheck disable=SC2016
TASK_PID=$(echo "$TASK_LINE" | $S_AWK -F "$ARRAY_SEPARATOR" '{print $3}')
wait $TASK_PID
EXIT_CODE=$?
PrintDebug "Finished parallel task PID $TASK_PID for pool \"$POOL_NAME\" at $POOL_SOCKET"
DeletePoolFromPendingList "$POOL_NAME" "$POOL_SOCKET"
PROCESS_MANAGER=$(getPoolManagerByCode $EXIT_CODE)
if [[ -n $PROCESS_MANAGER ]]; then
UpdatePoolInCache "$POOL_NAME" "$POOL_SOCKET" "$PROCESS_MANAGER"
fi
done
PrintDebug "All previously started parallel tasks are complete"
PARALLEL_TASKS=0
TASK_LIST=()
fi
#End of parallel mode
fi fi
#Confirm that we run not too much time #Confirm that we run not too much time