From: Chris Prather Date: Thu, 26 Jul 2007 23:27:15 +0000 (+0000) Subject: remove File::Flock and File::Slurp infavor of File::Pid X-Git-Tag: 0.02~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fa2b72a40f356bef1bcd64dee82cd81e56960c81;p=gitmo%2FMooseX-Daemonize.git remove File::Flock and File::Slurp infavor of File::Pid --- diff --git a/Makefile.PL b/Makefile.PL index b0e09dd..ff7aab9 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,8 +9,7 @@ all_from 'lib/MooseX/Daemonize.pm'; build_requires 'Test::More' => 0; requires 'Proc::Daemon' => 0; requires 'Carp' => 0; -requires 'File::Flock' => 0; -requires 'File::Slurp' => 0; +requires 'File::Pid' => 0; requires 'MooseX::Getopt' => 0; requires 'Moose' => 0.20; diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index 920b16e..925a786 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -2,12 +2,12 @@ package MooseX::Daemonize; use strict; # because Kwalitee is pedantic use Moose::Role; -our $VERSION = 0.01; +our $VERSION = 0.01_1; use Carp; use Proc::Daemon; -use File::Flock; -use File::Slurp; +use File::Pid; +use Moose::Util::TypeConstraints; with qw(MooseX::Getopt); @@ -38,14 +38,27 @@ has pidbase => ( default => sub { return '/var/run' }, ); +subtype 'Pidfile' => as 'Object' => where { $_->isa('File::Pid') }; +coerce 'Pidfile' => from 'Str' => via { + File::Pid->new( { file => $_, } ); +}; + has pidfile => ( - isa => 'Str', + isa => 'Pidfile', is => 'ro', lazy => 1, required => 1, + coerce => 1, default => sub { die 'Cannot write to ' . $_[0]->pidbase unless -w $_[0]->pidbase; - $_[0]->pidbase . '/' . $_[0]->progname . '.pid'; + my $file = $_[0]->pidbase . '/' . $_[0]->progname . '.pid'; + File::Pid->new( { file => $file } ); + }, + handles => { + check => 'running', + save_pid => 'write', + remove_pid => 'remove', + get_pid => 'pid', }, ); @@ -57,20 +70,6 @@ has foreground => ( default => sub { 0 }, ); -sub check { - my ($self) = @_; - if ( my $pid = $self->get_pid ) { - my $prog = $self->progname; - if ( CORE::kill 0 => $pid ) { - croak "$prog already running ($pid)."; - } - carp "$prog not running but found pid ($pid)." - . "Perhaps the pid file (@{ [$self->pidfile] }) is stale?"; - return 1; - } - return 0; -} - sub daemonize { my ($self) = @_; Proc::Daemon::Init; @@ -87,43 +86,15 @@ sub start { open( NULL, '/dev/null' ); if (0); ## use critic - + # Change to basedir chdir $self->basedir; - + $self->save_pid; $self->setup_signals; return $$; } -sub save_pid { - my ($self) = @_; - my $pidfile = $self->pidfile; - lock( $pidfile, undef, 'nonblocking' ) - or croak "Could not lock PID file $pidfile: $!"; - write_file( $pidfile, "$$\n" ); - unlock($pidfile); - return; -} - -sub remove_pid { - my ($self) = @_; - my $pidfile = $self->pidfile; - lock( $pidfile, undef, 'nonblocking' ) - or croak "Could not lock PID file $pidfile: $!"; - unlink($pidfile); - unlock($pidfile); - return; -} - -sub get_pid { - my ($self) = @_; - my $pidfile = $self->pidfile; - return unless -e $pidfile; - chomp( my $pid = read_file($pidfile) ); - return $pid; -} - sub stop { my ( $self, %args ) = @_; my $pid = $self->get_pid; @@ -314,7 +285,7 @@ The C method from L the module is part of the standard Perl distribution, part of the module's distribution, or must be installed separately. ] -Obviously L, also L, L, L, L +Obviously L, also L, L, L =head1 INCOMPATIBILITIES diff --git a/lib/Test/MooseX/Daemonize.pm b/lib/Test/MooseX/Daemonize.pm index a11c1bf..6b79933 100644 --- a/lib/Test/MooseX/Daemonize.pm +++ b/lib/Test/MooseX/Daemonize.pm @@ -33,7 +33,7 @@ sub daemonize_ok { else { sleep(1); # Punt on sleep time, 1 seconds should be enough $Test->ok( -e $daemon->pidfile, $msg ) - || $Test->diag( 'Pidfile (' . $daemon->pidfile . ') not found.' ); + || $Test->diag( 'Pidfile (' . $daemon->pidfile->file . ') not found.' ); } }