blib/lib/Clustericious/Command/status.pm

filter: Statementstmt 40.5% 30 / 74 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 75.0% 9 / 12 Podpod 100% 1 / 1 totaltotal 28.3% 40 / 141 CC 26 SCAR 54.5
CC
26
Cov
25%
run
420.0
_check_pidfile
30.0
_check_database
12.0
CRAP 232.3
« blib/lib/Clustericious/RouteBuilder/Search.pm ↑ Summary blib/lib/Clustericious/Client.pm »
1 package Clustericious::Command::status;
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 Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Clustericious::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Clustericious::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base 'Clustericious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Clustericious command to report status of Clustericious application
14 1 our $VERSION = '1.29'; # VERSION
15  
16  
17 1 __PACKAGE__->attr(description => <<'');
18 Report the status of a daemon.
19  
20 1 __PACKAGE__->attr(usage => <<"");
21 usage: $0 status
22 Report the status of a clustericious daemon.
23  
24 sub _check_pidfile
25 0 ▶{
Subroutine _check_pidfile: not called CC=5 SCAR=34.0
26 0 my($filename) = @_;
27 0 ▶ return ( state => 'error', message => 'missing pid filename' ) unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
28 0 ▶ return ( state => 'down', message => 'no pid file' ) unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
29 0 my $pid = Mojo::Asset::File->new(path => $filename)->slurp;
30 0 ▶ return ( state => 'down', messasge => 'no pid in file' ) unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
31 0 ▶ return ( state => 'ok' ) if kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 0 0 0
32 0 return ( state => 'down', message => "Pid $pid in file is not running." );
33 }
34  
35 sub _check_database
36 0 ▶{
Subroutine _check_database: not called CC=3 SCAR=24.8
37 0 my($db_class) = @_;
38 0 my $db = $db_class->new_or_cached;
39 0 my ( $domain, $type ) = ( $db->default_domain, $db->default_type );
40 0 my $dbh = $db->dbh;
41 0 my ( $state, $message );
42 0 ▶ if ($dbh)
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) { } 0 0 0
43 {
44 0 $state = 'ok';
45 0 ▶ ($message) = join ':', grep {defined && length} $dbh->selectrow_array('select current_database(), inet_server_addr(), inet_server_port()');
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
46 }
47 else
48 {
49 0 $state = 'down';
50 0 $message = $db_class->name;
51 }
52 return {
53 0 name => "database",
54 state => $state,
55 message => "$domain:$type $message",
56 };
57 }
58  
59 sub run
60 0 ▶{
Subroutine run: not called CC=20 SCAR=60.4
Pod documented
61 0 my($self, @args) = @_;
62 0 ▶ exit 2 unless $self->app->sanity_check;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app->sanity_check 0 0 0
63 0 my $app = $ENV{MOJO_APP};
64 0 my $conf = $self->app->config;
65  
66 0 eval "require $app";
67 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
68  
69 0 my @status; # array of { name =>.., state =>.., message =>.. } hashrefs.
70  
71 0 my $exe = $0;
72 # webserver
73 0 for ($self->app->config->start_mode)
74 {
75 0 ▶ push @status, { name => $_,
Branch: 4 branches
BranchTrueFalseTotal
/hypnotoad/ ? : 0 0 0
/apache/ ? : 0 0 0
/plackup/ ? : 0 0 0
/lighttpd/ ? : 0 0 0
76 (
77 # THIS IS A MESS PLEASE CLEAN IT UP
78 /hypnotoad/ ? _check_pidfile($conf->hypnotoad->pid_file(default => dirname($exe).'/hypnotoad.pid'))
79 : /apache/ ? _check_pidfile($conf->apache->pid_file)
80 : /plackup/ ? _check_pidfile($conf->plackup->pidfile)
81 # NB: see http://redmine.lighttpd.net/issues/2137
82 # lighttpd's pid files disappear. Time to switch to nginx?
83 : /lighttpd/ ? _check_pidfile($conf->lighttpd->pid_file)
84 : ( state => 'error', message => "Status for start_mode $_ is unimplemented." ))};
85 }
86  
87 # Do a HEAD request if the webserver(s) are ok.
88 0 ▶ if ((grep {$_->{state} eq 'ok'} @status)==@status)
Branch: 1 branch
BranchTrueFalseTotal
if (grep({$_->{"state"} eq "ok";} @status) == @status) 0 0 0
89 {
90 0 my $res = Mojo::UserAgent->new->head($conf->url)->res;
91 0 ▶ printf "%10s : %-10s (%s %s)\n", "url", $conf->url, $res->code || '?', $res->message || '';
Condition0%0/4
$res->code || '?'
l!l
00
$res->message || ''
l!l
00
MC/DC0%0/1
$res->code || '?'
$res->code
MC/DC0%0/1
$res->message || ''
$res->message
Truth table0%0/2
A
A= $res->code
Aresult
11
00
Truth table0%0/2
A
A= $res->message
Aresult
11
00
92 }
93  
94 # Database
95 0 ▶ if ( $INC{'Rose/Planter/DB.pm'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Rose/Planter/DB.pm"}) 0 0 0
96 {
97 0 ▶ if ( my $db_class = Rose::Planter::DB->registered_by($app) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $db_class = "Rose::Planter::DB"->registered_by($app)) 0 0 0
98 {
99 0 push @status, _check_database($db_class);
100 }
101 }
102  
103 0 my $ok;
104  
105 # Send as YAML if requested?
106 0 for (@status)
107 {
108 0 ▶ $_->{message} &&= "($_->{message})";;
Condition0%0/2
$_->{"message"} &&= qq[($_->{"message"})]
!ll
00
MC/DC0%0/1
$_->{"message"} &&= qq[($_->{"message"})]
$_->{"message"}
Truth table0%0/2
A
A= $_->{"message"}
Aresult
00
11
109 0 ▶ $_->{message} ||= "";
Condition0%0/2
$_->{"message"} ||= ""
l!l
00
MC/DC0%0/1
$_->{"message"} ||= ""
$_->{"message"}
Truth table0%0/2
A
A= $_->{"message"}
Aresult
11
00
110 0 printf "%10s : %-10s %s\n", @$_{qw/name state message/};
111 0 ▶ $ok //= 1 if $_->{state} eq 'ok';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"state"} eq "ok" 0 0 0
Condition0%0/2
$ok //= 1
l!l
00
MC/DC0%0/1
$ok //= 1
$ok
Truth table0%0/2
A
A= $ok
Aresult
11
00
112 0 ▶ $ok = 0 if $_->{state} ne 'ok';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"state"} ne "ok" 0 0 0
113 }
114
115 0 ▶ exit($ok ? 0 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
116 }
117  
118 1 1;
119  
120 __END__
« blib/lib/Clustericious/RouteBuilder/Search.pm ↑ Summary blib/lib/Clustericious/Client.pm »