| Server-Control documentation | Contained in the Server-Control distribution. |
Server::Control::NetServer -- apachectl style control for Net::Server servers
version 0.15
package My::Server;
use base qw(Net::Server);
sub process_request {
#...code...
}
---
use Server::Control::NetServer;
my $ctl = Server::Control::NetServer->new(
net_server_class => 'My::Server',
net_server_params => {
pid_file => '/path/to/server.pid',
port => 5678,
log_file => '/path/to/file.log'
}
);
if ( !$ctl->is_running() ) {
$ctl->start(...);
}
Server::Control::NetServer is a subclass of
Server::Control for Net::Server servers.
The constructor options are as described in Server::Control, except for:
Required. Specifies a Net::Server subclass. Will be loaded if not already.
Specifies a hashref of parameters to pass to the server's run() method.
Will be taken from net_server_params.
Will be taken from net_server_params.
If not provided, will attempt to get from log_file key in
net_server_params.
This software is copyright (c) 2011 by Jonathan Swartz.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
| Server-Control documentation | Contained in the Server-Control distribution. |
package Server::Control::NetServer; BEGIN { $Server::Control::NetServer::VERSION = '0.15'; } use Carp; use Moose; use MooseX::StrictConstructor; use strict; use warnings; extends 'Server::Control'; has 'net_server_class' => ( is => 'ro', isa => 'Str', required => 1 ); has 'net_server_params' => ( is => 'ro', isa => 'HashRef', default => sub { {} } ); # All of this hackery is to skip the port check on start during a HUP, # because Net::Server leaves the sockets open. # has 'in_hup' => ( is => 'ro' ); before '_perform_cli_action' => sub { push( @ARGV, '--in-hup' ) if !( grep { $_ eq '--in-hup' } @ARGV ); }; around '_listening_before_start' => sub { my $orig = shift; my $self = shift; return $self->in_hup() ? 0 : $self->$orig(@_); }; __PACKAGE__->meta->make_immutable(); sub _cli_option_pairs { my $class = shift; return ( $class->SUPER::_cli_option_pairs, 'in-hup' => 'in_hup', ); } sub _build_port { my $self = shift; return $self->net_server_params->{port} || die "port must be passed in net_server_params"; } sub _build_pid_file { my $self = shift; return $self->net_server_params->{pid_file} || die "pid_file must be passed in net_server_params"; } sub _build_error_log { my $self = shift; my $server_log_file = $self->net_server_params->{log_file}; return ( defined($server_log_file) && $server_log_file ne 'Sys::Syslog' ) ? $server_log_file : undef; } sub do_start { my $self = shift; # Fork child. Child will fork again to start server, and then exit in # Net::Server::post_configure. Parent continues with rest of # Server::Control::start() to see if the server has started correctly # and report status. # my $child = fork; croak "Can't fork: $!" unless defined($child); if ( !$child ) { Class::MOP::load_class( $self->net_server_class ); $self->net_server_class->run( background => 1, %{ $self->net_server_params } ); exit(0); # Net::Server should exit, but just to be safe } } 1;
__END__