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