Get the restarter running
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
CommitLineData
0ba6e8aa 1package Catalyst::Script::Server;
2
53b08cf9 3BEGIN {
4 $ENV{CATALYST_ENGINE} ||= 'HTTP';
5 $ENV{CATALYST_SCRIPT_GEN} = 31;
6 require Catalyst::Engine::HTTP;
7}
5b923b0b 8
a3ca4468 9use FindBin qw/$Bin/;
10use lib "$Bin/../lib";
11use Pod::Usage;
12use Moose;
a7dea640 13use Catalyst::Restarter;
4ebd5ecf 14#use Catalyst::Engine::HTTP;
59804176 15use namespace::autoclean;
a3ca4468 16
17with 'MooseX::Getopt';
18
f59a9d1b 19has debug => (
20 traits => [qw(Getopt)],
21 cmd_aliases => 'd',
57dc50b0 22 isa => 'Bool',
f59a9d1b 23 is => 'ro',
2e81e132 24 documentation => qq{
57dc50b0 25 -d --debug force debug mode
2e81e132 26 }
f59a9d1b 27
28);
29
57dc50b0 30has help => (
e46bf32c 31 traits => [qw(Getopt)],
90b481b1 32 cmd_aliases => 'h',
57dc50b0 33 isa => 'Bool',
34 is => 'ro',
2e81e132 35 documentation => qq{
57dc50b0 36 -h --help display this help and exits
37 },
e46bf32c 38);
39
57dc50b0 40has host => (
41 isa => 'Str',
42 is => 'ro',
43 ,
44 default => "localhost"
41b55019 45);
90b481b1 46
57dc50b0 47has fork => (
90b481b1 48 traits => [qw(Getopt)],
49 cmd_aliases => 'f',
50 isa => 'Bool',
57dc50b0 51 is => 'ro',
52
90b481b1 53);
54
57dc50b0 55has listen => (
204c6935 56 traits => [qw(Getopt)],
57 cmd_aliases => 'l',
58 isa => 'Int',
57dc50b0 59 is => 'ro',
60 ,
61 default => "3000"
204c6935 62);
63
57dc50b0 64has pidfile => (
41b55019 65 traits => [qw(Getopt)],
b6aaf142 66 cmd_aliases => 'pid',
57dc50b0 67 isa => 'Str',
68 is => 'ro',
69
41b55019 70);
71
57dc50b0 72has keepalive => (
bd31e11f 73 traits => [qw(Getopt)],
74 cmd_aliases => 'k',
57dc50b0 75 isa => 'Bool',
76 is => 'ro',
77 ,
78
bd31e11f 79);
80
57dc50b0 81has background => (
3954573c 82 traits => [qw(Getopt)],
83 cmd_aliases => 'bg',
57dc50b0 84 isa => 'Bool',
85 is => 'ro',
3954573c 86);
87
4ebd5ecf 88
57dc50b0 89has _app => (
90 reader => 'app',
80a909c2 91 init_arg => 'app',
92 traits => [qw(NoGetopt)],
57dc50b0 93 isa => 'Str',
94 is => 'ro',
95);
4b3881d4 96
8f01ed5f 97has restart => (
98 traits => [qw(Getopt)],
57dc50b0 99 cmd_aliases => 'r',
100 isa => 'Bool',
101 is => 'ro',
102
103);
a7dea640 104
105has restart_directory => (
106 traits => [qw(Getopt)],
107 cmd_aliases => 'rdir',
abee32cb 108 isa => 'ArrayRef[Str]',
a7dea640 109 is => 'ro',
abee32cb 110 predicate => '_has_restart_directory',
8f01ed5f 111);
112
57dc50b0 113has restart_delay => (
70871584 114 traits => [qw(Getopt)],
115 cmd_aliases => 'rdel',
57dc50b0 116 isa => 'Int',
117 is => 'ro',
abee32cb 118 predicate => '_has_restart_delay',
70871584 119);
120
57dc50b0 121has restart_regex => (
ee7aabd6 122 traits => [qw(Getopt)],
123 cmd_aliases => 'rxp',
57dc50b0 124 isa => 'Str',
125 is => 'ro',
abee32cb 126 predicate => '_has_restart_regex',
ee7aabd6 127);
128
57dc50b0 129has follow_symlinks => (
bbd42ac8 130 traits => [qw(Getopt)],
131 cmd_aliases => 'sym',
57dc50b0 132 isa => 'Bool',
133 is => 'ro',
abee32cb 134 predicate => '_has_follow_symlinks',
57dc50b0 135
bbd42ac8 136);
a3ca4468 137
8bae939b 138sub usage {
139 my ($self) = shift;
57dc50b0 140
8bae939b 141 return pod2usage();
142
143}
144
a3ca4468 145my @argv = @ARGV;
146
147sub run {
148 my $self = shift;
57dc50b0 149
8bae939b 150 $self->usage if $self->help;
57dc50b0 151
a7dea640 152 if ( $self->debug ) {
153 $ENV{CATALYST_DEBUG} = 1;
154 }
155
abee32cb 156 # If we load this here, then in the case of a restarter, it does not
157 # need to be reloaded for each restart.
158 require Catalyst;
159
160 # If this isn't done, then the Catalyst::Devel tests for the restarter
161 # fail.
162 $| = 1 if $ENV{HARNESS_ACTIVE};
163
a7dea640 164 if ( $self->restart ) {
165 die "Cannot run in the background and also watch for changed files.\n"
166 if $self->background;
167
168 require Catalyst::Restarter;
169
170 my $subclass = Catalyst::Restarter->pick_subclass;
171
172 my %args;
abee32cb 173 $args{follow_symlinks} = $self->follow_symlinks
174 if $self->_has_follow_symlinks;
175 $args{directories} = $self->restart_directory
176 if $self->_has_restart_directory;
177 $args{sleep_interval} = $self->restart_delay
178 if $self->_has_restart_delay;
179 $args{filter} = qr/$self->restart_regex/
180 if $self->_has_restart_regex;
a7dea640 181
182 my $restarter = $subclass->new(
183 %args,
abee32cb 184 start_sub => sub { $self->_run },
a7dea640 185 argv => $self->ARGV,
186 );
187
188 $restarter->run_and_watch;
189 }
190 else {
abee32cb 191 $self->_run;
a7dea640 192 }
193
194
57dc50b0 195}
196
abee32cb 197sub _run {
a7dea640 198 my ($self) = shift;
57dc50b0 199
a3ca4468 200 my $app = $self->app;
201 Class::MOP::load_class($app);
a7dea640 202
a3ca4468 203 $app->run(
204 $self->listen, $self->host,
57dc50b0 205 {
4b3881d4 206 'fork' => $self->fork,
207 keepalive => $self->keepalive,
208 background => $self->background,
209 pidfile => $self->pidfile,
615fee7c 210 keepalive => $self->keepalive,
615fee7c 211 follow_symlinks => $self->follow_symlinks,
57dc50b0 212 }
a3ca4468 213 );
a3ca4468 214}
5b923b0b 215
a7dea640 216
2e81e132 217no Moose;
218__PACKAGE__->meta->make_immutable;
5b923b0b 219
0ba6e8aa 2201;
e46bf32c 221
222=head1 NAME
223
224[% appprefix %]_server.pl - Catalyst Testserver
225
226=head1 SYNOPSIS
227
228[% appprefix %]_server.pl [options]
229
230 Options:
4b3881d4 231 -d --debug force debug mode
232 -f --fork handle each request in a new process
e46bf32c 233 (defaults to false)
4b3881d4 234 -h --help display this help and exits
235 --host host (defaults to all)
236 -p --port port (defaults to 3000)
237 -k --keepalive enable keep-alive connections
238 -r --restart restart when files get modified
239 (defaults to false)
240 --rd --restartdelay delay between file checks
e46bf32c 241 (ignored if you have Linux::Inotify2 installed)
4b3881d4 242 --rr --restartregex regex match files that trigger
e46bf32c 243 a restart when modified
244 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
4b3881d4 245 --rdir --restartdirectory the directory to search for
e46bf32c 246 modified files, can be set mulitple times
247 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 248 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 249 (defaults to false. this is a no-op on Win32)
4b3881d4 250 --bg --background run the process in the background
251 --pid --pidfile specify filename for pid file
e46bf32c 252
253 See also:
254 perldoc Catalyst::Manual
255 perldoc Catalyst::Manual::Intro
256
257=head1 DESCRIPTION
258
259Run a Catalyst Testserver for this application.
260
261=head1 AUTHORS
262
263Catalyst Contributors, see Catalyst.pm
264
265=head1 COPYRIGHT
266
267This library is free software. You can redistribute it and/or modify
268it under the same terms as Perl itself.
269
270=cut