From: Chris Prather Date: Tue, 11 Sep 2007 09:15:23 +0000 (+0000) Subject: add a lab directory X-Git-Tag: 0.02~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cbff8e5216c7e752ed8f2e64e0f200e1d3c9c718;p=gitmo%2FMooseX-Daemonize.git add a lab directory r27545@alice-3: perigrin | 2007-08-02 17:43:53 -0500 different Daemonize support r27566@alice-3: perigrin | 2007-08-03 19:18:12 -0500 add trunk r27567@alice-3: perigrin | 2007-08-03 19:18:27 -0500 move everything into trunk r28827@alice-3: perigrin | 2007-09-11 03:33:57 -0500 add in the changes for dec and bump the version number ... it's RELEASE TIME BABY r28836@alice-3: perigrin | 2007-09-11 03:40:40 -0500 trying to merge in manually changes that lack metadata r28837@alice-3: perigrin | 2007-09-11 04:14:41 -0500 merging changes I didn't make down --- diff --git a/Changes b/Changes index 3cb135e..2424740 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for MooseX-Daemonize 0.02 ??? + - Switch to File::Pid - Add the GetOpt prereq - Adjust the kill timings - Added THANKS to pod diff --git a/lab/dec.pl b/lab/dec.pl new file mode 100755 index 0000000..0d143e2 --- /dev/null +++ b/lab/dec.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl -l + +package TestDaemon; +use Moose; +with('MooseX::Daemonize'); + +before 'daemonize' => sub { + warn 'forking ' . $$; +}; + +after 'start' => sub { + return unless $_[0]->is_daemon; + while (1) { + local *LOG; + open LOG, '>>', '/tmp/testdaemon.log'; + print LOG "$0:$$"; + close LOG; + sleep 1; + } +}; + +package main; +my $td = new_with_options TestDaemon( pidbase => '/tmp' ); +use YAML; +warn Dump $td->pidfile; +warn $td->check; +print "PARENT: $$"; +print 'PID: ' . $td->get_pid; +print $td->start; diff --git a/lib/MooseX/Daemonize.pm b/lib/MooseX/Daemonize.pm index 925a786..2a12ce8 100644 --- a/lib/MooseX/Daemonize.pm +++ b/lib/MooseX/Daemonize.pm @@ -2,7 +2,7 @@ package MooseX::Daemonize; use strict; # because Kwalitee is pedantic use Moose::Role; -our $VERSION = 0.01_1; +our $VERSION = 0.02; use Carp; use Proc::Daemon; @@ -23,33 +23,33 @@ has progname => ( ); has basedir => ( - isa => 'Str', - is => 'ro', - lazy => 1, - default => sub { return '/' }, + isa => 'Str', + is => 'ro', + required => 1, + lazy => 1, + default => sub { return '/' }, ); has pidbase => ( - isa => 'Str', - is => 'ro', - - # required => 1, - lazy => 1, - default => sub { return '/var/run' }, + isa => 'Str', + is => 'ro', + lazy => 1, + required => 1, + default => sub { return '/var/run' }, ); subtype 'Pidfile' => as 'Object' => where { $_->isa('File::Pid') }; -coerce 'Pidfile' => from 'Str' => via { - File::Pid->new( { file => $_, } ); -}; + +coerce 'Pidfile' => from 'Str' => via { File::Pid->new( { file => $_, } ); }; has pidfile => ( - isa => 'Pidfile', - is => 'ro', - lazy => 1, - required => 1, - coerce => 1, - default => sub { + isa => 'Pidfile', + is => 'rw', + lazy => 1, + required => 1, + coerce => 1, + predicate => 'has_pidfile', + default => sub { die 'Cannot write to ' . $_[0]->pidbase unless -w $_[0]->pidbase; my $file = $_[0]->pidbase . '/' . $_[0]->progname . '.pid'; File::Pid->new( { file => $file } ); @@ -59,28 +59,40 @@ has pidfile => ( save_pid => 'write', remove_pid => 'remove', get_pid => 'pid', + _pidfile => 'file', }, ); has foreground => ( - metaclass => 'Getopt', - cmd_aliases => ['f'], + metaclass => 'MooseX::Getopt::Meta::Attribute', + cmd_aliases => 'f', isa => 'Bool', is => 'ro', default => sub { 0 }, ); +has is_daemon => ( + isa => 'Bool', + is => 'rw', + default => sub { 0 }, +); + sub daemonize { my ($self) = @_; + return if Proc::Daemon::Fork; Proc::Daemon::Init; + $self->is_daemon(1); } sub start { my ($self) = @_; - return if $self->check; - + confess "instance already running" if $self->check; $self->daemonize unless $self->foreground; + return unless $self->is_daemon; + + $self->pidfile->pid($$); + # Avoid 'stdin reopened for output' warning with newer perls ## no critic open( NULL, '/dev/null' ); @@ -200,7 +212,7 @@ This module helps provide the basic infrastructure to do that. =item progname Str -The name of our daemon, defaults to $0 +The name of our daemon, defaults to $self->meta->name =~ s/::/_/; =item pidbase Str