Distributed locks for everyone!
GetLock.tech service exists for those who need to get some kind of session lock or simple semaphore-like mechanism.
Actual case was absence of proper concurrency limit for BitBucket Pipelines.
PUT https://getlock.tech/v1/<uuid>Create new lock or update existing one.
Params:
ttl - float, in seconds, default is 60.0.Response codes:
201 created200 refreshedGET https://getlock.tech/v1/<uuid>Check if lock is active.
Response codes:
423 for active404 for other casesDELETE https://getlock.tech/v1/<uuid>Release existing lock.
Response codes:
200 success404 lock not foundLet's say we have some task which takes up to 1 hour yet could be triggered from number of workers. It concurrency required to be limited to single worker at a time.
We choose some random yet valid UUID as our job ID. It should be the same for all workers you decide to limit concurrency for:
YOUR_JOB_UUID=28c9c1be-7bb8-4e8c-b22e-c16402a421d5
Worker logic then could look like following:
echo "Checking if another job is running ..."
while [ "$(curl \"https://getlock.tech/v1/${YOUR_JOB_UUID}\" | jq '.locked')" = "true" ]; do
echo "Locked, waiting for free spot ..."
sleep 1
done
echo "Free spot found"
echo "Acquiring lock ..."
# NOTE Prefer lower TTL values while refreshing lock periodically
# to not waste time in case job has failed
curl -X PUT -d ttl=3600 "https://getlock.tech/v1/${YOUR_JOB_UUID}"
echo "Starting payload ..."
sleep 2400
echo "Payload done"
echo "Releasing lock ..."
curl -X DELETE "https://getlock.tech/v1/${YOUR_JOB_UUID}"
echo "Job done"
I also recommend you to consider using awesome httpie tool.
Content type
Image
Digest
sha256:2834512b2…
Size
9.4 MB
Last updated
over 3 years ago
docker pull agrrh/getlock-docs:v0.3.1