blib/lib/Nginx/Control.pm

filter: Statementstmt 42.7% 47 / 110 Branchbran 15.6% 5 / 32 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 47.6% 10 / 21 Podpod 100% 12 / 12 totaltotal 42.1% 75 / 178 CC 22 SCAR 22.8
CC
22
Cov
37%
_find_pid_file
30.0
stop
20.0
reload
20.0
CRAP 9.8
↑ Summary
1 package Nginx::Control;
2 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.04';
7 4 our $AUTHORITY = 'cpan:PERIGRIN';
8 4 our $NGINX_BIN = 'nginx';
9 4 our @SEARCH_PATH = qw( /usr /usr/local /usr/local/nginx /opt/local /opt/nginx /sw );
10  
11 4 has 'config_file' => (
12 is => 'rw',
13 isa => 'Path::Class::File',
14 coerce => 1,
15 );
16  
17 4 has 'prefix_path' => (
18 is => 'rw',
19 isa => 'Path::Class::Dir',
20 coerce => 1,
21 );
22  
23 4 has 'binary_path' => (
24 is => 'rw',
25 isa => 'Path::Class::File',
26 coerce => 1,
27 lazy => 1,
28 builder => '_find_binary_path'
29 );
30  
31 4 has 'pid_file' => (
32 is => 'rw',
33 isa => 'Path::Class::File',
34 coerce => 1,
35 lazy => 1,
36 builder => '_find_pid_file',
37 );
38  
39 4 has 'server_pid' => (
40 init_arg => undef,
41 is => 'ro',
42 isa => 'Int',
43 lazy => 1,
44 builder => '_find_server_pid',
45 );
46  
47 2 ▶sub log { shift; warn @_, "\n" }
Subroutine log: called CC=1 SCAR=0.0
Pod documented
48  
49 ## ---------------------------------
50 ## events
51  
52 2 ▶sub pre_startup { inner() }
Subroutine pre_startup: called CC=1 SCAR=0.0
Pod documented
53 0 ▶sub post_startup { inner() }
Subroutine post_startup: not called CC=1 SCAR=6.9
Pod documented
54  
55 0 ▶sub pre_shutdown { inner() }
Subroutine pre_shutdown: not called CC=1 SCAR=6.9
Pod documented
56 0 ▶sub post_shutdown { inner() }
Subroutine post_shutdown: not called CC=1 SCAR=6.9
Pod documented
57  
58 0 ▶sub pre_reload { inner() }
Subroutine pre_reload: not called CC=1 SCAR=6.9
Pod documented
59 0 ▶sub post_reload { inner() }
Subroutine post_reload: not called CC=1 SCAR=6.9
Pod documented
60  
61 ## ---------------------------------
62  
63 0 ▶sub _find_server_pid {
Subroutine _find_server_pid: not called CC=2 SCAR=17.9
64 0 my $self = shift;
65 0 my $pid = $self->pid_file->slurp( chomp => 1 );
66 0 ▶ ($pid)
Branch: 1 branch
BranchTrueFalseTotal
$pid or confess('No PID found in pid_file (' . $self->pid_file . ')') 0 0 0
67 || confess "No PID found in pid_file (" . $self->pid_file . ")";
68 0 $pid;
69 }
70  
71 0 ▶sub _find_pid_file {
Subroutine _find_pid_file: not called CC=5 SCAR=34.0
72 0 my $self = shift;
73  
74 0 my $config_file = $self->config_file;
75  
76 0 ▶ ( -f $config_file )
Branch: 1 branch
BranchTrueFalseTotal
-f $config_file or confess("Could not find pid_file because could not find config file ($config_file)") 0 0 0
77 || confess
78 "Could not find pid_file because could not find config file ($config_file)";
79  
80 # the two possible approaches to
81 # find the pid file (that I know of)
82 0 ▶ my @approaches = ( sub { $config_file->slurp( chomp => 1 ) }, );
Subroutine __ANON__: not called CC=1 SCAR=6.9
83  
84 0 foreach my $approach (@approaches) {
85 0 my @config = $approach->();
86 0 foreach my $line (@config) {
87 0 ▶ if ( $line =~ /^\s*pid\s+(.*);/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*pid\s+(.*);/) 0 0 0
88 0 return Path::Class::File->new($1);
89 }
90 }
91 }
92  
93 confess
94 0 "Could not locate the pid-file information, please supply it manually";
95 }
96  
97 6 ▶sub _find_binary_path {
Subroutine _find_binary_path: called CC=6 SCAR=18.2
98 6 my $self = shift;
99  
100 6 my $nginx = do {
101 6 my $bin = `which $NGINX_BIN`;
102 6 chomp($bin);
103 6 Path::Class::File->new($bin);
104 };
105  
106 6 ▶ return $nginx if -x $nginx;
Branch: 1 branch
BranchTrueFalseTotal
if -x $nginx 0 6 6
107  
108 6 for my $prefix (@SEARCH_PATH) {
109 21 for my $bindir (qw(bin sbin)) {
110 42 my $nginx = Path::Class::File->new( $prefix, $bindir, $NGINX_BIN );
111 42 ▶ return $nginx if -x $nginx;
Branch: 1 branch
BranchTrueFalseTotal
if -x $nginx 0 42 42
112 }
113 }
114  
115 6 ▶ confess "can't find nginx anywhere tried => ("
Condition50%1/2
$nginx || 'nothing'
l!l
60
MC/DC0%0/1
$nginx || 'nothing'
$nginx
Truth table50%1/2
A
A= $nginx
Aresult
11
00
116 . ( $nginx || 'nothing' ) . ")";
117 }
118  
119 2 ▶sub _construct_command_line {
Subroutine _construct_command_line: called CC=3 SCAR=13.0
120 2 my $self = shift;
121 2 my @opts = @_;
122 2 my $conf = $self->config_file;
123  
124 2 ▶ ( -f $conf )
Branch: 1 branch
BranchTrueFalseTotal
-f $conf or confess("Could not locate configuration file ($conf)") 1 1 2
125 || confess "Could not locate configuration file ($conf)";
126  
127 1 my @cli = ( $self->binary_path, @opts, '-c', $conf->stringify );
128 0 ▶ if ($self->prefix_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prefix_path) 0 0 0
129 0 push @cli, ( '-p', $self->prefix_path->stringify . "/" );
130 }
131  
132 0 return @cli;
133 }
134  
135 ## ---------------------------------
136  
137 1 ▶sub is_server_running {
Subroutine is_server_running: called CC=3 SCAR=15.4
Pod documented
138 1 my $self = shift;
139  
140 # no pid file, no server running ...
141 1 ▶ return 0 unless -s $self->pid_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $self->pid_file 1 0 1
142  
143 # has pid file, then check it ...
144 0 ▶ kill( 0, $self->server_pid ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
kill(0, $self->server_pid) ? : 0 0 0
145 }
146  
147 2 ▶sub start {
Subroutine start: called CC=2 SCAR=11.6
Pod documented
148 2 my $self = shift;
149  
150 2 $self->log("Starting nginx ...");
151 2 $self->pre_startup;
152  
153 # NOTE:
154 # do this after startup so that it
155 # would be possible to write the
156 # config file in the pre_startup
157 # hook if we wanted to.
158 # - SL
159 2 my @cli = $self->_construct_command_line;
160 0 $self->log( "Command-line: " . join(" ", @cli) );
161  
162 0 ▶ unless ( system(@cli) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (system(@cli) == 0) 0 0 0
163 0 $self->log("Could not start nginx (@cli) exited with status $?");
164 0 return;
165 }
166  
167 0 $self->post_startup;
168 0 $self->log("Nginx started.");
169 }
170  
171 0 ▶sub stop {
Subroutine stop: not called CC=4 SCAR=30.0
Pod documented
172 0 my $self = shift;
173 0 my $pid_file = $self->pid_file;
174  
175 0 ▶ if ( -f $pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $pid_file) 0 0 0
176  
177 0 ▶ if ( !$self->is_server_running ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_server_running) 0 0 0
178 0 $self->log(
179 "Found pid_file($pid_file), but process does not seem to be running."
180 );
181 0 return;
182 }
183  
184 0 $self->log("Stoping nginx ...");
185 0 $self->pre_shutdown;
186  
187 0 my @cli = $self->_construct_command_line(qw( -s stop ));
188 0 ▶ unless ( system(@cli) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (system(@cli) == 0) 0 0 0
189 0 kill 2, $self->server_pid;
190 }
191  
192 0 $self->post_shutdown;
193 0 $self->log("Nginx stopped.");
194  
195 0 return;
196 }
197  
198 0 $self->log("... pid_file($pid_file) not found.");
199 }
200  
201 0 ▶sub reload {
Subroutine reload: not called CC=4 SCAR=30.0
Pod documented
202 0 my $self = shift;
203 0 my $pid_file = $self->pid_file;
204  
205 0 ▶ unless ( -f $pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $pid_file) 0 0 0
206 0 $self->log("... pid_file($pid_file) not found.");
207 }
208  
209 0 ▶ unless ( $self->is_server_running ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_server_running) 0 0 0
210 0 $self->log(
211 "Found pid_file($pid_file), but process does not seem to be running."
212 );
213 0 return;
214 }
215  
216 0 $self->log("Reloading nginx ...");
217 0 $self->pre_reload;
218  
219 0 my @cli = $self->_construct_command_line(qw( -s reload ));
220 0 ▶ unless ( system(@cli) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (system(@cli) == 0) 0 0 0
221 0 $self->log("Failed to reload Nginx.");
222 }
223  
224 0 $self->post_reload;
225 0 $self->log("Nginx reloaded.");
226 }
227  
228 0 ▶sub test {
Subroutine test: not called CC=1 SCAR=6.9
Pod documented
229 0 my $self = shift;
230 0 my $pid_file = $self->pid_file;
231  
232 0 my @cli = $self->_construct_command_line("-t");
233  
234 0 return ( system(@cli) == 0 );
235 }
236  
237 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 4 1;
239  
240 __END__
↑ Summary