1 package Catalyst::Script::Server;
4 use Class::Load qw(try_load_class load_class);
5 use namespace::autoclean;
7 with 'Catalyst::ScriptRole';
10 traits => [qw(Getopt)],
14 documentation => q{Force debug mode},
18 traits => [qw(Getopt)],
22 # N.B. undef (the default) means we bind on all interfaces on the host.
23 documentation => 'Specify a hostname or IP on this host for the server to bind to',
27 traits => [qw(Getopt)],
32 documentation => 'Fork the server to be able to serve multiple requests at once',
36 traits => [qw(Getopt)],
41 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
43 documentation => 'Specify a different listening port (to the default port 3000)',
46 use Moose::Util::TypeConstraints;
47 class_type 'MooseX::Daemonize::Pid::File';
48 subtype 'Catalyst::Script::Server::Types::Pidfile',
49 as 'MooseX::Daemonize::Pid::File';
51 coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via {
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;
55 MooseX::Daemonize::Pid::File->new( file => $_ );
57 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
58 'Catalyst::Script::Server::Types::Pidfile' => '=s',
61 traits => [qw(Getopt)],
63 isa => 'Catalyst::Script::Server::Types::Pidfile',
65 documentation => 'Specify a pidfile',
67 predicate => '_has_pidfile',
70 # Override MooseX::Daemonize
71 sub dont_close_all_files { 1 }
75 if ($self->background) {
76 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
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 ($success, $error) = try_load_class("POSIX");
81 warn("$error\n"), exit 1 if not $success;
82 MooseX::Daemonize::Core->meta->apply($self);
83 POSIX::close($_) foreach (0..2);
88 traits => [qw(Getopt)],
93 documentation => 'Support keepalive',
97 traits => [qw(Getopt)],
102 documentation => 'Run in the background',
106 traits => [qw(Getopt)],
111 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
113 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
116 has restart_directory => (
117 traits => [qw(Getopt)],
118 cmd_aliases => [ 'rdir', 'restartdirectory' ],
119 isa => 'ArrayRef[Str]',
121 documentation => 'Restarter directory to watch',
122 predicate => '_has_restart_directory',
125 has restart_delay => (
126 traits => [qw(Getopt)],
130 documentation => 'Set a restart delay',
131 predicate => '_has_restart_delay',
135 use Moose::Util::TypeConstraints;
137 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef';
138 coerce $tc, from 'Str', via { qr/$_/ };
140 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
142 has restart_regex => (
143 traits => [qw(Getopt)],
148 documentation => 'Restart regex',
149 predicate => '_has_restart_regex',
153 has follow_symlinks => (
154 traits => [qw(Getopt)],
155 cmd_aliases => 'sym',
159 documentation => 'Follow symbolic links',
160 predicate => '_has_follow_symlinks',
163 sub _plack_engine_name {
165 return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
168 sub _restarter_args {
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) : ()),
177 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
180 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
184 has restarter_class => (
190 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
197 local $ENV{CATALYST_DEBUG} = 1
200 if ( $self->restart ) {
201 die "Cannot run in the background and also watch for changed files.\n"
202 if $self->background;
203 die "Cannot write out a pid file and fork for the restarter.\n"
204 if $self->_has_pidfile;
206 # If we load this here, then in the case of a restarter, it does not
207 # need to be reloaded for each restart.
210 # If this isn't done, then the Catalyst::Devel tests for the restarter
212 $| = 1 if $ENV{HARNESS_ACTIVE};
214 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
216 my $subclass = $self->restarter_class->pick_subclass;
218 my $restarter = $subclass->new(
219 $self->_restarter_args()
222 $restarter->run_and_watch;
225 if ($self->background) {
228 return 1 unless $self->is_daemon;
230 load_class($self->application_name);
232 $self->daemon_detach;
235 $self->pidfile->write
236 if $self->_has_pidfile;
238 $self->_run_application;
244 sub _plack_loader_args {
249 keepalive => $self->keepalive ? 100 : 1,
250 server_ready => sub {
253 my $name = $args->{server_software} || ref($args); # $args is $server
254 my $host = $args->{host} || 0;
255 my $proto = $args->{proto} || 'http';
257 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
262 around _application_args => sub {
263 my ($orig, $self) = @_;
269 map { $_ => $self->$_ } qw/
283 __PACKAGE__->meta->make_immutable;
288 Catalyst::Script::Server - Catalyst test server
292 myapp_server.pl [options]
295 -d --debug force debug mode
296 -f --fork handle each request in a new process
298 --help display this help and exits
299 -h --host host (defaults to all)
300 -p --port port (defaults to 3000)
301 -k --keepalive enable keep-alive connections
302 -r --restart restart when files get modified
304 --rd --restart_delay delay between file checks
305 (ignored if you have Linux::Inotify2 installed)
306 --rr --restart_regex regex match files that trigger
307 a restart when modified
308 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
309 --rdir --restart_directory the directory to search for
310 modified files, can be set multiple times
311 (defaults to '[SCRIPT_DIR]/..')
312 --sym --follow_symlinks follow symlinks in search directories
313 (defaults to false. this is a no-op on Win32)
314 --bg --background run the process in the background
315 --pid --pidfile specify filename for pid file
318 perldoc Catalyst::Manual
319 perldoc Catalyst::Manual::Intro
323 Run a Catalyst test server for this application.
327 L<Catalyst::ScriptRunner>
331 Catalyst Contributors, see Catalyst.pm
335 This library is free software. You can redistribute it and/or modify
336 it under the same terms as Perl itself.