NAME

MooseX::Control - Simple class to manage a execute deamon

SYNOPSIS

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 { }

DESCRIPTION

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

REQUIRED ATTRIBUTES AND METHODS

ATTRIUTES

control_name

has '+control_name' => ( default => 'perlbal' );
# or
has '+control_name' => ( default => 'searchd' );

METHODS

find_pid_file

To find a pid file for b<control_name>

if the pid file is optional for b<control_name> like perlbal, we return

return Path::Class::File->new();

construct_command_line

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);
}

get_server_pid

a pid number for b<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;
             }
         }
     }
     confess "No PID found in pid_file (" . $pid_file . ")";
 }

PROVIDED ATTRIBUTES AND METHODS

ATTRIBUTES

binary_path

return a Path::Class::File of execute file like /usr/bin/search or /usr/bin/perlbal

verbose

control $self->debug

METHODS

is_server_running

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).

start

Starts the control_name deamon that is currently being controlled by this instance. It will also run the pre_startup and post_startup hooks.

stop

Stops the control_name deamon that is currently being controlled by this instance. It will also run the pre_shutdown and post_shutdown hooks.

SEE ALSO

Moose, MooseX::Types::Path::Class, Sphinx::Control, Perlbal::Control

AUTHOR

Fayland Lam, <fayland at gmail.com>

COPYRIGHT & LICENSE

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.