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