MidCOM svn: r14375 - branches/MidCOM_2_8/midcom.core/support

rambo midcom-commits at lists.midgard-project.org
Fri Jan 11 14:39:14 CET 2008


Author: rambo
Date: Fri Jan 11 14:39:14 2008
New Revision: 14375
URL: http://trac.midgard-project.org/software/changeset/14375

Log:
lockfile freshnes checking

Modified:
   branches/MidCOM_2_8/midcom.core/support/midcom_services_cron.sh

Modified: branches/MidCOM_2_8/midcom.core/support/midcom_services_cron.sh
==============================================================================
--- branches/MidCOM_2_8/midcom.core/support/midcom_services_cron.sh	(original)
+++ branches/MidCOM_2_8/midcom.core/support/midcom_services_cron.sh	Fri Jan 11 14:39:14 2008
@@ -16,8 +16,12 @@
 # alternatively use elinks, you also specify other options here
 # like "lynx -auth=username:password" for HTTP basic-auth
 LYNX="lynx" 
+STAT="stat -c %Z" # how to get file timestamp
+DATE="date +%s" # how to get system timestamp
+LIFETIME=3600 # Max pidfile age in seconds before it's considered stale
 
-if [ "$1" == "" ]; then
+if [ "$1" == "" ]
+then
     URL_SUFFIX=""
     PIDFILE="/var/run/midcom_services_cron_everymin.pid"
     PIDFILE2=""
@@ -27,23 +31,52 @@
     PIDFILE2="/var/run/midcom_services_cron_everymin.pid"
 fi  
 
-# TODO: figure out a locking mechanism that ensures only one instance
-# is running at a time, without missing any valid runs
+function check_pidfile
+{
+    if [ -f $1 ]
+    then
+        MODIFIED=`$STAT $1`
+        NOW=`$DATE`
+        AGE=`expr $NOW - $MODIFIED`
+        #echo "MODIFIED=$MODIFIED NOW=$NOW AGE=$AGE LIFETIME=$LIFETIME"
+        if [ "$AGE" -lt "$LIFETIME" ]
+        then
+            return 1
+        fi
+        echo "Lock $1 is stale, removing"
+        # PONDER: Should we kill the process ??, the line below should do the trick
+        # kill -9 `cat $1`
+        rm -f $1
+        return 0
+    fi
+    return 0
+}
 
 # Make sure we only have one copy of the type running at a given time
-test -f $PIDFILE && exit 1
+check_pidfile $PIDFILE
+PIDSTAT=$?
+if [  "$PIDSTAT" != "0" ]
+then
+    exit 1
+fi
 echo $$ > $PIDFILE
 # Wait untill everymin script has completed if it's running
-if [ "$PIDFILE2" != "" ]; then
-    if [ -f $PIDFILE2 ]; then
-        while [ -f $PIDFILE2 ]; do
-            sleep 10
-        done
-    fi
+if [ "$PIDFILE2" != "" ]
+then
+    check_pidfile $PIDFILE2
+    PIDSTAT2=$?
+    while [  "$PIDSTAT2" != "0" ]
+    do
+        sleep 10
+        check_pidfile $PIDFILE2
+        PIDSTAT2=$?
+    done
 fi
 
-for SITE in $SITES; do
-    if [ `expr match "$SITE" 'https*'` -eq 0 ]; then
+for SITE in $SITES
+do
+    if [ `expr match "$SITE" 'https*'` -eq 0 ]
+    then
         URL="http://$SITE/midcom-exec-midcom/cron.php$URL_SUFFIX"
     else
         URL="$SITE/midcom-exec-midcom/cron.php$URL_SUFFIX"


More information about the midcom-commits mailing list