From 1021f3f33d607b689a1050db0e0f1111751bf75c Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Wed, 10 Jun 2020 11:45:58 -0700 Subject: [PATCH] gc.sh: refactor lock_gc function for reuse Split the `lock_gc` function into two parts: 1) a new `v_lock_gc` function that always returns and is reusable 2) a minimal `lock_gc` function that calls the new function and provides behavior identical to the previous incarnation of the `lock_gc` function Signed-off-by: Kyle J. McKay --- jobd/gc.sh | 61 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/jobd/gc.sh b/jobd/gc.sh index f7694bd..bb3de2d 100755 --- a/jobd/gc.sh +++ b/jobd/gc.sh @@ -367,39 +367,56 @@ pack_is_complete() { } # On return a "$lockf" will have been created that must be removed when gc is done -lock_gc() { +# Create a gc.pid lockfile +# $1 => name of variable to store result in +# On success: +# variable named by $1 will contain the name of the newly create lockfile (i.e. "gc.pid") +# On failure: +# variable named by $1 will contain the failure reason +v_lock_gc() { # be compatibile with gc.pid file from newer Git releases - lockf=gc.pid - hn="$(hostname)" - active= - if [ "$(createlock "$lockf")" ]; then - # If $lockf is: + _lockf='gc.pid' + _hn="$(hostname)" + _active= + if [ "$(createlock "$_lockf")" ]; then + # If $_lockf is: # 1) less than 12 hours old # 2) contains two fields (pid hostname) NO trailing NL # 3) the hostname is different OR the pid is still alive # then we exit as another active process is holding the lock - if [ "$(find -L "$lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then - apid= - ahost= - read -r apid ahost ajunk <"$lockf" || : - if [ "$apid" ] && [ "$ahost" ]; then - if [ "$ahost" != "$hn" ] || pidactive "$apid"; then - active=1 + if [ "$(find -L "$_lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then + _apid= + _ahost= + read -r _apid _ahost _ajunk <"$_lockf" || : + if [ "$_apid" ] && [ "$_ahost" ]; then + if [ "$_ahost" != "$_hn" ] || pidactive "$_apid"; then + _active=1 fi fi fi else - echo >&2 "[$proj] unable to create gc.pid.lock file" - exit 1 + eval "$1="'"unable to create $_lockf.lock file"' + return 1 fi - if [ -n "$active" ]; then - rm -f "$lockf.lock" - echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'" - exit 1 + if [ -n "$_active" ]; then + rm -f "$_lockf.lock" + eval "$1="'"gc already running on machine '\''$_ahost'\'' pid '\''$_apid'\''"' + return 1 fi - printf "%s %s" "$$" "$hn" >"$lockf.lock" - chmod 0664 "$lockf.lock" - mv -f "$lockf.lock" "$lockf" + printf "%s %s" "$$" "$_hn" >"$_lockf.lock" + chmod 0664 "$_lockf.lock" + mv -f "$_lockf.lock" "$_lockf" + eval "$1="'"$_lockf"' + return 0 +} + +# On return a "$lockf" will have been created that must be removed when gc is done +lock_gc() { + v_lock_gc _lockresult || { + echo >&2 "[$proj] $_lockresult" + exit 1 + } + lockf="$_lockresult" } # Create a repack subdirectory such that running repack in it will pack the -- 2.11.4.GIT