Skip to content

Instantly share code, notes, and snippets.

@rudolphjacksonm
Created December 17, 2021 14:51
Show Gist options
  • Save rudolphjacksonm/1fa3ff2bdb06cfe8cef12e1f50610a66 to your computer and use it in GitHub Desktop.
Save rudolphjacksonm/1fa3ff2bdb06cfe8cef12e1f50610a66 to your computer and use it in GitHub Desktop.
Test CosmosDB connectivity with newly generated key
# Test CosmosDB Connectivity with a given string
function test_connectivity() {
local connString=$1
local counter=0
local successCount=0 # count of successful attempts
local maxWait=120 # max time allowed between loops
local minWait=5 # start of process
local timeOut=$((120 * 60)) # max time allowed until the loop is cancelled
local waitTime=minWait # time to wait between loops
local requiredSuccessConnections=5 # Number of successful connections required
local backoffRatio=2 # Multiplier for the exponential backoff
startTime="$(date +%s)"
# ensure we can connect to CosmosDB at least 5 times successfully
until [[ "$(($(date +%s)-startTime > timeOut))" -eq 1 ]] || [[ "${successCount}" == "${requiredSuccessConnections}" ]]; do
if mongosh "${connString}" --eval 'db.stats()'; then
waitTime=minWait
((counter=counter+1))
((successCount=successCount+1))
echo "Successful connection attempts: ${successCount}"
else
successCount=0
waitTime=$((maxWait > $((waitTime*2)) ? waitTime * backoffRatio : maxWait))
((counter=counter+1))
echo "Connection attempt ${counter} unsuccessful, retrying in ${waitTime} seconds..."
sleep "${waitTime}"
fi
done
if [[ "$(($(date +%s)-startTime > timeOut))" -eq 1 ]]; then
echo "Reached maximum attempts to connect to CosmosDB, an issue with connectivity is present!"
exit 1
else
echo "Connection successfully established to CosmosDB, proceeding with Key Vault secret update..."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment