Skip to content

Commit

Permalink
Cancel build if pipeline number is newer
Browse files Browse the repository at this point in the history
If pipeline number is newer, that means we have reach the API limit of
running jobs, and newer pipeline already got front of the line, so we
can cancel the current build as it is already out of date.

In our use case this means we would try to deploy an older commit which
would then get rejected since a newer one is already deployed.

Also, reduce max wait time by default, we never want to wait for that
long. Plus, drop "dont-quit" and "force-cancel-previous", we don't use
them and not implemented.
  • Loading branch information
PChambino committed Feb 14, 2024
1 parent 475cb7c commit ba88b3d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 40 deletions.
38 changes: 19 additions & 19 deletions scripts/loop.bash
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ cancel_build_num(){
echo "Cancelling build ${BUILD_NUM}"
cancel_api_url_template="${CIRCLECI_BASE_URL}/api/v1.1/project/${VCS_TYPE}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${BUILD_NUM}/cancel?circle-token=${CCI_TOKEN}"
curl -s -X POST $cancel_api_url_template > /dev/null

sleep 30 # wait for API to cancel this job, rather than showing as failure
exit 1 # but just in case, fail job
}


Expand All @@ -190,7 +193,7 @@ cancel_build_num(){

#
# MAIN LOGIC STARTS HERE
#
#
load_variables
do_we_run #exit early if we can
echo "Max Queue Time: ${max_time_seconds} seconds."
Expand All @@ -207,6 +210,18 @@ while true; do
echo "This Job's Pipeline #: $MY_PIPELINE_NUMBER"
echo "Front of Queue (fifo) Pipeline #: $front_of_queue_pipeline_number"

if [ -z "$front_of_queue_pipeline_number" ]; then
# NOTE: This can happen if there are more jobs running than the ones we fetched from the API due to pagination limits.
echo "We failed to find the front of the queue, this means another pipeline probably already got front of the queue, so cancel this one..."
cancel_build_num $CIRCLE_BUILD_NUM
fi

if [ $front_of_queue_pipeline_number -gt $MY_PIPELINE_NUMBER ]; then
# NOTE: This can happen if there are more jobs running than the ones we fetched from the API due to pagination limits.
echo "We found a newer pipeline as the front of the queue, this means another pipeline probably already got front of the queue, so cancel this one..."
cancel_build_num $CIRCLE_BUILD_NUM
fi

if [ "$front_of_queue_pipeline_number" = "$MY_PIPELINE_NUMBER" ]; then
# recent-jobs API does not include pending, so it is possible we queried in between a workflow transition, and we're NOT really front of line.
if [ $confidence -lt $CONFIDENCE_THRESHOLD ]; then
Expand All @@ -221,28 +236,13 @@ while true; do
else
# If we fail, reset confidence
confidence=0
if [ -z "$front_of_queue_pipeline_number" ]; then
echo "This build (${CIRCLE_BUILD_NUM}), pipeline (${MY_PIPELINE_NUMBER}) is queued, waiting for total number of jobs to be under 100."
else
echo "This build (${CIRCLE_BUILD_NUM}), pipeline (${MY_PIPELINE_NUMBER}) is queued, waiting for build(${oldest_running_build_num}) pipeline (${front_of_queue_pipeline_number}) to complete."
fi
echo "This build (${CIRCLE_BUILD_NUM}), pipeline (${MY_PIPELINE_NUMBER}) is queued, waiting for build(${oldest_running_build_num}) pipeline (${front_of_queue_pipeline_number}) to complete."
echo "Total Queue time: ${wait_time} seconds."
fi

if [ $wait_time -ge $max_time_seconds ]; then
echo "Max wait time exceeded, fail or force cancel..."
if [ "${DONT_QUIT}" = "1" ];then
echo "Orb parameter dont-quit is set to true, letting this job proceed!"
if [ "${FORCE_CANCEL_PREVIOUS}" = "1" ]; then
"FEATURE NOT IMPLEMENTED"
exit 1
fi
exit 0
else
cancel_build_num $CIRCLE_BUILD_NUM
sleep 5 # wait for API to cancel this job, rather than showing as failure
exit 1 # but just in case, fail job
fi
echo "Max wait time exceeded, cancel..."
cancel_build_num $CIRCLE_BUILD_NUM
fi

sleep $loop_time
Expand Down
12 changes: 1 addition & 11 deletions src/commands/until_front_of_line.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@ parameters:
description: "If true, this job will block until no other workflows with ANY JOBS with an earlier timestamp are running. Typically used as first job."
max-wait-time:
type: integer
default: 10
default: 5
description: "How many minutes to wait before giving up."
dont-quit:
type: boolean
default: false
description: "Quitting is for losers. Force job through once time expires instead of failing."
force-cancel-previous:
type: boolean
default: false
description: "No Mercy. Issue cancel commands for any previous competitors (only applies when dont-quit also true)"
limit-branch-name:
type: string
default: "*"
Expand Down Expand Up @@ -64,8 +56,6 @@ steps:
ONLY_ON_BRANCH: <<parameters.limit-branch-name>>
BLOCK_WORKFLOW: <<parameters.block-workflow>>
MAX_TIME: <<parameters.max-wait-time>>
DONT_QUIT: <<parameters.dont-quit>>
FORCE_CANCEL_PREVIOUS: <<parameters.force-cancel-previous>>
FILTER_BRANCH: << parameters.this-branch-only >>
ONLY_ON_WORKFLOW: <<parameters.limit-workflow-name>>
CONFIDENCE_THRESHOLD: <<parameters.confidence>>
Expand Down
11 changes: 1 addition & 10 deletions src/jobs/block_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@ parameters:
description: "If true, this job will block until no other workflows with an earlier timestamp are running. Typically used as first job."
max-wait-time:
type: integer
default: 10
default: 5
description: "How many minutes to wait before giving up."
dont-quit:
type: boolean
default: false
description: "Quitting is for losers. Force job through once time expires instead of failing."
force-cancel-previous:
type: boolean
default: false
description: "No Mercy. Issue cancel commands for any previous competitors (only applies when dont-quit also true)"
limit-branch-name:
type: string
default: "*"
Expand Down Expand Up @@ -64,7 +56,6 @@ steps:
this-branch-only: <<parameters.this-branch-only>>
block-workflow: <<parameters.block-workflow>>
max-wait-time: <<parameters.max-wait-time>>
dont-quit: <<parameters.dont-quit>>
limit-workflow-name: <<parameters.limit-workflow-name>>
limit-branch-name: <<parameters.limit-branch-name>>
confidence: <<parameters.confidence>>
Expand Down

0 comments on commit ba88b3d

Please sign in to comment.