Home > OS >  Bash : fail the build after 5 minutes
Bash : fail the build after 5 minutes

Time:06-17

Idea is to get the status of the deployment after the deployment command is run, If the status says Pending, sleep for 30 seconds and check again untill the status is Completed, catch here being, timeout is 300 seconds. (if the deployment is not Completed within 300 seconds, exit with return code 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

Above logic is not elegant and I've to add few more validations in the else part to fail the build, if the deployment is not completed within 300 seconds.

Any help in handling this scenario in more generic and elegantly, thanks.

CodePudding user response:

I don't think that you can do it "generically" in a different manner.

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

CodePudding user response:

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