diff --git a/reposync/runreposync.sh b/reposync/runreposync.sh index e3c1084787a3b860c9024393ebb0e9f5c357d0ca..9ce5f65d98b2313b146e185f63fb16d034c6bd90 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