MooseX::Control - Simple class to manage a execute deamon
package XXXX::Control; use Moose; with 'MooseX::Control'; has '+control_name' => ( default => 'xxxx' ); sub pre_startup { inner() } sub post_startup { inner() } sub pre_shutdown { inner() } sub post_shutdown { inner() } sub get_server_pid { } sub construct_command_line { } sub find_pid_file { }
It is a Moose Role to ease writing XXX::Control like Sphinx::Control, Perlbal::Control
Please view source code for more details.
http://search.cpan.org/dist/Sphinx-Control/lib/Sphinx/Control.pm
http://search.cpan.org/dist/Perlbal-Control/lib/Perlbal/Control.pm
has '+control_name' => ( default => 'perlbal' ); # or has '+control_name' => ( default => 'searchd' );
To find a pid file for control_name
if the pid file is optional for control_name like perlbal, we return
Path::Class::File->new('/tmp/unknown.pid')
system command for start.
sub construct_command_line { my $self = shift; my $conf = $self->config_file; (-f $conf) || confess "Could not locate configuration file ($conf)"; ($self->binary_path, '--daemon', '--config', $conf->stringify); }
a pid number for contorl_name
if $self->pid_file is there, we general write like:
sub get_server_pid { my $self = shift; my $pid = $self->pid_file->slurp(chomp => 1); ($pid) || confess "No PID found in pid_file (" . $self->pid_file . ")"; $pid; }
if no $pid_file, we may use Proc::ProcessTable.
sub get_server_pid { my $self = shift; my $pid_file = $self->pid_file; if ( $pid_file ) { my $pid = $pid_file->slurp(chomp => 1); ($pid) || confess "No PID found in pid_file (" . $pid_file . ")"; return $pid; } else { my $config_file = $self->config_file->stringify; my $control_name = $self->control_name; my $p = new Proc::ProcessTable( 'cache_ttys' => 1 ); my $all = $p->table; foreach my $one (@$all) { if ($one->cmndline =~ /$control_name/ and $one->cmndline =~ /$config_file/) { return $one->pid; } } } return 0; }
return a Path::Class::File of execute file like /usr/bin/search or /usr/bin/perlbal
controls $self->debug
Checks to see if the control_name deamon that is currently being controlled by this instance is running or not (based on the state of the PID).
Starts the control_name deamon that is currently being controlled by this instance. It will also run the pre_startup and post_startup hooks.
Stops the control_name deamon that is currently being controlled by this instance. It will also run the pre_shutdown and post_shutdown hooks.
Moose, MooseX::Types::Path::Class, Sphinx::Control, Perlbal::Control
Fayland Lam, <fayland at gmail.com>
<fayland at gmail.com>
Copyright 2008 Fayland Lam
except for those parts that are
Copyright 2008 Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MooseX::Control, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Control
CPAN shell
perl -MCPAN -e shell install MooseX::Control
For more information on module installation, please visit the detailed CPAN module installation guide.