想法是在部署命令運行后獲取部署的狀態,如果狀態Pending為,睡眠30秒并再次檢查,直到狀態為Completed,捕獲這里,超時為300秒。(如果部署未在 300 秒內完成,則回傳代碼 1 退出。)
service_ui_staus=$(command to check the deployment of UI service)
service_db_staus=$((command to check the deployment of DB service)
for i in $(seq 1 5); do
if [[ "${service_ui_staus}" =~ "Completed" && "${service_db_staus}" =~ "Completed" ]]; then echo "Deployments done..."
break
else
sleep 30
done
上面的邏輯并不優雅,如果部署未在 300 秒內完成,我必須在 else 部分添加更多驗證以使構建失敗。
任何幫助以更通用和優雅的方式處理這種情況,謝謝。
uj5u.com熱心網友回復:
我不認為你可以以不同的方式“一般”地做到這一點。
#!/bin/bash
time_limit=300
sleep_time=30
for (( i = time_limit; i > 0 ; i -= sleep_time ))
do
service_ui_staus=$(command to check the deployment of UI service)
service_db_staus=$((command to check the deployment of DB service)
[[ "${service_ui_staus}" =~ "Completed" && "${service_db_staus}" =~ "Completed" ]] && break
sleep "$sleep_time"
done
if (( i > 0 ))
then
echo "Deployments done..."
else
commands to kill the deployment processes
exit 1
fi
uj5u.com熱心網友回復:
#!/bin/bash
service_ui_staus(){
command to check the deployment of UI service 2>&1|grep -q "Completed" && return 0
return 1
}
service_db_staus(){
command to check the deployment of DB service 2>&1|grep -q "Completed" && return 0
return 1
}
deployment_status(){
if service_ui_staus && service_db_staus; then return 0; fi
return 1
}
counter=0
maxcount=5
waittime=10
while :; do
((counter ))
if deployment_status; then
echo "Deployments done..."
# more actions ....
# .
# .
break
fi
if [ "$counter" -eq "$maxcount" ]; then
echo "Maximum counter reached. Deployment not done";
break
else
echo "waiting $waittime seconds ..."
sleep "$waittime"
fi
done
$ ./script.sh
waiting 10 seconds ...
waiting 10 seconds ...
waiting 10 seconds ...
waiting 10 seconds ...
Maximum counter reached. Deployment not done
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/492675.html
