fixed follow_symlinks option in restarter code
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
1 package Catalyst::Script::Server;
2
3 BEGIN {
4     $ENV{CATALYST_ENGINE} ||= 'HTTP';
5     $ENV{CATALYST_SCRIPT_GEN} = 31;
6     require Catalyst::Engine::HTTP;
7 }
8
9 use FindBin qw/$Bin/;
10 use lib "$Bin/../lib";
11 use Pod::Usage;
12 use Moose;
13 use Catalyst::Restarter;
14 #use Catalyst::Engine::HTTP;
15 use namespace::autoclean;
16
17 with 'MooseX::Getopt';
18
19 has debug => (
20     traits => [qw(Getopt)],
21     cmd_aliases => 'd',
22     isa => 'Bool', 
23     is => 'ro',
24     documentation => qq{
25     -d --debug force debug mode    
26     }
27
28 );
29
30 has help => ( 
31     traits => [qw(Getopt)],
32     cmd_aliases => 'h',
33     isa => 'Bool',   
34     is => 'ro', 
35     documentation => qq{
36     -h --help display this help and exits    
37     },  
38 );
39
40 has host => ( 
41     isa => 'Str',    
42     is => 'ro', 
43     , 
44     default =>  "localhost" 
45 );
46
47 has fork => ( 
48     traits => [qw(Getopt)],
49     cmd_aliases => 'f',
50     isa => 'Bool',
51     is => 'ro', 
52      
53 );
54
55 has listen => ( 
56     traits => [qw(Getopt)],
57     cmd_aliases => 'l',
58     isa => 'Int',
59     is => 'ro', 
60     , 
61     default => "3000" 
62 );
63
64 has pidfile => ( 
65     traits => [qw(Getopt)],
66     cmd_aliases => 'pid',
67     isa => 'Str',    
68     is => 'ro', 
69      
70 );
71
72 has keepalive => ( 
73     traits => [qw(Getopt)],
74     cmd_aliases => 'k',
75     isa => 'Bool',   
76     is => 'ro', 
77     , 
78      
79 );
80
81 has background => ( 
82     traits => [qw(Getopt)],
83     cmd_aliases => 'bg',
84     isa => 'Bool',   
85     is => 'ro', 
86 );
87
88
89 has _app => ( 
90     reader   => 'app', 
91     init_arg => 'app',
92     traits => [qw(NoGetopt)],
93     isa => 'Str',    
94     is => 'ro', 
95 ); 
96
97 has restart => (
98     traits => [qw(Getopt)],
99     cmd_aliases => 'r', 
100     isa => 'Bool',   
101     is => 'ro', 
102      
103 ); 
104
105 has restart_directory => (
106     traits => [qw(Getopt)],
107     cmd_aliases => 'rdir',
108     isa => 'Str',
109     is  => 'ro',
110 );
111
112 has restart_delay => ( 
113     traits => [qw(Getopt)],
114     cmd_aliases => 'rdel',
115     isa => 'Int',    
116     is => 'ro', 
117      
118 );
119
120 has restart_regex => ( 
121     traits => [qw(Getopt)],
122     cmd_aliases => 'rxp',
123     isa => 'Str',    
124     is => 'ro', 
125      
126 );
127
128 has follow_symlinks => ( 
129     traits => [qw(Getopt)],
130     cmd_aliases => 'sym',
131     isa => 'Bool',   
132     is => 'ro', 
133      
134 );
135
136 sub usage {
137     my ($self) = shift;
138     
139     return pod2usage();
140
141 }
142
143 my @argv = @ARGV;
144
145 sub run {
146     my $self = shift;
147     
148     $self->usage if $self->help;
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} = $self->follow_symlinks;
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  
183 sub 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;
196     my $app = $self->app;
197     Class::MOP::load_class($app);
198
199     if ( $self->debug ) {
200         $ENV{CATALYST_DEBUG} = 1;
201     }
202
203     
204     $app->run(
205         $self->listen, $self->host,
206         {  
207            'fork'            => $self->fork,
208            keepalive         => $self->keepalive,
209            background        => $self->background,
210            pidfile           => $self->pidfile,
211            keepalive         => $self->keepalive,
212            restart           => $self->restart,
213            restart_delay     => $self->restart_delay,
214            restart_regex     => qr/$self->restart_regex/,
215            restart_directory => $self->restart_directory,
216            follow_symlinks   => $self->follow_symlinks,
217         }  
218     );
219 }
220
221
222 no Moose;
223 __PACKAGE__->meta->make_immutable;
224
225 1;
226
227 =head1 NAME
228
229 [% appprefix %]_server.pl - Catalyst Testserver
230
231 =head1 SYNOPSIS
232
233 [% appprefix %]_server.pl [options]
234
235  Options:
236    -d     --debug          force debug mode
237    -f     --fork           handle each request in a new process
238                       (defaults to false)
239    -h     --help           display this help and exits
240           --host           host (defaults to all)
241    -p     --port           port (defaults to 3000)
242    -k     --keepalive      enable keep-alive connections
243    -r     --restart        restart when files get modified
244                        (defaults to false)
245    --rd   --restartdelay  delay between file checks
246                       (ignored if you have Linux::Inotify2 installed)
247    --rr   --restartregex  regex match files that trigger
248                       a restart when modified
249                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
250    --rdir --restartdirectory  the directory to search for
251                       modified files, can be set mulitple times
252                       (defaults to '[SCRIPT_DIR]/..')
253    --sym  --follow_symlinks   follow symlinks in search directories
254                       (defaults to false. this is a no-op on Win32)
255    --bg   --background        run the process in the background
256    --pid  --pidfile           specify filename for pid file
257
258  See also:
259    perldoc Catalyst::Manual
260    perldoc Catalyst::Manual::Intro
261
262 =head1 DESCRIPTION
263
264 Run a Catalyst Testserver for this application.
265
266 =head1 AUTHORS
267
268 Catalyst Contributors, see Catalyst.pm
269
270 =head1 COPYRIGHT
271
272 This library is free software. You can redistribute it and/or modify
273 it under the same terms as Perl itself.
274
275 =cut