Skip to content

Commit

Permalink
implement
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemfh committed Oct 25, 2024
1 parent bf5a3a0 commit 1301948
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
description: 'The branch to use for the mutex'
required: false
default: 'gh-mutex'
force-unlock-timeout:
description: 'The time in seconds after which the lock is forcefully unlocked'
required: false
default: ''
debug:
description: 'Print debug output'
required: false
Expand All @@ -35,10 +39,10 @@ runs:
env:
ARG_BRANCH: ${{ inputs.branch }}
ARG_CHECKOUT_LOCATION: ${{ inputs.internal_checkout-location }}
ARG_FORCE_UNLOCK_TIMEOUT: ${{ inputs.force-unlock-timeout }}
ARG_GITHUB_SERVER: ${{ inputs.github_server }}
ARG_REPOSITORY: ${{ inputs.repository }}
ARG_REPO_TOKEN: ${{ inputs.repo-token }}
ARG_DEBUG: ${{ inputs.debug }}
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 @@ -20,7 +20,7 @@ echo "ticket_id=$__ticket_id" >> $GITHUB_STATE

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_FORCE_UNLOCK_TIMEOUT

echo "Lock successfully acquired"

39 changes: 36 additions & 3 deletions rootfs/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ enqueue() {
# $1: branch
# $2: queue_file
# $3: ticket_id
wait_for_lock() {
_wait_for_lock() {
__branch=$1
__queue_file=$2
__ticket_id=$3
Expand All @@ -79,12 +79,46 @@ wait_for_lock() {
if [ "$cur_lock" != "$__ticket_id" ]; then
echo "[$__ticket_id] Waiting for lock - Current lock assigned to [$cur_lock]"
sleep 5
wait_for_lock $@
_wait_for_lock $@
fi
else
echo "[$__ticket_id] $__queue_file unexpectedly empty, continuing"
fi
}
# Wait for the lock to become available,
# but if timeout is specified, then force unlock heads until we get the lock
# args:
# $1: branch
# $2: queue_file
# $3: ticket_id
# $4: timeout
wait_for_lock() {
__branch=$1
__queue_file=$2
__ticket_id=$3
__timeout=$4

# Wait for lock w/o timeout
if [ -z "$__timeout" ]; then
_wait_for_lock $1 $2 $3
return
fi

# Wait for lock with timeout
__exit_code=124
while [ $__exit_code -eq 124 ]; do
timeout $__timeout bash -c "source $SCRIPT_DIR/utils.sh && _wait_for_lock $1 $2 $3" &&
__exit_code=0 ||
__exit_code=$? # timeout returns 124

if [ $__exit_code -eq 124 ]; then
# Remove top line
echo "[$(head -n 1 $__queue_file)] Force unlocking head"
sed -i '1d' "$__queue_file"
fi
done
}

# Remove from the queue, when locked by it or just enqueued
# args:
# $1: branch
Expand Down Expand Up @@ -128,4 +162,3 @@ dequeue() {
dequeue $@
fi
}

0 comments on commit 1301948

Please sign in to comment.