App-Sv
view release on metacpan or search on metacpan
lib/App/Sv.pm view on Meta::CPAN
$svc->{state} = 'restart';
$debug->("Restarting '$svc->{name}' in $svc->{restart_delay} seconds");
my $t; $t = AE::timer $svc->{restart_delay}, 0, sub {
$self->_start_svc($svc);
undef $t;
};
}
sub _check_svc_up {
my ($self, $svc) = @_;
return unless $svc->{state} eq 'start';
if (!$svc->{pid}) {
$svc->{state} = 'fail';
return;
}
delete $svc->{start_count};
$svc->{state} = 'up';
}
sub _stop_svc {
my ($self, $svc) = @_;
$svc->{state} = 'stop';
my $st = $self->_signal_svc($svc, 'TERM');
if ($svc->{stop_wait} && $svc->{stop_wait} > 0) {
my $t; $t = AE::timer $svc->{stop_wait}, 0, sub {
$self->_check_svc_down($svc);
undef $t;
};
}
return $st;
}
sub _check_svc_down {
my ($self, $svc) = @_;
return unless $svc->{state} eq 'stop';
if ($svc->{pid}) {
my $st = $self->_signal_svc($svc, 'KILL');
}
}
sub _signal_svc {
my ($self, $svc, $sig) = @_;
return unless ($svc->{pid} && $sig);
my $debug = $self->{log}->logger(8);
$debug->("Sent signal $sig to pid $svc->{pid}");
my $st = kill($sig, $svc->{pid});
return $st;
}
sub _signal_all_svc {
my ($self, $sig, $cv) = @_;
my $debug = $self->{log}->logger(8);
$debug->("Received signal $sig");
my $is_any_alive = 0;
foreach my $key (keys %{ $self->{run} }) {
my $svc = $self->{run}->{$key};
next unless my $pid = $svc->{pid};
$debug->("... sent signal $sig to pid $pid");
$is_any_alive++;
kill($sig, $pid);
}
return if $cv and $is_any_alive;
$debug->('Exiting...');
$cv->send if $cv;
}
# Contolling socket
sub _listener {
my $self = shift;
my $debug = $self->{log}->logger(8);
my ($host, $port) = parse_hostport($self->{conf}->{listen});
croak "Socket \'$port\' already in use" if ($host eq 'unix/' && -e $port);
$self->{server} = tcp_server $host, $port,
sub { $self->_client_conn(@_) },
sub {
my ($fh, $host, $port) = @_;
$debug->("Listening at $host:$port");
};
}
sub _client_conn {
my ($self, $fh, $host, $port) = @_;
return unless $fh;
my $debug = $self->{log}->logger(8);
$debug->("New connection to $host:$port");
my $hdl; $hdl = AnyEvent::Handle->new(
fh => $fh,
timeout => 30,
rbuf_max => 64,
wbuf_max => 64,
on_read => sub { $self->_client_input($hdl) },
on_eof => sub { $self->_client_disconn($hdl) },
on_timeout => sub { $self->_client_error($hdl, undef, 'Timeout') },
on_error => sub { $self->_client_error($hdl, undef, $!) }
);
$self->{conn}->{fileno($fh)} = $hdl;
return $fh;
}
sub _client_input {
my ($self, $hdl) = @_;
$hdl->push_read(line => sub {
my ($hdl, $ln) = @_;
my $client = $self->{conn}->{fileno($hdl->fh)};
my $cmds = $self->{cmds};
if ($ln) {
# generic commands
$hdl->push_write("\n");
if ($ln =~ /^(\.|quit)$/) {
$self->_client_disconn($hdl);
}
elsif ($ln eq 'status') {
$self->_status($hdl);
}
( run in 1.924 second using v1.01-cache-2.11-cpan-df04353d9ac )