remove stamp file if older than one hour
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 7 Dec 2010 14:01:28 +0000 (15:01 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 7 Dec 2010 14:01:28 +0000 (15:01 +0100)
clean stamp file if job interrupted
pretty printed

scripts/git-mirror.sh

index b28a725..0578e59 100755 (executable)
@@ -11,9 +11,8 @@ NOTIFIED_FILE="NOTIFIED_ADMINS"
 RUNNING_FILE=$LOCAL_MIRROR_DIR/RUNNING_MIRROR
 
 function msg () {
-    if [ $QUIET -eq 0 ]
-    then
-        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@"
+    if [ $QUIET -eq 0 ] ; then
+        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "$@"
     fi
 }
 
@@ -22,21 +21,18 @@ function error () {
     MSG=$2
     CHECK_FILE=$3/$NOTIFIED_FILE
 
-    if [ -f $CHECK_FILE ]
-    then
+    if [ -f $CHECK_FILE ];  then
         return
     fi
 
-    for admin in $ADMINS
-    do
+    for admin in $ADMINS; do
         echo -e "$MSG" | mail -s "$SUBJECT" $admin
     done
     touch $CHECK_FILE
 }
 
 function run () {
-    if [ $QUIET -eq 1 ]
-    then
+    if [ $QUIET -eq 1 ]; then
         COMMAND="$1 &> /dev/null"
     else
         COMMAND="$1"
@@ -46,8 +42,7 @@ function run () {
 
     pushd ${REPO} > /dev/null
     eval $COMMAND
-    if [ $? -ne 0 ]
-    then
+    if [ $? -ne 0 ]; then
         FAILED=1
         FAILED_CMDS="$FAILED_CMDS\n$COMMAND" 
     fi
@@ -109,16 +104,13 @@ function mirror () {
         # if there is no remote repository it may be that we only have
         # the repository locally and don't need to mirror
         git ls-remote $MIRROR_REPO &> /dev/null
-        if [ $? -eq 0 ]
-        then
-            if [ -d ${REPO_DIR} ]
-            then
+        if [ $? -eq 0 ]; then
+            if [ -d ${REPO_DIR} ]; then
                msg "pulling from ${NAME}"
                 run "git fetch origin --tags" ${REPO_DIR}
                run "git fetch origin" ${REPO_DIR}
                 merge_all_branches $NAME origin $REPO_DIR
-                if [ $? -ne 0 ]
-                then
+                if [ $? -ne 0 ]; then
                     error "git-mirror.sh failed" "Can not fetch from ${MASTER_REPO}" $REPO_DIR
                 fi
             else
@@ -131,8 +123,7 @@ function mirror () {
             run "git fetch local_master --tags" ${REPO_DIR}
             run "git fetch local_master" ${REPO_DIR}
             merge_all_branches $NAME local_master $REPO_DIR
-            if [ $FAILED -ne 0 ]
-            then
+            if [ $FAILED -ne 0 ]; then
                 pushd ${REPO_DIR} > /dev/null
                 STATUS_OUT=$(git status)
                 popd > /dev/null                
@@ -150,6 +141,12 @@ function mirror () {
 }
 
 
+function failure () {
+    msg "Received signal - cleaning up $RUNNING_FILE and aborting"
+    rm -f $RUNNING_FILE
+    exit 1
+}
+
 while getopts ":hq" opt
 do
   case $opt in
@@ -158,7 +155,7 @@ do
           break
           ;;
       h)
-          echo "USAGE: $0 [-q] REPONAME*"
+          echo "Usage: $0 [-q] REPONAME*"
           exit 1
           ;;
       \?)
@@ -168,17 +165,23 @@ do
 done
 
 
-if [ -f $RUNNING_FILE ]
-then
-    if [ $QUIET -eq 0 ]
-    then
-        echo "Another git-mirror is running. Aborting... " $RUNNING_FILE
-    fi
-else
-    shift $((OPTIND-1))
-    touch $RUNNING_FILE
-    mirror $@
+# is the stamp older than an hour ? 
+# in minutes
+GRACE=60
+is_old=$(find $RUNNING_FILE -mmin +$GRACE)
+if [ -n "$is_old" ] ; then
+    msg "$RUNNING_FILE is older than $GRACE minutes - removing"
     rm -f $RUNNING_FILE
 fi
 
+if [ -f $RUNNING_FILE ] ; then
+    msg "Found $RUNNING_FILE - another git-mirror seems to be running. Aborting... " 
+    exit 1
+fi
+
+trap failure ERR INT
 
+shift $((OPTIND-1))
+date > $RUNNING_FILE
+mirror $@
+rm -f $RUNNING_FILE