How to avoid deadlocks using the API

api
#1

How to check the number of concurrency available for a build?

In the post that discusses how to fan out steps in a workflow, there was a mention about avoiding deadlocks and a brief description of a solution.

Which particular API endpoint(s) was the post pertaining to?
Is there a chance I can get more detail on this?

Thank you.

0 Likes

#2

Hey @jcyu0208,

This is the exact API endpoint: https://api-docs.bitrise.io/#/organizations/org-show

And here’s a simple script that implements the waiting mechanism:

Script
#!/usr/bin/env bash
# fail if any commands fails
set -e
# debug log
#set -x

if [ -z "$STARTED_BUILD_COUNT" ]
then
    echo "\$STARTED_BUILD_COUNT is empty. Please add the variable on the Env Vars tab of the Workflow Editor and specified the number of builds started with fan out"
    exit 1
else
    echo "\$STARTED_BUILD_COUNT is set to $STARTED_BUILD_COUNT"
fi

brew install jq
# Get the currently available CCs
json=$(curl -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$BITRISE_APP_SLUG)
account_type=$(echo "${json}" | jq -R '.data.owner.account_type')
account_slug=$(echo "${json}" | jq -R '.data.owner.slug')
cc_count=${TRIAL_EXTRA_CC_COUNT-0}
if [ "$account_type" == "organization" ]
then
    json=$(curl -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/organizations/'$account_slug)
    plan_cc=$(echo "${json}" | jq -r '.data.concurrency_count')
else
    json=$(curl -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/me/plan')
    plan_cc=$(echo "${json}" | jq -r '.data.current_plan.container_count')
fi
if (("plan_cc" > 0))
then
    cc_count+=plan_cc
fi
if [ $cc_count -eq 0 ]
then
    echo "no concurrencies"
    exit 2
fi

if [ $STARTED_BUILD_COUNT -ge $cc_count ]
then
    echo "you are trying to start more builds during fan out than the available CC count. If you have an active trial with extra concurrencies please add the number of concurrencies on the Env Vars tab of the Workflow Editor to a variable called TRIAL_EXTRA_CC_COUNT and run the build again!"
    exit 3
fi

# Get currently running build count
json=$(curl -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$BITRISE_APP_SLUG'/builds?status=0')
running_build_count=$(echo "${json}" | jq -r '.paging.total_item_count')

available_cc_count=$((cc_count - running_build_count))

echo "Running build count for the owner: " + $running_build_count + "\nAvailable CC count: " + $available_cc_count

# Check if there's enough available concurrencies to run the fan out builds (only using the STARTED_BUILD_COUNT, that indicates the number of builds this workflow is going to start as the current workflow is already running so it would be already counted in the available_cc_count as a running build
if [ $STARTED_BUILD_COUNT -gt $available_cc_count ]
then
    while true
    do
        echo "Time Now: `date +%H:%M:%S`"
        echo "Sleeping for 5 seconds"
        wait_period=$(($wait_period+5))
        json=$(curl -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$BITRISE_APP_SLUG'/builds?status=0')
        running_build_count=$(echo "${json}" | jq -r '.paging.total_item_count')
        available_cc_count=$((cc_count - running_build_count))
        if [ $STARTED_BUILD_COUNT -le $available_cc_count ]
        then
            echo "enough CCs are available, starting fan out" # do nothing as the fan-out can run smoothly
            break
        fi
        if [ $wait_period -gt $WAIT_TIMEOUT ];then
            echo "Not enough concurrencies, restarting"
            break
        else
            sleep 5
        fi
    done
    json=$(curl -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$BITRISE_APP_SLUG'/builds?status=0')
    running_build_count=$(echo "${json}" | jq -r '.paging.total_item_count')

    available_cc_count=$((cc_count - running_build_count))
fi

if [ $STARTED_BUILD_COUNT -gt $available_cc_count ]
then
    echo "start the same build again"
    build_params=$(curl -sH 'Accept-encoding: json' -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$slug'/builds/'$BITRISE_BUILD_SLUG'' | jq -r '.data.original_build_params')
    curl -X POST -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$slug'/builds' -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"build_params\": "''"${build_params}"''", \"hook_info\": {\"type\": \"bitrise\"}}"
    echo "aborting current build"
    json=$(curl -X POST -H 'Authorization: '$permapi'' 'https://api.bitrise.io/v0.1/apps/'$BITRISE_APP_SLUG'/builds/'$BITRISE_BUILD_SLUG'/abort' -d '{"abort_reason": "abort with abort_with_success=true test & skip_notifications=true", "abort_with_success": true,"skip_notifications": true}')
else
    echo "enough CCs are available, starting fan out" # do nothing as the fan-out can run smoothly
fi
0 Likes