Skip to content

Commit

Permalink
Handle lock matching better and clean up (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran-Rg authored Mar 1, 2024
1 parent 8eb8c5a commit 5baf892
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.16
FROM alpine:3.19

RUN apk update \
&& apk add --no-cache bash curl git
Expand Down
33 changes: 18 additions & 15 deletions rootfs/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enqueue() {
touch $__queue_file

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

git add $__queue_file
Expand Down Expand Up @@ -70,17 +71,20 @@ wait_for_lock() {
__queue_file=$2
__ticket_id=$3

echo "[$__ticket_id] Waiting for lock"

update_branch $__branch

# if we are not the first in line, spin
if [ "$(cat $__queue_file | awk NF | head -n 1)" != "$__ticket_id" ]; then
sleep 5
wait_for_lock $@
if [ -s $__queue_file ]; then
cur_lock=$(head -n 1 $__queue_file)
if [ "$cur_lock" != "$__ticket_id" ]; then
echo "[$__ticket_id] Waiting for lock - Current lock assigned to [$cur_lock]"
sleep 5
wait_for_lock $@
fi
else
echo "[$__ticket_id] $__queue_file unexpectedly empty, continuing"
fi
}

# Remove from the queue, when locked by it or just enqueued
# args:
# $1: branch
Expand All @@ -95,23 +99,22 @@ dequeue() {

update_branch $__branch

if [ "$(cat $__queue_file | awk NF | head -n 1)" == "$__ticket_id" ]; then
if [[ "$(head -n 1 $__queue_file)" == "$__ticket_id" ]]; then
echo "[$__ticket_id] Unlocking"
__message="[$__ticket_id] Unlock"
else
# Remove top line
sed -i '1d' "$__queue_file"
elif grep -qx "$__ticket_id" "$__queue_file" ; then
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
# Remove the matching line
sed -i "/^${__ticket_id}$/d" $__queue_file
else
1>&2 echo "[$__ticket_id] Not in queue! Mutex file:"
cat $__queue_file
exit 1
fi

awk "!/$__ticket_id/" $__queue_file | awk NF > ${__queue_file}.new
mv ${__queue_file}.new $__queue_file

git add $__queue_file
git commit -m "$__message" --quiet

Expand Down

0 comments on commit 5baf892

Please sign in to comment.