Bash: 5分钟后构建失败



的想法是获取部署命令运行后的部署状态,如果状态为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部分添加一些验证以使构建失败。

在更通用和优雅地处理此场景方面的任何帮助,谢谢。

我不认为你能做到&;一般&;以不同的方式。

#!/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
#!/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

相关内容

  • 没有找到相关文章

最新更新