Refactored restarter into a subclass of Engine::HTTP, improved restarter performance
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
1 package Catalyst::Engine::HTTP::Restarter;
2
3 use strict;
4 use warnings;
5 use base 'Catalyst::Engine::HTTP';
6 use Catalyst::Engine::HTTP::Restarter::Watcher;
7 use NEXT;
8
9 sub 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
50 1;
51 __END__
52
53 =head1 NAME
54
55 Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
56
57 =head1 SYNOPSIS
58
59     script/myapp_server.pl -restart
60
61 =head1 DESCRIPTION
62
63 The Restarter engine will monitor files in your application for changes
64 and 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
76 L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
77 L<Catalyst::Engine>.
78
79 =head1 AUTHORS
80
81 Sebastian Riedel, <sri@cpan.org>
82
83 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
84
85 Andy Grundman, <andy@hybridized.org>
86
87 =head1 THANKS
88
89 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
90
91 =head1 COPYRIGHT
92
93 This program is free software, you can redistribute it and/or modify it under
94 the same terms as Perl itself.
95
96 =cut