lib/Catalyst/Script/Server.pm

filter: Statementstmt 70.8% 68 / 96 Branchbran 47.6% 20 / 42 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 16 / 20 Podpod 0.0% 0 / 3 totaltotal 59.3% 105 / 177 CC 27 SCAR 25.4
CC
27
Cov
60%
run
37.5
__ANON__
20.0
BUILD
17.1
CRAP 12.7
« lib/Catalyst.pm ↑ Summary lib/Catalyst/Request.pm »
1 package Catalyst::Script::Server;
2 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Class::Load qw(try_load_class load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use namespace::clean -except => [ 'meta' ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 with 'Catalyst::ScriptRole';
8  
9 3 has debug => (
10 traits => [qw(Getopt)],
11 cmd_aliases => 'd',
12 isa => 'Bool',
13 is => 'ro',
14 documentation => q{Force debug mode},
15 );
16  
17 3 has host => (
18 traits => [qw(Getopt)],
19 cmd_aliases => 'h',
20 isa => 'Str',
21 is => 'ro',
22 # N.B. undef (the default) means we bind on all interfaces on the host.
23 documentation => 'Specify a hostname or IP on this host for the server to bind to',
24 );
25  
26 3 has fork => (
27 traits => [qw(Getopt)],
28 cmd_aliases => 'f',
29 isa => 'Bool',
30 is => 'ro',
31 default => 0,
32 documentation => 'Fork the server to be able to serve multiple requests at once',
33 );
34  
35 has port => (
36 traits => [qw(Getopt)],
37 cmd_aliases => 'p',
38 isa => 'Int',
39 is => 'ro',
40 lazy => 1,
41 34 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 34 ▶ Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
Branch: 1 branch
BranchTrueFalseTotal
Catalyst::Utils::env_value((shift())->application_name, "port") or 3000 4 30 34
43 },
44 3 documentation => 'Specify a different listening port (to the default port 3000)',
45 );
46  
47 3 has '+help_flag' => (
48 cmd_aliases => [ qw(usage ?) ],
49 );
50  
51 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 class_type 'MooseX::Daemonize::Pid::File';
53 3 subtype 'Catalyst::Script::Server::Types::Pidfile',
54 as 'MooseX::Daemonize::Pid::File';
55  
56 0 ▶coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via {
Subroutine __ANON__: not called CC=2 SCAR=17.9
57 0 my ($success, $error) = try_load_class("MooseX::Daemonize::Pid::File");
58 0 ▶ warn("Could not load MooseX::Daemonize::Pid::File, needed for --pid option: $error\n"),
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
59 exit 1 if not $success;
60 0 MooseX::Daemonize::Pid::File->new( file => $_ );
61 3 };
62 3 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
63 'Catalyst::Script::Server::Types::Pidfile' => '=s',
64 );
65 3 has pidfile => (
66 traits => [qw(Getopt)],
67 cmd_aliases => 'pid',
68 isa => 'Catalyst::Script::Server::Types::Pidfile',
69 is => 'ro',
70 documentation => 'Specify a pidfile',
71 coerce => 1,
72 predicate => '_has_pidfile',
73 );
74  
75 # Override MooseX::Daemonize
76 0 ▶sub dont_close_all_files { 1 }
Subroutine dont_close_all_files: not called CC=1 SCAR=6.9
Pod undocumented
77 44 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=28.4
Pod undocumented
78 44 my $self = shift;
79  
80 44 ▶ if ($self->background) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->background) 0 44 44
81 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
82 0 my ($success, $error) = try_load_class("MooseX::Daemonize::Core");
83 0 ▶ warn("MooseX::Daemonize is needed for the --background option: $error\n"),
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
84 exit 1 if not $success;
85 0 ($success, $error) = try_load_class("POSIX");
86 0 ▶ warn("$error\n"), exit 1 if not $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
87 0 MooseX::Daemonize::Core->meta->apply($self);
88 0 POSIX::close($_) foreach (0..2);
89 }
90 }
91  
92 3 has keepalive => (
93 traits => [qw(Getopt)],
94 cmd_aliases => 'k',
95 isa => 'Bool',
96 is => 'ro',
97 default => 0,
98 documentation => 'Support keepalive',
99 );
100  
101 3 has background => (
102 traits => [qw(Getopt)],
103 cmd_aliases => 'bg',
104 isa => 'Bool',
105 is => 'ro',
106 default => 0,
107 documentation => 'Run in the background',
108 );
109  
110 has restart => (
111 traits => [qw(Getopt)],
112 cmd_aliases => 'r',
113 isa => 'Bool',
114 is => 'ro',
115 lazy => 1,
116 22 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
117 22 ▶ Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
Branch: 1 branch
BranchTrueFalseTotal
Catalyst::Utils::env_value((shift())->application_name, "reload") or 0 4 18 22
118 },
119 3 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
120 );
121  
122 3 has restart_directory => (
123 traits => [qw(Getopt)],
124 cmd_aliases => [ 'rdir', 'restartdirectory' ],
125 isa => 'ArrayRef[Str]',
126 is => 'ro',
127 documentation => 'Restarter directory to watch',
128 predicate => '_has_restart_directory',
129 );
130  
131 3 has restart_delay => (
132 traits => [qw(Getopt)],
133 cmd_aliases => 'rd',
134 isa => 'Int',
135 is => 'ro',
136 documentation => 'Set a restart delay',
137 predicate => '_has_restart_delay',
138 );
139  
140 {
141 3 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 3 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef';
144 4 ▶ coerce $tc, from 'Str', via { qr/$_/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
145  
146 3 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
147  
148 3 has restart_regex => (
149 traits => [qw(Getopt)],
150 cmd_aliases => 'rr',
151 isa => $tc,
152 coerce => 1,
153 is => 'ro',
154 documentation => 'Restart regex',
155 predicate => '_has_restart_regex',
156 );
157 }
158  
159 3 has follow_symlinks => (
160 traits => [qw(Getopt)],
161 cmd_aliases => 'sym',
162 isa => 'Bool',
163 is => 'ro',
164 default => 0,
165 documentation => 'Follow symbolic links',
166 predicate => '_has_follow_symlinks',
167 );
168  
169 18 ▶sub _plack_engine_name {
Subroutine _plack_engine_name: called CC=3 SCAR=13.1
170 18 my $self = shift;
171 18 ▶ return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
Branch: 1 branch
BranchTrueFalseTotal
$self->fork || $self->keepalive ? : 0 18 18
Condition33%1/3
$self->fork or $self->keepalive
l!l&&r!l&&!r
0018
MC/DC0%0/2
$self->fork or $self->keepalive
$self->fork $self->keepalive
Truth table33%1/3
A or B
A= $self->forkB= $self->keepalive
ABresult
1X1
011
000
172 }
173  
174 20 ▶sub _restarter_args {
Subroutine _restarter_args: called CC=5 SCAR=16.3
175 20 my $self = shift;
176  
177 return (
178 argv => $self->ARGV,
179 0 ▶ start_sub => sub { $self->_run_application },
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
181 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
182 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
183 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
184 ),
185 (
186 20 ▶ map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
Branch: 4 branches
BranchTrueFalseTotal
$self->_has_follow_symlinks ? : 20 0 20
$self->_has_restart_delay ? : 4 16 20
$self->_has_restart_directory ? : 6 14 20
$self->_has_restart_regex ? : 4 16 20
187 );
188 }
189  
190 has restarter_class => (
191 is => 'ro',
192 isa => 'Str',
193 lazy => 1,
194 6 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
195 6 my $self = shift;
196 6 ▶ Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
Branch: 1 branch
BranchTrueFalseTotal
Catalyst::Utils::env_value($self->application_name, "RESTARTER") or "Catalyst::Restarter" 4 2 6
197 }
198 3 );
199  
200 18 ▶sub run {
Subroutine run: called CC=9 SCAR=36.2
Pod undocumented
201 18 my $self = shift;
202  
203 18 ▶ local $ENV{CATALYST_DEBUG} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 18 18
204 if $self->debug;
205  
206 18 ▶ if ( $self->restart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->restart) { } 0 18 18
207 0 ▶ die "Cannot run in the background and also watch for changed files.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->background 0 0 0
208 if $self->background;
209 0 ▶ die "Cannot write out a pid file and fork for the restarter.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_pidfile 0 0 0
210 if $self->_has_pidfile;
211  
212 # If we load this here, then in the case of a restarter, it does not
213 # need to be reloaded for each restart.
214 0 require Catalyst;
215  
216 # If this isn't done, then the Catalyst::Devel tests for the restarter
217 # fail.
218 0 ▶ $| = 1 if $ENV{HARNESS_ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HARNESS_ACTIVE"} 0 0 0
219  
220 0 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
221  
222 0 my $subclass = $self->restarter_class->pick_subclass;
223  
224 0 my $restarter = $subclass->new(
225 $self->_restarter_args()
226 );
227  
228 0 $restarter->run_and_watch;
229 }
230 else {
231 18 ▶ if ($self->background) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->background) 0 18 18
232 0 $self->daemon_fork;
233  
234 0 ▶ return 1 unless $self->is_daemon;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_daemon 0 0 0
235  
236 0 load_class($self->application_name);
237  
238 0 $self->daemon_detach;
239 }
240  
241 $self->pidfile->write
242 18 ▶ if $self->_has_pidfile;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_pidfile 0 18 18
243  
244 18 $self->_run_application;
245 }
246  
247  
248 }
249  
250 18 ▶sub _plack_loader_args {
Subroutine _plack_loader_args: called CC=2 SCAR=14.2
251 18 my ($self) = shift;
252 return (
253 port => $self->port,
254 host => $self->host,
255 keepalive => $self->keepalive ? 100 : 1,
256 0 ▶ server_ready => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
257 0 my ($args) = @_;
258  
259 0 ▶ my $name = $args->{server_software} || ref($args); # $args is $server
Condition0%0/3
$args->{'server_software'} || ref $args
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'server_software'} || ref $args
$args->{'server_software'} ref $args
Truth table0%0/3
A || B
A= $args->{'server_software'}B= ref $args
ABresult
1X1
011
000
260 0 ▶ my $host = $args->{host} || 0;
Condition0%0/2
$args->{'host'} || 0
l!l
00
MC/DC0%0/1
$args->{'host'} || 0
$args->{'host'}
Truth table0%0/2
A
A= $args->{'host'}
Aresult
11
00
261 0 ▶ my $proto = $args->{proto} || 'http';
Condition0%0/2
$args->{'proto'} || 'http'
l!l
00
MC/DC0%0/1
$args->{'proto'} || 'http'
$args->{'proto'}
Truth table0%0/2
A
A= $args->{'proto'}
Aresult
11
00
262  
263 0 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
264 },
265 18 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->keepalive ? : 0 18 18
266 }
267  
268 18 ▶around _application_args => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
269 18 my ($orig, $self) = @_;
270 return (
271 $self->port,
272 $self->host,
273 {
274 18 %{ $self->$orig },
275 18 map { $_ => $self->$_ } qw/
276 fork
277 keepalive
278 background
279 pidfile
280 keepalive
281 follow_symlinks
282 port
283 host
284 /,
285 },
286 );
287 3 };
288  
289 3 __PACKAGE__->meta->make_immutable;
290 3 1;
291  
292 =head1 NAME
293  
294 Catalyst::Script::Server - Catalyst test server
295  
296 =head1 SYNOPSIS
297  
298 myapp_server.pl [options]
299  
300 Options:
301 -d --debug force debug mode
302 -f --fork handle each request in a new process
303 (defaults to false)
304 --help display this help and exits
305 -h --host host (defaults to all)
306 -p --port port (defaults to 3000)
307 -k --keepalive enable keep-alive connections
308 -r --restart restart when files get modified
309 (defaults to false)
310 --rd --restart_delay delay between file checks
311 (ignored if you have Linux::Inotify2 installed)
312 --rr --restart_regex regex match files that trigger
313 a restart when modified
314 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
315 --rdir --restart_directory the directory to search for
316 modified files, can be set multiple times
317 (defaults to '[SCRIPT_DIR]/..')
318 --sym --follow_symlinks follow symlinks in search directories
319 (defaults to false. this is a no-op on Win32)
320 --bg --background run the process in the background
321 --pid --pidfile specify filename for pid file
322  
323 See also:
324 perldoc Catalyst::Manual
325 perldoc Catalyst::Manual::Intro
326  
327 =head1 DESCRIPTION
328  
329 Run a Catalyst test server for this application.
330  
331 =head1 SEE ALSO
332  
333 L<Catalyst::ScriptRunner>
334  
335 =head1 AUTHORS
336  
337 Catalyst Contributors, see Catalyst.pm
338  
339 =head1 COPYRIGHT
340  
341 This library is free software. You can redistribute it and/or modify
342 it under the same terms as Perl itself.
343  
344 =cut
« lib/Catalyst.pm ↑ Summary lib/Catalyst/Request.pm »