Changeset 83

Show
Ignore:
Timestamp:
02/21/10 22:51:43 (9 years ago)
Author:
depesz
Message:

simplify pid handling using ready made module

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/omnipitr/lib/OmniPITR/Program.pm

    r80 r83  
    44use OmniPITR::Log; 
    55use English qw( -no_match_vars ); 
     6use Proc::Pidfile; 
    67use Carp; 
    78 
     
    1112    $self->read_args(); 
    1213    $self->validate_args(); 
    13     $self->check_pidfile(); 
     14    $self->{'pid-file'} = Proc::Pidfile->new( 'pidfile' => $self->{ 'pid-file' } ) if $self->{'pid-file'}; 
     15 
    1416    return $self; 
    1517} 
     
    1820    my $self = shift; 
    1921    croak("run() method in OmniPITR::Program was not overridden!"); 
    20 } 
    21  
    22 sub check_pidfile { 
    23     my $self = shift; 
    24     return unless defined $self->{'pid-file'}; 
    25  
    26     my $pidfile = $self->{'pid-file'}; 
    27  
    28     if ( -e $pidfile ) { 
    29         open( my $fh, '<', $pidfile ) or $self->log->fatal( 'Pidfile (%s) exists, but cannot be opened: %s', $pidfile, $OS_ERROR ); 
    30         my $old_pid = <$fh>; 
    31         close $fh; 
    32         $self->log->fatal('Pidfile (%s) exists, but contains unexpected value: {{%s}}', $pidfile, $old_pid) unless $old_pid =~ s/\A(\d{1,5})\s*\z/$1/; 
    33         $self->log->fatal('Previous copy of %s seems to be still running. PID: %d', $PROGRAM_NAME, $old_pid) if kill(0, $old_pid ); 
    34     } 
    35     open my $fh, '>', $pidfile or $self->log->fatal( 'Cannot open pidfile (%s) for writing: %s', $pidfile, $OS_ERROR); 
    36     print $fh $PROCESS_ID; 
    37     close $fh; 
    38     return; 
    3922} 
    4023