Toadfarm/Command/start.pm

filter: Statementstmt 90.6% 58 / 64 Branchbran 73.3% 22 / 30 Conditioncond 59.0% 13 / 22 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 78.3% 112 / 143 CC 23 SCAR 15.2
CC
23
Cov
80%
_end
8.5
_start
6.2
_pid
5.2
CRAP 4.5
« Toadfarm/Command/stop.pm ↑ Summary Toadfarm/Command/reload.pm »
1 package Toadfarm::Command::start;
2 6 ▶use Mojo::Base 'Toadfarm::Command::tail';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Time::HiRes 'usleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶$ENV{TOADFARM_VERBOSE} //= $ENV{HARNESS_IS_VERBOSE} || -t STDOUT;
Condition33%2/6
$ENV{"TOADFARM_VERBOSE"} //= $ENV{"HARNESS_IS_VERBOSE"} || -t STDOUT
l!l&&r!l&&!r
060
$ENV{'HARNESS_IS_VERBOSE'} || -t STDOUT
l!l&&r!l&&!r
006
MC/DC0%0/3
$ENV{"TOADFARM_VERBOSE"} //= $ENV{"HARNESS_IS_VERBOSE"} || -t STDOUT
$ENV{"TOADFARM_VERBOSE"} $ENV{'HARNESS_IS_VERBOSE'} -t STDOUT
Truth table0%0/4
A //= B || C
A= $ENV{"TOADFARM_VERBOSE"}B= $ENV{'HARNESS_IS_VERBOSE'}C= -t STDOUT
ABCresult
1XX1
01X1
0011
0000
9  
10 6 has description => 'Toadfarm: Start the server if not already running';
11  
12 4 ▶sub run { shift->_start(@_) }
Subroutine run: called CC=1 SCAR=0.0
Pod documented
13  
14 9 ▶sub _hypnotoad {
Subroutine _hypnotoad: called CC=3 SCAR=11.1
15 9 my $self = shift;
16  
17 9 for my $p (File::Spec->path, dirname($^X)) {
18 36 my $exe = File::Spec->catfile($p, 'hypnotoad');
19 36 ▶ return $exe if -r $exe;
Branch: 1 branch
BranchTrueFalseTotal
if -r $exe 9 27 36
20 }
21  
22 0 die "Cannot find 'hypnotoad' in \$PATH.";
23 }
24  
25 40 ▶sub _is_running {
Subroutine _is_running: called CC=2 SCAR=7.0
26 40 my $self = shift;
27 40 my $pid = $self->_pid;
28 40 ▶ return $pid && kill 0, $pid;
Condition66%2/3
$pid && kill(0, $pid)
!ll&&!rl&&r
11029
MC/DC50%1/2
$pid && kill(0, $pid)
$pid kill(0, $pid)
Truth table66%2/3
A && B
A= $pidB= kill(0, $pid)
ABresult
0X0
100
111
29 }
30  
31 13 ▶sub _end {
Subroutine _end: called CC=6 SCAR=21.4
32 13 my ($self, $exit, $message) = @_;
33  
34 13 ▶ if ($message and $ENV{TOADFARM_VERBOSE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message and $ENV{'TOADFARM_VERBOSE'}) { } 0 13 13
Condition66%2/3
$message and $ENV{"TOADFARM_VERBOSE"}
!ll&&!rl&&r
850
MC/DC0%0/2
$message and $ENV{"TOADFARM_VERBOSE"}
$message $ENV{"TOADFARM_VERBOSE"}
Truth table66%2/3
A and B
A= $messageB= $ENV{"TOADFARM_VERBOSE"}
ABresult
0X0
100
111
35 0 ▶ $self->_printf(" ...%s (%s)\n", $exit ? "fail!" : "done.", $message);
Branch: 1 branch
BranchTrueFalseTotal
$exit ? : 0 0 0
36 }
37 else {
38 13 ▶ $self->_printf(" ...%s\n", $exit ? "fail!" : "done.");
Branch: 1 branch
BranchTrueFalseTotal
$exit ? : 6 7 13
39 }
40  
41 13 ▶ return $exit if $ENV{TOADFARM_NO_EXIT};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TOADFARM_NO_EXIT"} 13 0 13
42 0 exit $exit;
43 }
44  
45 13 ▶sub _log_daemon_msg {
Subroutine _log_daemon_msg: called CC=1 SCAR=0.0
46 13 my ($self, $message) = @_;
47 13 $self->_printf(" * $message\n", $self->app->moniker);
48 }
49  
50 55 ▶sub _pid {
Subroutine _pid: called CC=5 SCAR=16.5
51 55 my $self = shift;
52 55 my $file = $self->_pid_file;
53 54 ▶ return 0 unless $file and -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -e $file 19 35 54
Condition66%2/3
$file and -e $file
!ll&&!rl&&r
01935
MC/DC50%1/2
$file and -e $file
$file -e $file
Truth table66%2/3
A and B
A= $fileB= -e $file
ABresult
0X0
100
111
54 35 ▶ open my $PID, '<', $file or die "Unable to read pid_file $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $PID, "<", $file or die "Unable to read pid_file ${file}: $!\n" 35 0 35
55 35 my $pid = join '', <$PID>;
56 35 ▶ return $pid =~ /(\d+)/ ? $1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pid =~ /(\d+)/u ? : 35 0 35
57 }
58  
59 62 ▶sub _pid_file {
Subroutine _pid_file: called CC=2 SCAR=6.9
60 62 my $self = shift;
61 62 my $moniker = $self->app->moniker;
62 return $self->app->config->{hypnotoad}{pid_file}
63 62 ▶ || die "$moniker has invalid config: /hypnotoad/pid_file is not set.\n";
Condition100%2/2
$self->app->config->{'hypnotoad'}{'pid_file'} || die("$moniker has invalid config: /hypnotoad/pid_file is not set.\n")
l!l
593
MC/DC100%1/1
$self->app->config->{'hypnotoad'}{'pid_file'} || die("$moniker has invalid config: /hypnotoad/pid_file is not set.\n")
$self->app->config->{'hypnotoad'}{'pid_file'}
Truth table100%2/2
A
A= $self->app->config->{'hypnotoad'}{'pid_file'}
Aresult
11
00
64 }
65  
66 sub _printf { shift; printf shift, @_; }
67  
68 6 ▶sub _start {
Subroutine _start: called CC=6 SCAR=18.3
69 6 my ($self, @args) = @_;
70 6 my $pid = $self->_pid;
71 0 my $tail = grep {/^--tail/} @args;
72  
73 5 $self->_log_daemon_msg('Starting the process %s');
74  
75 # running
76 5 ▶ if ($self->_is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_running) 1 4 5
77 0 ▶ return $self->_tail(grep { !/^--tail/ } @args) if $tail;
Branch: 1 branch
BranchTrueFalseTotal
if $tail 0 1 1
78 1 return $self->_end(0, "already running with pid=$pid");
79 }
80  
81 # start
82 4 system $self->_hypnotoad, $0;
83 4 my $exit = $? >> 8;
84 4 ▶ return $self->_end($exit, "hypnotoad $0 = $exit") if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 2 2 4
85  
86 # wait until started
87 2 ▶ $self->_wait_for(5 => sub { shift->_is_running });
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 # check if started
90 2 ▶ return $self->_end(3, 'daemon is not running') unless $self->_is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_running 1 1 2
91 0 ▶ return $self->_tail(grep { !/^--tail/ } @args) if $tail;
Branch: 1 branch
BranchTrueFalseTotal
if $tail 0 1 1
92 1 return $self->_end(0);
93  
94 }
95  
96 5 ▶sub _wait_for {
Subroutine _wait_for: called CC=5 SCAR=16.2
97 5 my ($self, $cb) = (shift, pop);
98 5 ▶ my $timeout = shift || ($self->app->config->{hypnotoad}{graceful_timeout} || 20) + 5;
Condition60%3/5
shift() || ($self->app->config->{'hypnotoad'}{'graceful_timeout'} || 20) + 5
l!l&&r!l&&!r
230
$self->app->config->{'hypnotoad'}{'graceful_timeout'} || 20
l!l
03
MC/DC50%1/2
shift() || ($self->app->config->{'hypnotoad'}{'graceful_timeout'} || 20) + 5
shift() $self->app->config->{'hypnotoad'}{'graceful_timeout'}
Truth table66%2/3
A || B
A= shift()B= $self->app->config->{'hypnotoad'}{'graceful_timeout'}
ABresult
1X1
011
000
99  
100 5 ▶ while ($timeout--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout--) 57 3 60
101 57 ▶ last if $self->$cb;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$cb 2 55 57
102 }
103 continue {
104 55 usleep(300e3);
105 }
106 }
107  
108 6 1;
109  
110 =encoding utf8
111  
112 =head1 NAME
113  
114 Toadfarm::Command::start - Start a Toadfarm DSL script
115  
116 =head1 DESCRIPTION
117  
118 L<Toadfarm::Command::start> is a command for starting a L<Toadfarm> application.
119  
120 =head1 SYNOPSIS
121  
122 $ /path/to/script.pl start
123 $ /path/to/script.pl start --tail <args>
124  
125 =head1 ATTRIBUTES
126  
127 =head2 description
128  
129 Short description of command, used for the command list.
130  
131 =head1 METHODS
132  
133 =head2 run
134  
135 Run command.
136  
137 =head1 COPYRIGHT AND LICENSE
138  
139 Copyright (C) 2014, Jan Henning Thorsen
140  
141 This program is free software, you can redistribute it and/or modify it under
142 the terms of the Artistic License version 2.0.
143  
144 =head1 AUTHOR
145  
146 Jan Henning Thorsen - C<jhthorsen@cpan.org>
147  
148 =cut
« Toadfarm/Command/stop.pm ↑ Summary Toadfarm/Command/reload.pm »