« blib/lib/Clustericious/Command/hypnotoad.pm
↑ Summary
blib/lib/Clustericious/RouteBuilder/CRUD.pm »
| 1 | package Clustericious::Command::stop; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Clustericious::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Clustericious::App; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use base 'Clustericious::Command'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Clustericious; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # ABSTRACT: Clustericious command to stop a Clustericious application | ||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | __PACKAGE__->attr(description => <<EOT); | |||||||||||||||||||||||||||
| 18 | Stop a running daemon. | ||||||||||||||||||||||||||||
| 19 | EOT | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | __PACKAGE__->attr(usage => <<EOT); | |||||||||||||||||||||||||||
| 22 | usage $0: stop | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Send a TERM signal to running daemon(s). | ||||||||||||||||||||||||||||
| 25 | See Clustericious::Command::Stop. | ||||||||||||||||||||||||||||
| 26 | EOT | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub _stop_pidfile | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_stop_pidfile: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 30 | 0 | my($pid_file, $signal) = @_; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | -e $pid_file or do { WARN "No pid file $pid_file\n"; return; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | TRACE "pid_file : $pid_file"; | |||||||||||||||||||||||||||
| 33 | 0 | my $pid = Clustericious::_slurp_pid $pid_file; # dies automatically | |||||||||||||||||||||||||||
| 34 | 0 | chomp $pid; | |||||||||||||||||||||||||||
| 35 | 0 | TRACE "file $pid_file has pid $pid"; | |||||||||||||||||||||||||||
| 36 | 0 | _stop_pid($pid, $signal); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | -e $pid_file or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | unlink $pid_file or WARN "Could not remove $pid_file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub _stop_pid | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_stop_pid: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 43 | 0 | my($pid, $signal) = @_; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | $signal //= 'QUIT'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$signal //= "QUIT"
MC/DC0%0/1
$signal //= "QUIT"
$signal
Truth table0%0/2
A
A= $signal
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless ($pid && $pid=~/^\d+$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pid and $pid =~ /^\d+$/
MC/DC0%0/2
$pid and $pid =~ /^\d+$/
$pid
$pid =~ /^\d+$/
Truth table0%0/3
A and B
A= $pidB= $pid =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 0 | WARN "Bad pid '$pid'. Not stopping process."; | |||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | kill 0, $pid or do { WARN "$pid is not running"; return; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | INFO "Sending $signal to $pid"; | |||||||||||||||||||||||||||
| 52 | 0 | kill $signal, $pid; | |||||||||||||||||||||||||||
| 53 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 54 | 0 | my $nap = 1; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | while (kill 0, $pid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | INFO "waiting for $pid"; | |||||||||||||||||||||||||||
| 58 | 0 | sleep $nap++; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if($nap > 5) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | INFO "pid $pid did not die on request, killing with signal 9"; | |||||||||||||||||||||||||||
| 62 | 0 | kill 9, $pid; | |||||||||||||||||||||||||||
| 63 | 0 | last; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub _stop_daemon | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_stop_daemon: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 70 | 0 | my($listen) = @_; | |||||||||||||||||||||||||||
| 71 | 0 | my $port = Mojo::URL->new($listen)->port; | |||||||||||||||||||||||||||
| 72 | 0 | my @got = `lsof -n -FR -i:$port`; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | return unless @got && $got[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@got and $got[0]
MC/DC0%0/2
@got and $got[0]
@got
$got[0]
Truth table0%0/3
A and B
A= @gotB= $got[0]
| |||||||||||||||||||||||||||||
| 74 | # Only the first one; others may be child processes | ||||||||||||||||||||||||||||
| 75 | 0 | my ($pid) = $got[0] =~ /^p(\d+)$/; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | unless ($pid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 0 | WARN "could not find pid for daemon on port $port"; | |||||||||||||||||||||||||||
| 79 | 0 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | TRACE "Stopping pid $pid"; | |||||||||||||||||||||||||||
| 82 | 0 | _stop_pid($pid); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub _stop_nginx | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_stop_nginx: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 87 | 0 | my %conf = @_; | |||||||||||||||||||||||||||
| 88 | 0 | my $prefix = $conf{'-p'}; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $cnf = $conf{'-c'} || ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$conf{'-c'} || ''
MC/DC0%0/1
$conf{'-c'} || ''
$conf{'-c'}
Truth table0%0/2
A
A= $conf{'-c'}
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $cnf = "-c $cnf" if $cnf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | INFO "stopping nginx in $prefix"; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | system("nginx -p $prefix $cnf -s quit")==0 or WARN "could not stop nginx"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub _stop_apache | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_stop_apache: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 97 | 0 | my %conf = @_; | |||||||||||||||||||||||||||
| 98 | 0 | _stop_pidfile($conf{pid_file},'TERM'); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub run | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 0 | my($self, @args) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | exit 2 unless $self->app->sanity_check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | $self->app->init_logging(); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $exe = $0; | |||||||||||||||||||||||||||
| 109 | 0 | for (reverse $self->app->config->start_mode) { | |||||||||||||||||||||||||||
| 110 | 0 | DEBUG "Stopping $_ server"; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | /hypnotoad/ and _stop_pidfile($self->app->config->hypnotoad->pid_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | /plackup/ and _stop_pidfile($self->app->config->plackup->pid_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | /lighttpd/ and _stop_pidfile($self->app->config->lighttpd->pid_file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | /daemon/ and _stop_daemon($self->app->config->daemon->listen); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | /nginx/ and _stop_nginx($self->app->config->nginx); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | /apache/ and _stop_apache($self->app->config->apache); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | 1; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||