- Nuked LocalRegex.pm
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
CommitLineData
65586a18 1package Catalyst::Engine::HTTP::Restarter;
2
3use strict;
4use warnings;
5use base 'Catalyst::Engine::HTTP';
6use Catalyst::Engine::HTTP::Restarter::Watcher;
7use NEXT;
8
9sub run {
10 my ( $self, $class, $port, $host, $options ) = @_;
11
12 $options ||= {};
13
14 # Setup restarter
15 my $restarter;
16 my $parent = $$;
17
18 unless ( $restarter = fork ) {
19
20 # Prepare
21 close STDIN;
22 close STDOUT;
23
24 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
25 directory => File::Spec->catdir( $FindBin::Bin, '..' ),
26 regex => $options->{restart_regex},
27 delay => $options->{restart_delay},
28 );
29
30 while (1) {
31 # poll for changed files
32 my @changed_files = $watcher->watch();
33
34 # check if our parent process has died
35 exit if ( getppid == 1 );
36
37 # Restart if any files have changed
38 if ( @changed_files ) {
39 my $files = join ', ', @changed_files;
40 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
41 kill( 1, $parent );
42 exit;
43 }
44 }
45 }
46
47 return $self->NEXT::run( $class, $port, $host, $options );
48}
49
501;
51__END__
52
53=head1 NAME
54
55Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
56
57=head1 SYNOPSIS
58
59 script/myapp_server.pl -restart
60
61=head1 DESCRIPTION
62
63The Restarter engine will monitor files in your application for changes
64and restart the server when any changes are detected.
65
66=head1 METHODS
67
68=over 4
69
70=item run
71
72=back
73
74=head1 SEE ALSO
75
76L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
77L<Catalyst::Engine>.
78
79=head1 AUTHORS
80
81Sebastian Riedel, <sri@cpan.org>
82
83Dan Kubb, <dan.kubb-cpan@onautopilot.com>
84
85Andy Grundman, <andy@hybridized.org>
86
87=head1 THANKS
88
89Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
90
91=head1 COPYRIGHT
92
93This program is free software, you can redistribute it and/or modify it under
94the same terms as Perl itself.
95
96=cut