我遇到了一个问题,我编写了一个bash shell脚本来测试参数化的URL并返回HTTP响应代码。
我的目的是将URL传递给此脚本,验证URL是否符合我们的内部需求,然后尝试测试URL并检索HTTP响应代码。
脚本应重试配置次数(5),尝试之间暂停(30秒),但不会执行此操作。
我希望对URL的测试是即时的,我需要睡觉才能给应用程序(让我们在Tomcat或JBoss中说)开始。
相反,我得到的是一个非常长的暂停,当应用程序启动并处于某种 limbo 时。一旦应用程序联机,或者无法启动验证尝试,则返回正确的HTTP代码,并且脚本会将活动返回给source-r。
#!/bin/bash
#######################################################################################################
# APPLICATION ENDPOINT VALIDATION
#
# This function tests a parameterized URI using cURL and uses the HTTP response value to determine the
# whether the targeted endpoint is available.
#
# Endpoint validation returns a binary value to indicate whether the response was satisfactory.
#
# This functionality is generally used after an application is deployed or restarted.
#######################################################################################################
# TEST THE ENDPOINT
#######################################################################################################
function testEndpointAvailability {
# -L is used to ensure cURL follows up to 30 status changes through to the end.
# Without the -L, cURL will stop at the Apache reverse-proxy 301 status instead of listening for
# the eventual response.
endpointResponse=$(curl -oL --write-out '%{http_code}\n' --silent --head "${endpointToValidate}")
if ! [[ -z "${endpointResponse}" ]]
then
if ! [[ "${endpointResponse}" -eq "200" ]]
then
logThis "${tfAdminLogDir}/${tfAdminLogFile}" "HTTP Response was: ${endpointResponse}" "INFO"
else
logThis "${tfAdminLogDir}/${tfAdminLogFile}" "HTTP Response was OK: ${endpointResponse}" "INFO"
fi
fi
}
#######################################################################################################
# LOOP VALIDATION ATTEMPTS
#######################################################################################################
function validationLoop {
if [[ -z "${endpointResponse}" ]]
then
testEndpointAvailability
fi
while [[ "${endpointResponse}" -ne "200" ]] || [[ "${retryCount}" -gt "0" ]]
do
if ! [[ "${retryCount}" -eq "0" ]] && [[ "${retryCount}" -ne "${originalRetryCount}" ]]
then
testEndpointAvailability
sleep "${retryDelay}"
retryCount=$(( "${retryCount}" - 1 ))
else
logThis "${tfAdminLogDir}/${tfAdminLogFile}" "HTTP Response was OK: ${endpointResponse}" "INFO"
return 0
fi
done
}
#######################################################################################################
# VALIDATE ENDPOINT AVAILABILITY
#
# validateEndpointAvailability endpointToValidate
#######################################################################################################
function validateEndpointAvailability {
###################################################################################################
# TODO
#
# [ ] Externalize instantiation of retryCount and retryDelay to properties file(s).
# [ ] Externalize regex to property file(s).
###################################################################################################
retryCount="5"
originalRetryCount="${retryCount}"
retryDelay="30"
endpointToValidate="$1"
###################################################################################################
# Regex determines viable URL patterns, does not account for all spec options, but meets this
# script's needs for testing the internal URLs for in-house applications.
###################################################################################################
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
# Determine if properly parameter count was used
if ! [[ "$#" -eq "1" ]]
then
logThis "${tfAdminLogDir}/${tfAdminLogFile}" "Unable to validate endpoint availability: Improper parameter count." "ERROR"
exit 1
fi
# Check parameterized endpoint for valid structure
if echo "${endpointToValidate}" | grep -Eq "${regex}"
then
logThis "${tfAdminLogDir}/${tfAdminLogFile}" "Endpoint appears to be properly formed, proceeding with availability check." "INFO"
validationLoop
else
logThis "${tfAdminLogDir}/${tfAdminLogFile}" "Unable to validate endpoint availability: Provided endpoint is improperly formed." "ERROR"
exit 1
fi
}
trap cleanupOnExit exit