cleaned up sub runner
[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;
ba7e82e3 163 $args{follow_symlinks} = $self->follow_symlinks;
a7dea640 164 $args{directories} = $self->restart_directory;
165 $args{sleep_interval} = $self->restart_delay;
166 $args{filter} = qr/$self->restart_regex/;
167
168 my $restarter = $subclass->new(
169 %args,
170 start_sub => $self->runner,
171 argv => $self->ARGV,
172 );
173
174 $restarter->run_and_watch;
175 }
176 else {
177 $self->runner;
178 }
179
180
181}
182
183sub runner {
184 my ($self) = shift;
185
186 # If we load this here, then in the case of a restarter, it does not
187 # need to be reloaded for each restart.
188 require Catalyst;
189
190 # If this isn't done, then the Catalyst::Devel tests for the restarter
191 # fail.
192 $| = 1 if $ENV{HARNESS_ACTIVE};
193
194
195 $self->usage if $self->help;
a3ca4468 196 my $app = $self->app;
197 Class::MOP::load_class($app);
a7dea640 198
199 if ( $self->debug ) {
200 $ENV{CATALYST_DEBUG} = 1;
201 }
202
203
a3ca4468 204 $app->run(
205 $self->listen, $self->host,
206 {
4b3881d4 207 'fork' => $self->fork,
208 keepalive => $self->keepalive,
209 background => $self->background,
210 pidfile => $self->pidfile,
615fee7c 211 keepalive => $self->keepalive,
615fee7c 212 follow_symlinks => $self->follow_symlinks,
a3ca4468 213 }
214 );
a3ca4468 215}
5b923b0b 216
a7dea640 217
2e81e132 218no Moose;
219__PACKAGE__->meta->make_immutable;
5b923b0b 220
0ba6e8aa 2211;
e46bf32c 222
223=head1 NAME
224
225[% appprefix %]_server.pl - Catalyst Testserver
226
227=head1 SYNOPSIS
228
229[% appprefix %]_server.pl [options]
230
231 Options:
4b3881d4 232 -d --debug force debug mode
233 -f --fork handle each request in a new process
e46bf32c 234 (defaults to false)
4b3881d4 235 -h --help display this help and exits
236 --host host (defaults to all)
237 -p --port port (defaults to 3000)
238 -k --keepalive enable keep-alive connections
239 -r --restart restart when files get modified
240 (defaults to false)
241 --rd --restartdelay delay between file checks
e46bf32c 242 (ignored if you have Linux::Inotify2 installed)
4b3881d4 243 --rr --restartregex regex match files that trigger
e46bf32c 244 a restart when modified
245 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
4b3881d4 246 --rdir --restartdirectory the directory to search for
e46bf32c 247 modified files, can be set mulitple times
248 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 249 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 250 (defaults to false. this is a no-op on Win32)
4b3881d4 251 --bg --background run the process in the background
252 --pid --pidfile specify filename for pid file
e46bf32c 253
254 See also:
255 perldoc Catalyst::Manual
256 perldoc Catalyst::Manual::Intro
257
258=head1 DESCRIPTION
259
260Run a Catalyst Testserver for this application.
261
262=head1 AUTHORS
263
264Catalyst Contributors, see Catalyst.pm
265
266=head1 COPYRIGHT
267
268This library is free software. You can redistribute it and/or modify
269it under the same terms as Perl itself.
270
271=cut