blib/lib/Plack/Loader/Restarter.pm

filter: Statementstmt 34.9% 22 / 63 Branchbran 12.5% 2 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 5 totaltotal 37.3% 40 / 107 CC 12 SCAR 22.5
CC
12
Cov
35%
run
30.0
_fork_and_start
12.0
_kill_child
6.0
CRAP 9.5
« blib/lib/HTTP/Server/PSGI.pm ↑ Summary blib/lib/Plack/Middleware/ErrorDocument.pm »
1 package Plack::Loader::Restarter;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw(Plack::Loader);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 1 my($class, $runner) = @_;
10 1 bless { watch => [] }, $class;
11 }
12  
13 0 ▶sub preload_app {
Subroutine preload_app: not called CC=1 SCAR=6.9
Pod undocumented
14 0 my($self, $builder) = @_;
15 0 $self->{builder} = $builder;
16 }
17  
18 0 ▶sub watch {
Subroutine watch: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my($self, @dir) = @_;
20 0 push @{$self->{watch}}, @dir;
21 }
22  
23 0 ▶sub _fork_and_start {
Subroutine _fork_and_start: not called CC=3 SCAR=24.8
24 0 my($self, $server) = @_;
25  
26 0 delete $self->{pid}; # re-init in case it's a restart
27  
28 0 my $pid = fork;
29 0 ▶ die "Can't fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
30  
31 0 ▶ if ($pid == 0) { # child
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 0 0 0
32 0 return $server->run($self->{builder}->());
33 } else {
34 0 $self->{pid} = $pid;
35 }
36 }
37  
38 0 ▶sub _kill_child {
Subroutine _kill_child: not called CC=2 SCAR=17.9
39 0 my $self = shift;
40  
41 0 ▶ my $pid = $self->{pid} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = $self->{"pid"} or return 0 0 0
42 0 warn "Killing the existing server (pid:$pid)\n";
43 0 kill 'TERM' => $pid;
44 0 waitpid($pid, 0);
45 }
46  
47 23 ▶sub valid_file {
Subroutine valid_file: called CC=4 SCAR=13.9
Pod undocumented
48 23 my($self, $file) = @_;
49  
50 # vim temporary file is 4913 to 5036
51 # http://www.mail-archive.com/vim_dev@googlegroups.com/msg07518.html
52 23 ▶ if ( $file->{path} =~ m{(\d+)$} && $1 >= 4913 && $1 <= 5036) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"path"} =~ /(\d+)$/ and $1 >= 4913 and $1 <= 5036) 2 21 23
Condition100%6/6
$file->{"path"} =~ /(\d+)$/ and $1 >= 4913 and $1 <= 5036
!ll&&!rl&&r
2012
$file->{"path"} =~ /(\d+)$/ and $1 >= 4913
!ll&&!rl&&r
1823
MC/DC100%3/3
$file->{"path"} =~ /(\d+)$/ and $1 >= 4913 and $1 <= 5036
$file->{"path"} =~ /(\d+)$/ $1 >= 4913 $1 <= 5036
Truth table100%4/4
A and B and C
A= $file->{"path"} =~ /(\d+)$/B= $1 >= 4913C= $1 <= 5036
ABCresult
0XX0
10X0
1100
1111
53 2 return 0;
54 }
55 21 $file->{path} !~ m!^\.(?:git|svn)[/\\]|\.(?:bak|swp|swpx|swx)$|~$|_flymake\.p[lm]$|\.#!;
56 }
57  
58 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
59 0 my($self, $server) = @_;
60  
61 0 $self->_fork_and_start($server);
62 0 ▶ return unless $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 0 0
63  
64 0 require Filesys::Notify::Simple;
65 0 my $watcher = Filesys::Notify::Simple->new($self->{watch});
66 0 warn "Watching @{$self->{watch}} for file updates.\n";
67 0 ▶ local $SIG{TERM} = sub { $self->_kill_child; exit(0); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
68  
69 0 while (1) {
70 0 my @restart;
71  
72 # this is blocking
73 0 ▶ $watcher->wait(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
74 0 my @events = @_;
75 0 @events = grep $self->valid_file($_), @events;
76 0 ▶ return unless @events;
Branch: 1 branch
BranchTrueFalseTotal
unless @events 0 0 0
77  
78 0 @restart = @events;
79 0 });
80  
81 0 ▶ next unless @restart;
Branch: 1 branch
BranchTrueFalseTotal
unless @restart 0 0 0
82  
83 0 for my $ev (@restart) {
84 0 warn "-- $ev->{path} updated.\n";
85 }
86  
87 0 $self->_kill_child;
88 0 warn "Successfully killed! Restarting the new server process.\n";
89 0 $self->_fork_and_start($server);
90 0 ▶ return unless $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 0 0
91 }
92 }
93  
94 2 1;
95  
96 __END__
« blib/lib/HTTP/Server/PSGI.pm ↑ Summary blib/lib/Plack/Middleware/ErrorDocument.pm »