Commits

Anonymous committed 56b90e7

Use lockfile for update lock mechanisms

Comments (0)

Files changed (2)

slave/etc/rc.d/rc.vlbuildslave-update

 _LOCKFILE=/tmp/vlbuildbot-updated.lock
 
 function start() {
-	 echo $$ > $_LOCKFILE
+	lockfile $_LOCKFILE	 
 	 if [ -f $_PIDFILE ]; then
 	    kill -9 $(cat $_PIDFILE)
 	 fi
 }
 function stop() {
 	 rm -rf $_LOCKFILE
+	 sleep 1
 	 if [ -f $_PIDFILE ]; then
 	 	 kill -9 $(cat $_PIDFILE)
 		 rm -f $_PIDFILE
 }
 function restart() {
 	 stop
-	 sleep 1
 	 start
 }
 function status() {

slave/usr/local/bin/vlbuildbot-updated.sh

 function update_slave_info() {
 	# Reveal the local version for the slave on the slave info page.
 	infofile=$SLAVEPATH/info/host
+	log "Updating slave's info/host file"
+	if [ ! -r $infofile ]; then
+		log "Unable to read $infofile... will not update it"
+		return 0
+	fi
 	if [[ "x$(grep Please $infofile)" != "x" ]]; then
 		# replace this line
 		sed -i "s|^\(Please.*\)||g" $infofile
 
 function restart_slave() {
     log "Starting slave"
-	exec /etc/rc.d/rc.vlbuildslave restart
+    	if [ -x /etc/rc.d/rc.vlbuildslave ]; then
+		log "Starting slave"
+		exec /etc/rc.d/rc.vlbuildslave restart
+	else
+		log "Slave service is disabled... will not restart it"
+	fi
 }
 
 # The main update procedure
 function do_update() {
     log "Begin requested update"
     # Create semaphore
-    echo $$ > $UPDATELOCK
+    # every 30 seconds attempt to get this lock.  When acquired, the lock is
+    # only good for 4 hours.  Give up after 2 attempts.
+    lockfile -30 -l 14400 -r 2 $UPDATELOCK
+    log "Aquired lock $UPDATELOCK  for 14400 seconds"
     stop_slave
     update_slave # Run the function from update.conf
     set_localversion_to_remoteversion
     update_slave_info
     restart_slave
     # Remove the update lock
-    log "Update complete"
     rm -rf $UPDATELOCK
-    /etc/rc.d/rc.vlbuildslave-update restart
+    log "Update complete.  Discarding $UPDATELOCK" || log "Unable to remove $LOCKFILE"
 }
 
-function update_daemon() {
-# This is the function that runs at all times and checks if we need an update
-    if [ -f $LOCKFILE ]; then
-	echo "$LOCKFILE found ... Starting update service for vlbuildbot slave client"
-    while [ -f $LOCKFILE ]; do
-	refresh_git_tree
-	source $CONF
-	source $UPDATE
-	if [ $(check_update_needed) != "0" ]; then
-	    log "Detected available update"
-	# Check to make sure an update is not running yet.
-	if [ ! -f $UPDATELOCK ]; then
-	    echo $$ > $UPDATELOCK
-	    do_update
-	    rm -rf $UPDATELOCK
-    else
-	    echo "Removing possible stale locks (older than 5 hours)"
-	    find /tmp/ -type f -name $(basename $UPDATELOCK) -mmin +300 -exec rm -rf {} \;
-	    echo "$UPDATELOCK exists... cannot proceed with update until it is discarded"
-	fi
-	fi
-	sleep $INTERVAL
-     done
-    log "Stopping daemon"
-    rm -f $LOCKFILE
-    fi
+function daemonize {
+	while [ -f $LOCKFILE ]; do
+		refresh_git_tree
+		source $CONF
+		source $UPDATE
+		# Determine if an update is needed
+		if [ $(check_update_needed) != "0" ]; then
+			log "Detected available update"
+			do_update # This function should manage its own lock
+			rm -rf $UPDATELOCK # Just in case
+		fi
+		sleep $INTERVAL
+	done
+	log "Stopping daemon"
+	# Remove the lock file
+	rm -f $LOCKFILE
 }
 
 # Start daemon mode
-update_daemon
+daemonize
+#update_daemon