Browse Source

Refactoring pid lock code into LXR::Shell and adding for xref and search

13 years ago
parent
commit
68267152f2
4 changed files with 23 additions and 17 deletions
  1. 18 1
      lib/LXR/Shell.pm
  2. 2 2
      update-search.pl
  3. 1 14
      update-src.pl
  4. 2 0
      update-xref.pl

+ 18 - 1
lib/LXR/Shell.pm

@@ -1,8 +1,9 @@
 package LXR::Shell;
+use Fcntl;
 
 require Exporter;
 @ISA = qw(Exporter);
-@EXPORT = qw(&check_defaults);
+@EXPORT = qw(&check_defaults &get_lock);
 
 sub check_defaults {
   my ($defaultshash) = @_;
@@ -22,3 +23,19 @@ sub check_defaults {
   }
 }
 
+sub get_lock {
+  my ($db_dir, $task) = @_;
+  my $pid_lock = "$db_dir/update-$task.pid";
+  sysopen(PID, $pid_lock, O_RDWR | O_CREAT) ||
+    die "could not open lock file $pid_lock";
+  my $pid = <PID>;
+  if (defined $pid) {
+    chomp $pid;
+    die "update $task process is probably already running as pid $pid\n" if (kill 0, $pid);
+  }
+  seek(PID, 0, 0) ||
+    die "could not rewind lock file $pid_lock";
+  print PID $$;
+  close PID;
+  return $pid_lock;
+}

+ 2 - 2
update-search.pl

@@ -131,9 +131,8 @@ $ENV{'PATH'} = "$possible_path:$ENV{'PATH'}" if -d $possible_path;
 }
 
 mkdir $db_dir unless -d $db_dir;
+my $pid_lock = get_lock($db_dir, 'xref');
 $log = "$db_dir/glimpseindex.log";
-
-mkdir $db_dir unless -d $db_dir;
 #exec > $log 2>&1
 #XXX what does |set -x| mean?
 #system ("set -x > $log");
@@ -255,4 +254,5 @@ $UPTIME
 ");
 system ("$UPTIME >> $log") if $UPTIME =~ /\w/;
 
+unlink $pid_lock;
 exit 0;

+ 1 - 14
update-src.pl

@@ -5,7 +5,6 @@
 
 use Cwd;
 use File::Basename;
-use Fcntl;
 use lib 'lib';
 use LXR::Common;
 use LXR::Config;
@@ -185,19 +184,7 @@ for my $possible_path (keys %pathmap) {
 }
 
 -d $db_dir || mkdir $db_dir;
-my $pid_lock = "$db_dir/update-src.pid";
-sysopen(PID, $pid_lock, O_RDWR | O_CREAT) ||
-  die "could not open lock file $db_dir/update-src.pid";
-my $pid = <PID>;
-if (defined $pid) {
-  chomp $pid;
-  die "update process is probably already running as pid $pid\n" if (kill 0, $pid);
-}
-seek(PID, 0, 0) ||
-  die "could not rewind lock file $db_dir/update-src.pid";
-print PID $$;
-close PID;
-
+my $pid_lock = get_lock($db_dir, 'src');
 my $log="$db_dir/cvs.log";
 
 rename $log, "$log.old" if -f $log;

+ 2 - 0
update-xref.pl

@@ -135,6 +135,7 @@ unless (defined $src_dir) {
 }
 
 do_mkdir $db_dir;
+my $pid_lock = get_lock($db_dir, 'xref');
 $log = "$db_dir/genxref.log";
 
 #exec > $log 2>&1
@@ -218,4 +219,5 @@ chdir "../..";
 system ("$DATE >> $log");
 system ("$UPTIME >> $log") if $UPTIME =~ /\w/;
 
+unlink $pid_lock;
 exit 0;