File Coverage

blib/lib/Catalyst/Script/Server.pm
Criterion Covered Total %
statement 33 58 56.9
branch 14 34 41.1
condition 1 10 10.0
subroutine 11 14 78.5
pod 0 3 0.0
total 59 119 49.5


line stmt bran cond sub pod time code
1             package Catalyst::Script::Server;
2 3     3   120771 use Moose;
  3         942406  
  3         22  
3 3     3   23475 use Catalyst::Utils;
  3         12  
  3         117  
4 3     3   28 use Class::Load qw(try_load_class load_class);
  3         5  
  3         215  
5 3     3   28 use namespace::clean -except => [ 'meta' ];
  3         7  
  3         27  
6              
7             with 'Catalyst::ScriptRole';
8              
9             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             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             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             default => sub {
42             Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
43             },
44             documentation => 'Specify a different listening port (to the default port 3000)',
45             );
46              
47             has '+help_flag' => (
48             cmd_aliases => [ qw(usage ?) ],
49             );
50              
51 3     3   1644 use Moose::Util::TypeConstraints;
  3         7  
  3         65  
52             class_type 'MooseX::Daemonize::Pid::File';
53             subtype 'Catalyst::Script::Server::Types::Pidfile',
54             as 'MooseX::Daemonize::Pid::File';
55              
56             coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via {
57             my ($success, $error) = try_load_class("MooseX::Daemonize::Pid::File");
58             warn("Could not load MooseX::Daemonize::Pid::File, needed for --pid option: $error\n"),
59             exit 1 if not $success;
60             MooseX::Daemonize::Pid::File->new( file => $_ );
61             };
62             MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
63             'Catalyst::Script::Server::Types::Pidfile' => '=s',
64             );
65             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     0 0 0 sub dont_close_all_files { 1 }
77             sub BUILD {
78 44     44 0 88 my $self = shift;
79              
80 44 50       1570 if ($self->background) {
81             # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
82 0         0 my ($success, $error) = try_load_class("MooseX::Daemonize::Core");
83 0 0       0 warn("MooseX::Daemonize is needed for the --background option: $error\n"),
84             exit 1 if not $success;
85 0         0 ($success, $error) = try_load_class("POSIX");
86 0 0       0 warn("$error\n"), exit 1 if not $success;
87 0         0 MooseX::Daemonize::Core->meta->apply($self);
88 0         0 POSIX::close($_) foreach (0..2);
89             }
90             }
91              
92             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             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             default => sub {
117             Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
118             },
119             documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
120             );
121              
122             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             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     3   8587 use Moose::Util::TypeConstraints;
  3         9  
  3         14  
142              
143             my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef';
144             coerce $tc, from 'Str', via { qr/$_/ };
145              
146             MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
147              
148             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             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             sub _plack_engine_name {
170 18     18   39 my $self = shift;
171 18 50 33     565 return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
172             }
173              
174             sub _restarter_args {
175 20     20   52 my $self = shift;
176              
177             return (
178             argv => $self->ARGV,
179 0     0   0 start_sub => sub { $self->_run_application },
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 50       670 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
  160 100       4594  
    100          
    100          
187             );
188             }
189              
190             has restarter_class => (
191             is => 'ro',
192             isa => 'Str',
193             lazy => 1,
194             default => sub {
195             my $self = shift;
196             Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
197             }
198             );
199              
200             sub run {
201 18     18 0 14967 my $self = shift;
202              
203 18 50       676 local $ENV{CATALYST_DEBUG} = 1
204             if $self->debug;
205              
206 18 50       527 if ( $self->restart ) {
207 0 0       0 die "Cannot run in the background and also watch for changed files.\n"
208             if $self->background;
209 0 0       0 die "Cannot write out a pid file and fork for the restarter.\n"
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         0 require Catalyst;
215              
216             # If this isn't done, then the Catalyst::Devel tests for the restarter
217             # fail.
218 0 0       0 $| = 1 if $ENV{HARNESS_ACTIVE};
219              
220 0         0 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
221              
222 0         0 my $subclass = $self->restarter_class->pick_subclass;
223              
224 0         0 my $restarter = $subclass->new(
225             $self->_restarter_args()
226             );
227              
228 0         0 $restarter->run_and_watch;
229             }
230             else {
231 18 50       532 if ($self->background) {
232 0         0 $self->daemon_fork;
233              
234 0 0       0 return 1 unless $self->is_daemon;
235              
236 0         0 load_class($self->application_name);
237              
238 0         0 $self->daemon_detach;
239             }
240              
241             $self->pidfile->write
242 18 50       614 if $self->_has_pidfile;
243              
244 18         103 $self->_run_application;
245             }
246              
247              
248             }
249              
250             sub _plack_loader_args {
251 18     18   48 my ($self) = shift;
252             return (
253             port => $self->port,
254             host => $self->host,
255             keepalive => $self->keepalive ? 100 : 1,
256             server_ready => sub {
257 0     0     my ($args) = @_;
258              
259 0   0       my $name = $args->{server_software} || ref($args); # $args is $server
260 0   0       my $host = $args->{host} || 0;
261 0   0       my $proto = $args->{proto} || 'http';
262              
263 0           print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
264             },
265 18 50       506 );
266             }
267              
268             around _application_args => sub {
269             my ($orig, $self) = @_;
270             return (
271             $self->port,
272             $self->host,
273             {
274             %{ $self->$orig },
275             map { $_ => $self->$_ } qw/
276             fork
277             keepalive
278             background
279             pidfile
280             keepalive
281             follow_symlinks
282             port
283             host
284             /,
285             },
286             );
287             };
288              
289             __PACKAGE__->meta->make_immutable;
290             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