Files
kvitt/tools/gradle-lock.sh
T

25 lines
1.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# The one-Gradle-build-at-a-time lock (CLAUDE.md › Build and verify). Sourced, not run, by tools/gradle-serial.sh,
# tools/clean-builds.sh, infra/dev/build.sh and infra/build-images.sh; plain POSIX sh, so sh and bash scripts share it.
# The lock is a directory holding the pid of its owner; a lock whose owner has died is taken over.
# shellcheck disable=SC2034 # KVITT_GRADLE_LOCK is used by the scripts that source this file
KVITT_GRADLE_LOCK=/tmp/kvitt-gradle.lock
# Prints the pid of the live process holding the lock and succeeds; fails when nobody holds it.
gradle_lock_holder() {
_pid=$(cat "$KVITT_GRADLE_LOCK/pid" 2>/dev/null) || return 1
[ -n "$_pid" ] && kill -0 "$_pid" 2>/dev/null || return 1
printf '%s\n' "$_pid"
}
# Waits until the lock is free, then takes it for this shell ($$). The caller removes it on exit:
# trap 'rm -rf "$KVITT_GRADLE_LOCK"' EXIT INT TERM HUP
gradle_lock_acquire() {
while ! mkdir "$KVITT_GRADLE_LOCK" 2>/dev/null; do
_pid=$(cat "$KVITT_GRADLE_LOCK/pid" 2>/dev/null)
if [ -n "$_pid" ] && ! kill -0 "$_pid" 2>/dev/null; then rm -rf "$KVITT_GRADLE_LOCK"; continue; fi
sleep 5
done
echo $$ > "$KVITT_GRADLE_LOCK/pid"
}