Skip to content

Commit

Permalink
Always run unlock script completely even when cancelled or failed (#7)
Browse files Browse the repository at this point in the history
* Always run the lock/unlock scripts completely

This will avoid leaving the lock in an unexpected state

* remove undocumented `if`

* Add test for unlocking even in failure

* Instead of only popping, also unqueue on unlock

* Remove default post-if

* Change call of retry to new name unqueue

* Ignore blank lines

* Prefer dequeue over unqueue

* Swap lock check during dequeue
  • Loading branch information
chamini2 authored Jun 4, 2022
1 parent 271919c commit db51ccc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,32 @@ jobs:
- run: |
echo "I am protected!"
sleep 5
always_unlock_test_1:
runs-on: ubuntu-latest
name: Test unlocking even after failed (client 1)
steps:
- uses: actions/checkout@v3
- name: Set up mutex
uses: ./
with:
branch: gh-mutex-unlock-test
- run: |
echo "Failing, still should let the mutex free"
sleep 5
exit 1
always_unlock_test_2:
runs-on: ubuntu-latest
name: Test unlocking even after failed (client 2)
needs: [always_unlock_test_1]
if: ${{ always() }}
steps:
- uses: actions/checkout@v3
- run: echo "Should not hang"
- name: Set up mutex
uses: ./
with:
branch: gh-mutex-unlock-test
- run: sleep 5

2 changes: 1 addition & 1 deletion rootfs/scripts/unlock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ __repo_url="https://x-access-token:$ARG_REPO_TOKEN@github.com/$ARG_REPOSITORY"
__ticket_id="$STATE_ticket_id"

set_up_repo "$__repo_url"
unlock $ARG_BRANCH $__mutex_queue_file $__ticket_id
dequeue $ARG_BRANCH $__mutex_queue_file $__ticket_id

echo "Successfully unlocked"

35 changes: 20 additions & 15 deletions rootfs/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ enqueue() {
touch $__queue_file

# if we are not in the queue, add ourself to the queue
if [ -z "$(cat $__mutex_queue_file | grep -F $__ticket_id)" ]; then
echo "$__ticket_id" >> "$__mutex_queue_file"
if [ -z "$(cat $__queue_file | grep -F $__ticket_id)" ]; then
echo "$__ticket_id" >> "$__queue_file"

git add $__queue_file
git commit -m "[$__ticket_id] Enqueue " --quiet
Expand Down Expand Up @@ -75,40 +75,45 @@ wait_for_lock() {
update_branch $__branch

# if we are not the first in line, spin
if [ "$(cat $__mutex_queue_file | head -n 1)" != "$__ticket_id" ]; then
if [ "$(cat $__queue_file | awk NF | head -n 1)" != "$__ticket_id" ]; then
sleep 5
wait_for_lock $@
fi
}

# Wait for the lock to become available
# Remove from the queue, when locked by it or just enqueued
# args:
# $1: branch
# $2: queue_file
# $3: ticket_id
unlock() {
dequeue() {
__branch=$1
__queue_file=$2
__ticket_id=$3

__has_error=0


echo "[$__ticket_id] Unlocking"

update_branch $__branch

if [ "$(cat $__mutex_queue_file | head -n 1)" != "$__ticket_id" ]; then
1>&2 echo "We don't have the lock! Ticket ID: $__ticket_id. Mutex file:"
cat $__mutex_queue_file
if [ "$(cat $__queue_file | awk NF | head -n 1)" == "$__ticket_id" ]; then
echo "[$__ticket_id] Unlocking"
__message="[$__ticket_id] Unlock"
else
echo "[$__ticket_id] Dequeueing. We don't have the lock!"
__message="[$__ticket_id] Dequeue"
fi

if [ $(awk "/$__ticket_id/" $__queue_file | wc -l) == "0" ]; then
1>&2 echo "[$__ticket_id] Not in queue! Mutex file:"
cat $__queue_file
exit 1
fi

cat $__mutex_queue_file | tail -n +2 > ${__mutex_queue_file}.new
mv ${__mutex_queue_file}.new $__mutex_queue_file
awk "!/$__ticket_id/" $__queue_file | awk NF > ${__queue_file}.new
mv ${__queue_file}.new $__queue_file

git add $__queue_file
git commit -m "[$__ticket_id] Unlock" --quiet
git commit -m "$__message" --quiet

set +e # allow errors
git push --set-upstream origin $__branch --quiet
Expand All @@ -117,7 +122,7 @@ unlock() {

if [ ! $__has_error -eq 0 ]; then
sleep 1
unlock $@
dequeue $@
fi
}

0 comments on commit db51ccc

Please sign in to comment.