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