#!/usr/bin/env bash
# Marks a cache directory as held open by a running job, so ANY job's prune
# pass (including this run's own) skips it.
#
# Usage: cache-lock.sh acquire|release
#
# Without this, a directory's only protection from a concurrently running
# job's eviction pass is "it happens to also be that job's own target dir",
# which is true for the job that owns it and false for everyone else. The
# marker is per-job (not just per-run) so two jobs sharing one cache key each
# hold an independent lock rather than one clobbering the other's.
#
# A lock is a timestamp file, not a real mutex: prune-cache.sh honours it only
# until STALE_LOCK_SECONDS, after which it is treated as abandoned by a job
# the runner killed before it reached its own release step. Honouring a lock
# forever would let one crashed job pin a directory permanently.
set -euo pipefail
MODE="${1:?usage: cache-lock.sh acquire|release }"
DIR="${2:?}"; ID="${3:?}"
case "$MODE" in
acquire)
mkdir -p "$DIR"
date +%s > "$DIR/.ci-lock-${ID}"
echo "lock: acquired .ci-lock-${ID} on $(basename "$DIR")"
;;
release)
rm -f "$DIR/.ci-lock-${ID}" 2>/dev/null || true
echo "lock: released .ci-lock-${ID} on $(basename "$DIR")"
;;
*)
echo "::error::cache-lock.sh: unknown mode '$MODE' (expected acquire or release)" >&2
exit 1
;;
esac