From: Don Armstrong Date: Sat, 27 May 2006 07:02:46 +0000 (-0700) Subject: * Add a cron method to add_bug_to_estraier to enable easily running X-Git-Tag: release/2.6.0~585^2^2~103^2~14 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=c88923e000a7036276237537c0e0c3fbd21f8a4b;p=debbugs.git * Add a cron method to add_bug_to_estraier to enable easily running it in a cronjob --- diff --git a/bin/add_bug_to_estraier b/bin/add_bug_to_estraier index e27f8782..048708bc 100755 --- a/bin/add_bug_to_estraier +++ b/bin/add_bug_to_estraier @@ -91,6 +91,7 @@ use Debbugs::MIME qw(create_mime_message); use Search::Estraier; use Debbugs::Estraier qw(:add); use File::Find; +use File::stat; use vars qw($DEBUG $VERBOSE); @@ -127,7 +128,7 @@ else { chomp; next if /^\s*\#/; my ($key,$value) = split /\s*[:=]\s*/,$_,2; - $options{$key} = $value if not defined $options{$key}; + $options{$key} = $value if defined $key and not defined $options{$key}; } $ERRORS .= "url must be set\n" if not defined $options{url}; $ERRORS .= "user must be set\n" if not defined $options{user}; @@ -143,6 +144,7 @@ pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; +$Debbugs::Estraier::DEBUG = $DEBUG; $VERBOSE = 0; my $node = new Search::Estraier::Node (url => $options{url}, @@ -154,7 +156,9 @@ $Debbugs::Config::gSpoolDir = $options{spool} if defined $options{spool}; if ($options{cron}) { my %timestamps; my $start_time = time; + my $unlink = 0; my %seen_dirs; + check_pid($options{timestamp}); # read timestamp file if (defined $options{timestamp}) { my $timestamp_fh = new IO::File $options{timestamp},'r' or @@ -165,30 +169,40 @@ if ($options{cron}) { $timestamps{$key} = $value; } } - find(sub { - print STDERR "Examining $_\n" if $DEBUG; - return if not /^(\d+)\.log$/; - my $bug_num = $1; - my $stat = stat $_ or next; - return unless -f _; - return if exists $timestamps{$File::Find::dir} and - $timestamps{$File::Find::dir} > $stat->mtime; - $seen_dirs{$File::Find::dir} = $start_time; - add_bug_log($node,$1); - }, - map {(-d "$options{spool}/$_")?"$options{spool}/$_":()} - qw(db-h archived db), - ); - # write timestamp file - if (defined $options{timestamp}) { - %timestamps = (%timestamps,%seen_dirs); - my $timestamp_fh = new IO::File $options{timestamp},'w' or - die "Unable to open timestamp $options{timestamp}: $!"; - foreach my $key (keys %timestamps) { - print {$timestamp_fh} $key,' ', - $timestamps{$key}||'',qq(\n); + for my $hash (map {sprintf '%02d',$_ } 0..99) { + find(sub { + print STDERR "Examining $_\n" if $DEBUG > 1; + return if not /^(\d+)\.log$/; + my $bug_num = $1; + my $stat = stat $_ or next; + return unless -f _; + return if exists $timestamps{$File::Find::dir} and + ($timestamps{$File::Find::dir} > $stat->mtime); + $seen_dirs{$File::Find::dir} = $start_time; + print STDERR "Adding $bug_num\n" if $DEBUG; + eval{ + add_bug_log($node,$bug_num); + }; + if ($@) { + print STDERR "Adding $bug_num failed with $@\n"; + } + }, + map {(-d "$options{spool}/$_/$hash")? + "$options{spool}/$_/$hash":()} + qw(db-h archive), + ); + # write timestamp file + if (defined $options{timestamp}) { + %timestamps = (%timestamps,%seen_dirs); + my $timestamp_fh = new IO::File $options{timestamp},'w' or + die "Unable to open timestamp $options{timestamp}: $!"; + foreach my $key (keys %timestamps) { + print {$timestamp_fh} $key,' ', + $timestamps{$key}||'',qq(\n); + } } } + unlink("$options{timestamp}.pid"); } else { while (my $bug_num = ) { @@ -198,5 +212,20 @@ else { } +sub check_pid{ + my ($timestamp) = @_; + if (-e "${timestamp}.pid") { + my $time_fh = new IO::File "${timestamp}.pid", 'r' or die "Unable to read pidfile"; + local $/; + my $pid = <$time_fh>; + if (kill(0,$pid)) { + print STDERR "Another cron is running" and exit 0; + } + } + my $time_fh = new IO::File "${timestamp}.pid", 'w' or + die "Unable to read pidfile"; + print {$time_fh} $$; +} + __END__