From 97cbd3abbec85e27d29bbfafc9a881040ff97532 Mon Sep 17 00:00:00 2001 From: Alex Iribarren <Alex.Iribarren@cern.ch> Date: Fri, 15 Jan 2021 14:33:25 +0100 Subject: [PATCH] Implement job-level locks --- reposync/runreposync.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/reposync/runreposync.sh b/reposync/runreposync.sh index e3c1084..9ce5f65 100755 --- a/reposync/runreposync.sh +++ b/reposync/runreposync.sh @@ -33,6 +33,26 @@ REPOPATH="/repo/${REPOPATH}" # Create REPOPATH even if the repo is empty [ ! -d $REPOPATH ] && /bin/mkdir -p ${REPOPATH} +# Let's make absolutely sure there's only one copy of the job running +LOCKFILE="$REPOPATH/.reposynclock" +if [[ -f "$LOCKFILE" ]]; then + # The lockfile exists, let's check if it's fresh + AGE=$((`date +%s` - `stat -c '%Y' $LOCKFILE`)) + if [[ $AGE -gt 86400 ]]; then + echo "$LOCKFILE is over 24 hours old. It's probably stale, so get rid of it." + rm -f "$LOCKFILE" + fi +fi + +# Grab the lock, exit if you fail +if !(set -o noclobber; echo "`hostname` ($$)" > "$LOCKFILE") 2> /dev/null; then + echo "Lock exists: $LOCKFILE owned by $(cat $LOCKFILE)" + exit +fi + +# Delete the lock if we die +trap 'rm -f "$LOCKFILE"; exit $?' INT TERM EXIT + if [[ $RUN_REPOSYNC -eq 1 ]]; then # Record what's already there first PRECOUNT=`/usr/bin/find ${REPOPATH} -type f -name "*.rpm" ! -size 0 | /usr/bin/wc -l` @@ -192,3 +212,7 @@ cat << EOF | log > $OUTPUT "post_count": ${POSTCOUNT}, "changes": [${LIST}] EOF + +# Clean up after yourself, and release your trap +rm -f "$LOCKFILE" +trap - INT TERM EXIT -- GitLab