Mojo/Server/Morbo.pm

filter: Statementstmt 25.2% 23 / 91 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 42.8% 6 / 14 Podpod 100% 1 / 1 totaltotal 15.8% 30 / 189 CC 34 SCAR 47.3
CC
34
Cov
14%
_manage
272.0
run
72.0
_spawn
42.0
CRAP 113.1
« Mojo/Server/CGI.pm ↑ Summary Mojo/Server/Hypnotoad.pm »
1 package Mojo::Server::Morbo;
2 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # "Linda: With Haley's Comet out of ice, Earth is experiencing the devastating
5 # effects of sudden, intense global warming.
6 # Morbo: Morbo is pleased but sticky."
7 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::Loader qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::Server::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::Util qw(steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX qw(WNOHANG _exit setpgid);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶has backend => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
14 0 ▶ my $backend = $ENV{MOJO_MORBO_BACKEND} || 'Poll';
Condition0%0/2
$ENV{'MOJO_MORBO_BACKEND'} || 'Poll'
l!l
00
MC/DC0%0/1
$ENV{'MOJO_MORBO_BACKEND'} || 'Poll'
$ENV{'MOJO_MORBO_BACKEND'}
Truth table0%0/2
A
A= $ENV{'MOJO_MORBO_BACKEND'}
Aresult
11
00
15 0 $backend = "Mojo::Server::Morbo::Backend::$backend";
16 0 ▶ return $backend->new unless my $e = load_class $backend;
Branch: 1 branch
BranchTrueFalseTotal
unless my $e = load_class($backend) 0 0 0
17 0 ▶ die $e if ref $e;
Branch: 1 branch
BranchTrueFalseTotal
if ref $e 0 0 0
18 0 die qq{Can't find Morbo backend class "$backend" in \@INC. (@INC)\n};
19 1 };
20 0 ▶has daemon => sub { Mojo::Server::Daemon->new(silent => 1) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 0 ▶has exec => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
22 1 has silent => 1;
23  
24 0 ▶sub run {
Subroutine run: not called CC=8 SCAR=42.8
Pod documented
25 0 my ($self, $app, @args) = @_;
26  
27 # No fork emulation support
28 0 ▶ die "Morbo does not support fork emulation.\n" if $Config{d_pseudofork};
Branch: 1 branch
BranchTrueFalseTotal
if $Mojo::Server::Morbo::Config{"d_pseudofork"} 0 0 0
29  
30 # Clean manager environment
31 0 ▶ local $SIG{INT} = local $SIG{TERM} = local $SIG{HUP} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
32 0 $self->{finished} = 1;
33 0 $self->_stop($_) for grep {defined} $self->{worker}, keys %{$self->{commands}};
34 0 };
35 0 unshift @{$self->backend->watch}, $0 = $app;
36 0 ▶ $self->{command} = [$^X, $app, @args ? @args : 'daemon'];
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
37 0 $self->{modified} = 1;
38  
39 # Prepare and cache listen sockets for smooth restarting (inherited by the server)
40 0 $self->daemon->start->stop;
41 0 $ENV{MOJO_LISTEN} = join ',', @{$self->daemon->listen};
42 0 $ENV{PERL5LIB} = join $Config{path_sep}, grep { !ref } @INC;
43  
44 # Commands (like asset bundlers in watch mode) need to know the mode too, and only get started once
45 0 ▶ $ENV{MOJO_MODE} ||= 'development';
Condition0%0/2
$ENV{"MOJO_MODE"} ||= "development"
l!l
00
MC/DC0%0/1
$ENV{"MOJO_MODE"} ||= "development"
$ENV{"MOJO_MODE"}
Truth table0%0/2
A
A= $ENV{"MOJO_MODE"}
Aresult
11
00
46  
47 # Commands get a pipe as STDIN that stays open until Morbo exits, so they can read from it without getting stopped
48 # (process group is not in the foreground) and know when to exit if Morbo dies unexpectedly (like esbuild does)
49 0 ▶ pipe(my $stdin, $self->{stdin}) or die "Can't create pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $stdin, $self->{"stdin"} or die "Can't create pipe: $!" 0 0 0
50 0 $self->{commands} = {map { $self->_spawn($stdin, $_) => $_ } @{$self->exec}};
51 0 close $stdin;
52  
53 0 ▶ $self->_manage until $self->{finished} && !$self->{worker} && !keys %{$self->{commands}};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"finished"} and not $self->{"worker"} and not keys %{$$self{"commands"};} or do { $self->_manage } 0 0 0
Condition0%0/6
$self->{"finished"} and not $self->{"worker"} and not keys %{$$self{"commands"};}
!ll&&!rl&&r
000
$self->{"finished"} and not $self->{"worker"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"finished"} and not $self->{"worker"} and not keys %{$$self{"commands"};}
$self->{"finished"} not $self->{"worker"} not keys %{$$self{"commands"};}
Truth table0%0/4
A and not(B) and not(C)
A= $self->{"finished"}B= not $self->{"worker"}C= not keys %{$$self{"commands"};}
ABCresult
0XX0
10X0
1100
1111
54 0 exit 0;
55 }
56  
57 0 ▶sub _manage {
Subroutine _manage: not called CC=16 SCAR=56.1
58 0 my $self = shift;
59  
60 0 ▶ if (my @files = @{$self->backend->modified_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@files) = @{$self->backend->modified_files;}) 0 0 0
61 0 ▶ say @files == 1
Branch: 2 branches
BranchTrueFalseTotal
unless $self->silent 0 0 0
@files == 1 ? : 0 0 0
62 0 ? qq{File "@{[$files[0]]}" changed, restarting.}
63 0 : qq{@{[scalar @files]} files changed, restarting.}
64 unless $self->silent;
65 0 $self->_stop($self->{worker});
66 0 $self->{modified} = 1;
67 }
68  
69 0 for my $pid (grep {defined} $self->{worker}, keys %{$self->{commands}}) {
70 0 ▶ if (waitpid($pid, WNOHANG) == $pid) {
Branch: 3 branches
BranchTrueFalseTotal
if (waitpid($pid, 1) == $pid) { } 0 0 0
elsif ($self->{'kill'}{$pid}) { } 0 0 0
elsif ($self->{'finished'}) { } 0 0 0
71 0 delete $self->{kill}{$pid};
72 0 ▶ if ($self->{worker} && $self->{worker} == $pid) { delete $self->{worker} }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'worker'} and $self->{'worker'} == $pid) { } 0 0 0
elsif (not $self->{'finished'}) { } 0 0 0
Condition0%0/3
$self->{"worker"} and $self->{"worker"} == $pid
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"worker"} and $self->{"worker"} == $pid
$self->{"worker"} $self->{"worker"} == $pid
Truth table0%0/3
A and B
A= $self->{"worker"}B= $self->{"worker"} == $pid
ABresult
0X0
100
111
73 elsif (!$self->{finished}) {
74 0 my $cmd = $self->{commands}{$pid};
75 0 ▶ warn $? & 127
Branch: 1 branch
BranchTrueFalseTotal
$? & 127 ? : 0 0 0
76 0 ? qq{Command "$cmd" was killed by signal @{[$? & 127]}.\n}
77 0 : qq{Command "$cmd" exited with status @{[$? >> 8]}.\n};
78 }
79 0 delete $self->{commands}{$pid};
80 }
81  
82 # Process did not stop in time
83 0 ▶ elsif ($self->{kill}{$pid}) { kill 'KILL', -$pid if $self->{kill}{$pid} <= steady_time }
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"kill"}{$pid} <= &steady_time() 0 0 0
84  
85 # Process was started after the signal handler ran
86 0 elsif ($self->{finished}) { $self->_stop($pid) }
87 }
88  
89 0 $self->{worker} = $self->_spawn(undef, @{$self->{command}})
90 0 ▶ if !$self->{worker} && !$self->{finished} && delete $self->{modified};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"worker"} || $self->{"finished"} and delete $self->{"modified"} 0 0 0
Condition0%0/6
not $self->{"worker"} || $self->{"finished"} and delete $self->{"modified"}
!ll&&!rl&&r
000
$self->{'worker'} || $self->{'finished'}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->{"worker"} || $self->{"finished"} and delete $self->{"modified"}
$self->{'worker'} $self->{'finished'} delete $self->{"modified"}
Truth table0%0/4
not(A || B) and C
A= $self->{'worker'}B= $self->{'finished'}C= delete $self->{"modified"}
ABCresult
1XX0
01X0
0000
0011
91 }
92  
93 0 ▶sub _spawn {
Subroutine _spawn: not called CC=6 SCAR=37.4
94 0 my ($self, $stdin, @command) = @_;
95  
96 # Manager (process group gets set in both processes to avoid a race)
97 0 ▶ die "Can't fork: $!" unless defined(my $pid = fork);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $pid = fork) 0 0 0
98 0 ▶ if ($pid) { setpgid $pid, $pid; return $pid }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
99  
100 # Worker (process group includes subprocesses)
101 0 setpgid 0, 0;
102 0 ▶ open STDIN, '<&', $stdin or warn "Can't redirect STDIN: $!\n" if $stdin;
Branch: 1 branch
BranchTrueFalseTotal
if $stdin 0 0 0
Condition0%0/5
$stdin && (open(STDIN, '<&', $stdin) || warn("Can't redirect STDIN: $!\n"))
!ll&&!rl&&r
000
open STDIN, "<&", $stdin or warn "Can't redirect STDIN: $!\n"
l!l
00
MC/DC0%0/2
$stdin && (open(STDIN, '<&', $stdin) || warn("Can't redirect STDIN: $!\n"))
$stdin open STDIN, "<&", $stdin
Truth table0%0/3
A && B
A= $stdinB= open STDIN, "<&", $stdin
ABresult
0X0
100
111
103  
104 # Skip cleanup code inherited from the manager if exec fails
105 0 ▶ exec(@command) or _exit 1;
Branch: 1 branch
BranchTrueFalseTotal
CORE::exec @command or _exit(1) 0 0 0
106 }
107  
108 0 ▶sub _stop {
Subroutine _stop: not called CC=3 SCAR=24.8
109 0 my ($self, $pid) = @_;
110 0 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
111 0 kill 'TERM', -$pid;
112 0 ▶ $self->{kill}{$pid} //= steady_time + 5;
Condition0%0/2
$self->{"kill"}{$pid} //= &steady_time() + 5
l!l
00
MC/DC0%0/1
$self->{"kill"}{$pid} //= &steady_time() + 5
$self->{"kill"}{$pid}
Truth table0%0/2
A
A= $self->{"kill"}{$pid}
Aresult
11
00
113 }
114  
115 1 1;
116  
117 =encoding utf8
118  
119 =head1 NAME
120  
121 Mojo::Server::Morbo - Tonight at 11...DOOOOOOOOOOOOOOOM!
122  
123 =head1 SYNOPSIS
124  
125 use Mojo::Server::Morbo;
126  
127 my $morbo = Mojo::Server::Morbo->new;
128 $morbo->run('/home/sri/myapp.pl');
129 $morbo->run('/home/sri/myapp.pl', 'prefork', '-w', 1);
130  
131 Mojo::Server::Morbo->new(exec => ['npx webpack --watch'])->run('/home/sri/myapp.pl');
132  
133 =head1 DESCRIPTION
134  
135 L<Mojo::Server::Morbo> is a self-restart capable development server. It starts your application with a server command,
136 like L<Mojolicious::Command::daemon> or L<Mojolicious::Command::prefork>, and restarts it whenever a file changes. Listen
137 sockets are only created once and then passed on to the server, so they should be configured with Morbo and not the
138 server command. Note that the server uses signals for process management, so you should avoid modifying signal handlers
139 in your applications.
140  
141 Additional commands, like asset bundlers in watch mode, can be started alongside the server with L</"exec">. They only
142 get started once, are not restarted together with the server, and get stopped when Morbo shuts down.
143  
144 To start applications with it you can use the L<morbo> script.
145  
146 $ morbo ./myapp.pl
147 Web application available at http://127.0.0.1:3000
148  
149 For better scalability (epoll, kqueue) and to provide non-blocking name resolution, SOCKS5 as well as TLS support, the
150 optional modules L<EV> (4.32+), L<Net::DNS::Native> (0.15+), L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL>
151 (2.009+) will be used automatically if possible. Individual features can also be disabled with the C<MOJO_NO_NNR>,
152 C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment variables.
153  
154 See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.
155  
156 =head1 SIGNALS
157  
158 The L<Mojo::Server::Morbo> process can be controlled at runtime with the following signals.
159  
160 =head2 INT, TERM, HUP
161  
162 Shut down server immediately. The server, all L</"exec"> commands and their subprocesses get a C<TERM> signal, and
163 will be killed if they have not stopped after 5 seconds, just like for restarts.
164  
165 =head1 ATTRIBUTES
166  
167 L<Mojo::Server::Morbo> implements the following attributes.
168  
169 =head2 backend
170  
171 my $backend = $morbo->backend;
172 $morbo = $morbo->backend(Mojo::Server::Morbo::Backend::Poll->new);
173  
174 Backend, usually a L<Mojo::Server::Morbo::Backend::Poll> object.
175  
176 =head2 daemon
177  
178 my $daemon = $morbo->daemon;
179 $morbo = $morbo->daemon(Mojo::Server::Daemon->new);
180  
181 L<Mojo::Server::Daemon> object used to create listen sockets. Other settings will be ignored, since the server runs in
182 a separate process, use options of the server command instead.
183  
184 =head2 exec
185  
186 my $commands = $morbo->exec;
187 $morbo = $morbo->exec(['npx webpack --watch', 'npx tailwindcss -i app.css -o public/assets/app.css --watch']);
188  
189 Shell commands to start alongside the server, like asset bundlers in watch mode. The C<MOJO_MODE> environment variable
190 will be set to the operating mode of your application, so commands can use it too. C<STDIN> of all commands is a pipe
191 that gets closed when Morbo exits, even if it gets killed unexpectedly, which some tools use to shut down. When a
192 command exits on its own, a message with its exit status is written to C<STDERR>.
193  
194 =head2 silent
195  
196 my $bool = $morbo->silent;
197 $morbo = $morbo->silent($bool);
198  
199 Disable console messages, defaults to a true value.
200  
201 =head1 METHODS
202  
203 L<Mojo::Server::Morbo> inherits all methods from L<Mojo::Base> and implements the following new ones.
204  
205 =head2 run
206  
207 $morbo->run('script/my_app');
208 $morbo->run('script/my_app', 'prefork', '-w', 1);
209  
210 Run server command for application and wait for L</"SIGNALS">. The command defaults to C<daemon>.
211  
212 =head1 SEE ALSO
213  
214 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
215  
216 =cut
« Mojo/Server/CGI.pm ↑ Summary Mojo/Server/Hypnotoad.pm »