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