remove File::Flock and File::Slurp infavor of File::Pid
[gitmo/MooseX-Daemonize.git] / lib / MooseX / Daemonize.pm
index 6bdcadc..925a786 100644 (file)
@@ -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);
 
@@ -22,6 +22,13 @@ has progname => (
     },
 );
 
+has basedir => (
+    isa     => 'Str',
+    is      => 'ro',
+    lazy    => 1,
+    default => sub { return '/' },
+);
+
 has pidbase => (
     isa => 'Str',
     is  => 'ro',
@@ -31,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',
     },
 );
 
@@ -50,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;
@@ -80,44 +86,19 @@ sub start {
     open( NULL, '/dev/null' );
     <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;
-    $self->kill($pid) unless $self->foreground();
+    $self->_kill($pid) unless $self->foreground();
     $self->remove_pid;
     return 1 if $args{no_exit};
     exit;
@@ -125,7 +106,7 @@ sub stop {
 
 sub restart {
     my ($self) = @_;
-    $self->stop( noexit => 1 );
+    $self->stop( no_exit => 1 );
     $self->start();
 }
 
@@ -138,7 +119,8 @@ sub setup_signals {
 sub handle_sigint { $_[0]->stop; }
 sub handle_sighup { $_[0]->restart; }
 
-sub kill {
+sub _kill {
+    confess "_kill isn't public" unless caller eq __PACKAGE__;
     my ( $self, $pid ) = @_;
     return unless $pid;
     unless ( CORE::kill 0 => $pid ) {
@@ -163,7 +145,7 @@ sub kill {
 
     unless ( CORE::kill 0 => $pid or $!{EPERM} ) {    # IF it is still running
         CORE::kill( 9, $pid );                        # finally try SIGKILL
-        sleep(2) if CORE::kill( 0, $pid );
+        sleep(3) if CORE::kill( 0, $pid );
     }
 
     unless ( CORE::kill 0 => $pid or $!{EPERM} ) {    # IF it is still running
@@ -271,21 +253,27 @@ Setup the signal handlers, by default it only sets up handlers for SIGINT and SI
 
 =item handle_sigint()
 
-Handle a INT signal, by default calls C<$self->stop()>;
+Handle a INT signal, by default calls C<$self->stop()>
 
 =item handle_sighup()
 
-Handle a HUP signal. Nothing is done by default.
+Handle a HUP signal. By default calls C<$self->restart()>
 
 =item get_pid
 
+Lookup the pid from our pidfile.
+
 =item save_pid
 
+Save the current pid in our pidfile
+
 =item remove_pid
 
+Delete our pidfile
+
 =item meta()
 
-the C<meta()> method from L<Class::MOP::Class>
+The C<meta()> method from L<Class::MOP::Class>
 
 =back
 
@@ -297,7 +285,7 @@ the C<meta()> method from L<Class::MOP::Class>
     the module is part of the standard Perl distribution, part of the
     module's distribution, or must be installed separately. ]
 
-Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Flock>, L<File::Slurp>
+Obviously L<Moose>, also L<Carp>, L<Proc::Daemon>, L<File::Pid>
 
 =head1 INCOMPATIBILITIES
 
@@ -336,6 +324,9 @@ L<Proc::Daemon>, L<Daemon::Generic>, L<MooseX::Getopt>
 
 Chris Prather  C<< <perigrin@cpan.org> >>
 
+=head1 THANKS
+
+Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, and the #moose denzians
 
 =head1 LICENCE AND COPYRIGHT