Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timeout and slim down msg #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ inputs:
description: '[Internal] The location to store the mutex repo'
required: false
default: '/run/gh-action-mutex/repo'
timeout:
description: 'timeout in seconds to return exist code 1'
required: false
default: '0'
runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -39,6 +43,7 @@ runs:
ARG_REPOSITORY: ${{ inputs.repository }}
ARG_REPO_TOKEN: ${{ inputs.repo-token }}
ARG_DEBUG: ${{ inputs.debug }}
ARG_TIMEOUT: ${{ inputs.timeout }}
entrypoint: '/scripts/lock.sh'
post-entrypoint: '/scripts/unlock.sh'

2 changes: 1 addition & 1 deletion rootfs/scripts/lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID"

set_up_repo "$__repo_url"
enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id
wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id
wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id $ARG_TIMEOUT

echo "Lock successfully acquired"

1 change: 0 additions & 1 deletion rootfs/scripts/unlock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ cd "$ARG_CHECKOUT_LOCATION"

__mutex_queue_file=mutex_queue
__repo_url="https://x-access-token:$ARG_REPO_TOKEN@$ARG_GITHUB_SERVER/$ARG_REPOSITORY"
cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID"
export $(grep -v '^#' $GITHUB_WORKSPACE/.$GITHUB_RUN_ID | xargs -0)
__ticket_id=$ticket_id

Expand Down
9 changes: 9 additions & 0 deletions rootfs/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ enqueue() {
fi
}

total_sleep_time=0
# Wait for the lock to become available
# args:
# $1: branch
# $2: queue_file
# $3: ticket_id
# $4: timeout in minuts
wait_for_lock() {
__branch=$1
__queue_file=$2
__ticket_id=$3
__timeout=$4

update_branch $__branch

Expand All @@ -79,6 +82,12 @@ wait_for_lock() {
if [ "$cur_lock" != "$__ticket_id" ]; then
echo "[$__ticket_id] Waiting for lock - Current lock assigned to [$cur_lock]"
sleep 5
total_sleep_time=$((total_sleep_time + 5))
echo "It has been waiting for $total_sleep_time seconds zzz"
if [[ $__timeout -gt 0 && $total_sleep_time -gt $__timeout ]]; then
echo "[$__ticket_id] Total sleep time exceeded $__timeout seconds, exiting with code 1"
exit 1
fi
wait_for_lock $@
fi
else
Expand Down