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

max_lock_timeout threshold #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions gcslock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,37 @@
# echo "lock acquired"
# unlock mybucket

# The lock function expects the first (and only) argument to be
# the name of a bucket writable by the user running this script.
# The lock function expects the first argument to be the name of
# a bucket writable by the user running this script.
# As a second (optional) argument you can pass int value as max
# lock wait timeout, when total waiting time exceed we will
# consider that other running copy of this script died (dead-lock)
# or could not exited at given time.
# It creates the lock object in the passed bucket with a special
# header to obtain mutual exclusion. If the lock object creation
# fails, it retries indefinitely with expontential backoff.

lock() {
if [ "$1" = "" ]
bucket="$1"
max_lock_timeout="${2:-300}"
total_sleep=0
if [ "$bucket" = "" ]
then
echo "lock: missing bucket argument"
exit 1
fi
LOCK="gs://$1/lock"
LOCK="gs://$bucket/lock"
sleep_time=1
while ! echo "lock" | gsutil -q -h "x-goog-if-generation-match:0" cp - $LOCK
do
echo "lock: failed to obtain lock, retrying in $sleep_time seconds"
sleep $sleep_time
total_sleep=$((total_sleep+sleep_time))
if [ $total_sleep -gt $max_lock_timeout ]
then
echo "lock: lock timeout ${max_lock_timeout}s exceeded, forcing to get lock"
gsutil rm -f $LOCK
fi
sleep_time=$(expr $sleep_time '*' 2)
done
}
Expand Down