[Midnightbsd-cvs] mports: magus.pl: Try some basic locking to keep two or more workers

ctriv at midnightbsd.org ctriv at midnightbsd.org
Mon Sep 15 14:34:40 EDT 2008


Log Message:
-----------
Try some basic locking to keep two or more workers from updating the
machine's run at the same time.

Modified Files:
--------------
    mports/Tools/magus/slave:
        magus.pl (r1.23 -> r1.24)

-------------- next part --------------
Index: magus.pl
===================================================================
RCS file: /home/cvs/mports/Tools/magus/slave/magus.pl,v
retrieving revision 1.23
retrieving revision 1.24
diff -L Tools/magus/slave/magus.pl -L Tools/magus/slave/magus.pl -u -r1.23 -r1.24
--- Tools/magus/slave/magus.pl
+++ Tools/magus/slave/magus.pl
@@ -43,6 +43,7 @@
 use POSIX qw(setsid);
 use Getopt::Std qw(getopts);
 use File::Path qw(rmtree);
+use Fcntl qw(:flock);
 
 $SIG{INT} = sub { report('info', "$$: caught sigint"); die "Caught SIGINT $$\n" };
 
@@ -451,7 +452,27 @@
   my $current = Magus::Run->latest($Magus::Machine) || return;
   my $tree_id = get_tree_id("$Magus::Config{'SlaveDataDir'}/mports") || 0;
 
+  # we need up update the mports tree to match the new run, but we need
+  # to make sure that no other worker is using it, and that we have an
+  # exclusive lock to change it.
   if ($current != $Magus::Machine->run || $tree_id != $current) {
+    return if Magus::Lock->search(machine => $Magus::Machine);
+    
+    my $lockfile = "$Magus::Config{SlaveDataDir}/mports.lock";
+    
+    # we need to make sure that no other process is updating the mports dir.
+    open(my $lock, '>>', $lockfile) || die "Couldn't open $lockfile: $!\n";
+    unless (flock($lock, LOCK_EX|LOCK_NB)) {
+      report(debug => "Unable to lock mports directory (perhaps another process is updating it?)");
+      # this will block until the file is unlocked.
+      flock($lock, LOCK_EX) || die "Couldn't lock $lockfile: $!\n";
+      
+      report(debug => "Reloading self.");
+      exec($self, @origARGV);
+  
+      return;
+    }
+    
     $Magus::Machine->run($current);
     $Magus::Machine->update;
     


More information about the Midnightbsd-cvs mailing list