start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
CommitLineData
65586a18 1package Catalyst::Engine::HTTP::Restarter;
2
0fc2d522 3use Class::C3;
7fa2c9c1 4use Moose;
5extends 'Catalyst::Engine::HTTP';
0fc2d522 6no Moose;
7
65586a18 8use Catalyst::Engine::HTTP::Restarter::Watcher;
65586a18 9
0fc2d522 10sub run {
65586a18 11 my ( $self, $class, $port, $host, $options ) = @_;
12
13 $options ||= {};
14
15 # Setup restarter
57a87bb3 16 unless ( my $restarter = fork ) {
65586a18 17
18 # Prepare
19 close STDIN;
20 close STDOUT;
1cf1c56a 21
65586a18 22 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
ac5c933b 23 directory => (
24 $options->{restart_directory} ||
9e800f69 25 File::Spec->catdir( $FindBin::Bin, '..' )
26 ),
9c71d51d 27 follow_symlinks => $options->{follow_symlinks},
65586a18 28 regex => $options->{restart_regex},
29 delay => $options->{restart_delay},
30 );
31
1cf1c56a 32 $host ||= '127.0.0.1';
65586a18 33 while (1) {
1cf1c56a 34
65586a18 35 # poll for changed files
36 my @changed_files = $watcher->watch();
1cf1c56a 37
65586a18 38 # check if our parent process has died
1cf1c56a 39 exit if $^O ne 'MSWin32' and getppid == 1;
40
65586a18 41 # Restart if any files have changed
1cf1c56a 42 if (@changed_files) {
65586a18 43 my $files = join ', ', @changed_files;
44 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
1cf1c56a 45
46 require IO::Socket::INET;
47 require HTTP::Headers;
48 require HTTP::Request;
49
50 my $client = IO::Socket::INET->new(
51 PeerAddr => $host,
52 PeerPort => $port
53 )
49e0f58d 54 or die "Can't create client socket (is server running?): ",
1cf1c56a 55 $!;
56
57 # build the Kill request
58 my $req =
57a87bb3 59 HTTP::Request->new( 'RESTART', '/',
1cf1c56a 60 HTTP::Headers->new( 'Connection' => 'close' ) );
61 $req->protocol('HTTP/1.0');
62
63 $client->send( $req->as_string )
49e0f58d 64 or die "Can't send restart instruction: ", $!;
1cf1c56a 65 $client->close();
65586a18 66 exit;
67 }
68 }
69 }
70
0fc2d522 71 return $self->next::method( $class, $port, $host, $options );
7fa2c9c1 72};
65586a18 73
741;
75__END__
76
77=head1 NAME
78
79Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
80
81=head1 SYNOPSIS
82
83 script/myapp_server.pl -restart
84
85=head1 DESCRIPTION
86
87The Restarter engine will monitor files in your application for changes
88and restart the server when any changes are detected.
89
90=head1 METHODS
91
b5ecfcf0 92=head2 run
65586a18 93
94=head1 SEE ALSO
95
96L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
97L<Catalyst::Engine>.
98
99=head1 AUTHORS
100
101Sebastian Riedel, <sri@cpan.org>
102
103Dan Kubb, <dan.kubb-cpan@onautopilot.com>
104
105Andy Grundman, <andy@hybridized.org>
106
107=head1 THANKS
108
109Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
110
111=head1 COPYRIGHT
112
113This program is free software, you can redistribute it and/or modify it under
114the same terms as Perl itself.
115
116=cut