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