| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($pid == 0) { # child | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$file->{"path"} =~ /(\d+)$/ and $1 >= 4913 and $1 <= 5036
$file->{"path"} =~ /(\d+)$/ and $1 >= 4913
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | @restart = @events; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | next unless @restart; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||