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