Any-Daemon

 view release on metacpan or  search on metacpan

examples/net.pl  view on Meta::CPAN


my $mode     = 0;     # increase output

my %os_opts  =
  ( pid_file   => '/tmp/net.pid'  # usually in /var/run
  , user       => undef
  , group      => undef
  );

my %run_opts =
  ( background => 1
  , max_childs => 1    # there can only be one multiplexer
  );

my %net_opts =
  ( host       => 'localhost:5422'
  , port       => undef
  );

GetOptions
   'background|bg!' => \$run_opts{background}
 , 'childs|c=i'     => \$run_opts{max_childs}
 , 'group|g=s'      => \$os_opts{group}
 , 'host|h=s'       => \$net_opts{host}
 , 'pid-file|p=s'   => \$os_opts{pid_file}
 , 'port|p=s'       => \$net_opts{port}
 , 'user|u=s'       => \$os_opts{user}
 , 'v+'             => \$mode  # -v -vv -vvv
    or exit 1;

$run_opts{background} //= 1;

unless(defined $net_opts{port})
{   my $port = $net_opts{port} = $1
        if $net_opts{host} =~ s/\:([0-9]+)$//;
    defined $port or error __"no port specified";
}

#
## initialize the daemon activities
#

lib/Any/Daemon.pm  view on Meta::CPAN


    my $wd = $self->workdir;
    if($wd)
    {   -d $wd or mkdir $wd, 0700
            or fault __x"cannot create working directory {dir}", dir => $wd;

        chdir $wd
            or fault __x"cannot change to working directory {dir}", dir => $wd;
    }

    my $bg = exists $args{background} ? $args{background} : 1;
    if($bg)
    {   trace "backgrounding managing daemon";

        my $kid = fork;
        if($kid)
        {   # starting parent is ready to leave
            exit 0;
        }
        elsif(!defined $kid)
        {   fault __x"cannot start the managing daemon";
        }

lib/Any/Daemon.pod  view on Meta::CPAN

The C<run> method gets the activity related parameters.

You specify either C<run_task>, for the function to be called by this
deamon itself, or C<child_task> when you wish to manage child tasks
which run the action.

[0.96] When you pass a method name, it will be called on this object.
This is very clean when you have extended this daemon class.

 -Option     --Default
  background   <true>
  child_died   'childDied'
  child_task   undef
  kill_childs  'killChilds'
  max_childs   10
  reconfigure  'reconfigDaemon'
  run_task     undef

=over 2

=item background => BOOLEAN

Run the managing daemon in the background. During testing, it is
prefered to run the daemon in the foreground, to be able to stop
the daemon with Crtl-C and to see errors directly on the screen
in stead of only in some syslog file.

=item child_died => CODE|METHOD

The C<child_died> routine handles dieing kids and the restart of new
ones.  It gets two parameters: the maximum number of childs plus the
task to perform per kid.



( run in 1.401 second using v1.01-cache-2.11-cpan-f56aa216473 )