blib/lib/App/DuckPAN/Restart.pm

filter: Statementstmt 29.3% 22 / 75 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 1 totaltotal 22.7% 28 / 123 CC 20 SCAR 33.6
CC
20
Cov
21%
run_restarter
72.0
__ANON__
30.0
__ANON__
20.0
CRAP 28.9
« blib/lib/App/DuckPAN/Cmd/Publisher.pm ↑ Summary blib/lib/App/DuckPAN/WebPublisher.pm »
1 package App::DuckPAN::Restart;
2 1 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: Automatic restarting of application on file change
4 1 $App::DuckPAN::Restart::VERSION = '1021';
5 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Filesys::Notify::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use App::DuckPAN::TemplateDefinitions;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 requires '_run_app';
16  
17 0 ▶sub run_restarter {
Subroutine run_restarter: not called CC=8 SCAR=42.8
Pod undocumented
18 0 my ($self, $args) = @_;
19  
20 # exit immediately if not in an IA directory
21 0 $self->app->get_ia_type;
22  
23 # will keep (re)starting the server until the app exits
24 0 while(1){
25 0 ▶ defined(my $app = fork) or die 'Failed to fork application';
Branch: 1 branch
BranchTrueFalseTotal
defined(my $app = fork) or die "Failed to fork application" 0 0 0
26 0 ▶ unless($app){ # app kid
Branch: 1 branch
BranchTrueFalseTotal
unless ($app) 0 0 0
27 0 $self->_run_app($args);
28 0 exit 0;
29 }
30  
31 # Slightly different format here since we need to take care of
32 # the newly spawned app on failure.
33 0 my $fmon = fork;
34 0 ▶ unless($fmon){ # file monitor kid
Branch: 1 branch
BranchTrueFalseTotal
unless ($fmon) 0 0 0
35 0 ▶ unless(defined $fmon){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fmon) 0 0 0
36 0 kill SIGTERM => $app;
37 0 die 'Failed to fork file monitor';
38 }
39 0 $self->_monitor_directories;
40 0 exit 0;
41 }
42  
43 # wait for one them to exit. -1 waits for all children
44 0 my $pid = waitpid -1, 0;
45  
46 # reload the application
47 0 ▶ if($pid == $fmon){
Branch: 2 branches
BranchTrueFalseTotal
if ($pid == $fmon) { } 0 0 0
elsif ($pid == $app) { } 0 0 0
48 # if we can't kill the app, let's not start another
49 0 ▶ unless(kill SIGTERM => $app){
Branch: 1 branch
BranchTrueFalseTotal
unless (kill "SIGTERM", $app) 0 0 0
50 0 die "Failed to kill the application (pid: $app). Check manually";
51 }
52 # wait for it, otherwise the next whie loop will get it
53 0 waitpid($app, 0);
54 }
55 elsif($pid == $app){ # or exit
56 0 kill SIGTERM => $fmon;
57 0 exit;
58 }
59 0 else{ die "Unknown kid $pid reaped!\n"; } # shouldn't happen
60 }
61 }
62  
63 0 ▶sub _get_directories_to_monitor {
Subroutine _get_directories_to_monitor: not called CC=3 SCAR=24.8
64 0 my $self = shift;
65 0 my @output_dirs;
66  
67 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
68 0 my $template_defs = App::DuckPAN::TemplateDefinitions->new;
69 0 my @templates = $template_defs->get_templates;
70  
71 0 for my $template (@templates) {
72 0 ▶ push @output_dirs, $template->output_directory
Branch: 1 branch
BranchTrueFalseTotal
unless $template->name =~ /test/ 0 0 0
73 unless $template->name =~ /test/;
74 }
75 }
76 0 ▶ catch {
Subroutine __ANON__: not called CC=4 SCAR=30.0
77 0 ▶ if (/template definitions/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/template definitions/i) { } 0 0 0
78 # There was a problem loading the template definitions file. This
79 # can happen if the instant answer repository is of an older
80 # version and does not have the templates.yml file.
81 #
82 # In this case we use the older method of determining directories
83 # to monitor. This support can be removed sometime in the future.
84 # Note: Cheatsheet directories will not be monitored with this
85 # method.
86  
87 0 $self->app->emit_debug("Instant Answers repository does not have a " .
88 "template definitions file. Some directories may not be monitored " .
89 "for changes.");
90  
91 0 ▶ while(my ($type, $io) = each %{$self->app->get_ia_type()->{templates}}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->app->get_ia_type->{"templates"};}) 0 0 0
92 0 ▶ next if $type eq 'test'; # skip the test dir?
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "test" 0 0 0
93 0 push @output_dirs, $io->{out};
94 }
95 }
96 else {
97 0 die $_;
98 }
99 0 };
100  
101 0 my %distinct_dirs;
102  
103 # add all subdirectories
104 0 for my $dir (@output_dirs) {
105 0 ++$distinct_dirs{$_} for File::Find::Rule->directory()->in($dir);
106 }
107  
108 0 ++$distinct_dirs{$self->app->get_ia_type()->{dir}};
109  
110 0 return [ keys %distinct_dirs ];
111 }
112  
113 # Monitors development directories for file changes. Tries to get the
114 # list of directories in a general way. This subroutine
115 # blocks, so when it returns we know there's been a change
116 0 ▶sub _monitor_directories {
Subroutine _monitor_directories: not called CC=2 SCAR=17.9
117 0 my $self = shift;
118  
119 # Find all of the directories that neeed to monitored
120 # Note: Could potentially be functionality added to App::DuckPAN
121 # which would return the directories involved in an IA
122 # (see https://github.com/duckduckgo/p5-app-duckpan/issues/200)
123 0 my $dirs = $self->_get_directories_to_monitor;
124  
125 0 FSMON: while(1){
126 # Find all subdirectories
127 # Create our watcher with each directory
128 0 my $watcher = Filesys::Notify::Simple->new($dirs);
129 # Wait for something to happen. This blocks, which is why
130 # it's in a wheel. On detection of update it will fall
131 # through; thus the while(1)
132 0 my $reload;
133 0 ▶ $watcher->wait(sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
134 0 for my $event (@_) {
135 0 my $file = $event->{path};
136 # if it's a newly created directory, dot file, or pulled
137 # in dynamically there shouldn't be a need to reload.
138 # This will catch directories with files in them properly,
139 # as each file will be its own event
140 0 ▶ if( (-d $file) || ($file =~ m{^(?:.+/)?\.[^/]+$}o) || ($file =~ /\.(?:handlebars|css|js)$/oi) ){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file or $file =~ m[^(?:.+/)?\.[^/]+$]o or $file =~ /\.(?:handlebars|css|js)$/io) 0 0 0
Condition0%0/6
-d $file or $file =~ m[^(?:.+/)?\.[^/]+$]o or $file =~ /\.(?:handlebars|css|js)$/io
l!l&&r!l&&!r
000
-d $file or $file =~ m[^(?:.+/)?\.[^/]+$]o
l!l&&r!l&&!r
000
MC/DC0%0/3
-d $file or $file =~ m[^(?:.+/)?\.[^/]+$]o or $file =~ /\.(?:handlebars|css|js)$/io
-d $file $file =~ m[^(?:.+/)?\.[^/]+$]o $file =~ /\.(?:handlebars|css|js)$/io
Truth table0%0/4
A or B or C
A= -d $fileB= $file =~ m[^(?:.+/)?\.[^/]+$]oC= $file =~ /\.(?:handlebars|css|js)$/io
ABCresult
1XX1
01X1
0011
0000
141 0 next;
142 }
143 # All other changes trigger a reload
144 0 ++$reload;
145 }
146 0 });
147 # time to reload or keep waiting
148 0 ▶ last FSMON if $reload;
Branch: 1 branch
BranchTrueFalseTotal
if $reload 0 0 0
149 }
150 }
151  
152 1 1;
153  
154 __END__
« blib/lib/App/DuckPAN/Cmd/Publisher.pm ↑ Summary blib/lib/App/DuckPAN/WebPublisher.pm »