diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 55075e6..89fbb4f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/rootfs/scripts/unlock.sh b/rootfs/scripts/unlock.sh index ad49296..553ec5a 100755 --- a/rootfs/scripts/unlock.sh +++ b/rootfs/scripts/unlock.sh @@ -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" diff --git a/rootfs/scripts/utils.sh b/rootfs/scripts/utils.sh index 1f521ac..232cf19 100644 --- a/rootfs/scripts/utils.sh +++ b/rootfs/scripts/utils.sh @@ -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 @@ -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 @@ -117,7 +122,7 @@ unlock() { if [ ! $__has_error -eq 0 ]; then sleep 1 - unlock $@ + dequeue $@ fi }