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