Fixed some typos
[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 = $$;
1cf1c56a 17
65586a18 18 unless ( $restarter = fork ) {
19
20 # Prepare
21 close STDIN;
22 close STDOUT;
1cf1c56a 23
65586a18 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
1cf1c56a 30 $host ||= '127.0.0.1';
65586a18 31 while (1) {
1cf1c56a 32
65586a18 33 # poll for changed files
34 my @changed_files = $watcher->watch();
1cf1c56a 35
65586a18 36 # check if our parent process has died
1cf1c56a 37 exit if $^O ne 'MSWin32' and getppid == 1;
38
65586a18 39 # Restart if any files have changed
1cf1c56a 40 if (@changed_files) {
65586a18 41 my $files = join ', ', @changed_files;
42 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
1cf1c56a 43
44 require IO::Socket::INET;
45 require HTTP::Headers;
46 require HTTP::Request;
47
48 my $client = IO::Socket::INET->new(
49 PeerAddr => $host,
50 PeerPort => $port
51 )
49e0f58d 52 or die "Can't create client socket (is server running?): ",
1cf1c56a 53 $!;
54
55 # build the Kill request
56 my $req =
57 HTTP::Request->new( 'KILL', '/',
58 HTTP::Headers->new( 'Connection' => 'close' ) );
59 $req->protocol('HTTP/1.0');
60
61 $client->send( $req->as_string )
49e0f58d 62 or die "Can't send restart instruction: ", $!;
1cf1c56a 63 $client->close();
65586a18 64 exit;
65 }
66 }
67 }
68
69 return $self->NEXT::run( $class, $port, $host, $options );
70}
71
721;
73__END__
74
75=head1 NAME
76
77Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
78
79=head1 SYNOPSIS
80
81 script/myapp_server.pl -restart
82
83=head1 DESCRIPTION
84
85The Restarter engine will monitor files in your application for changes
86and restart the server when any changes are detected.
87
88=head1 METHODS
89
90=over 4
91
92=item run
93
94=back
95
96=head1 SEE ALSO
97
98L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
99L<Catalyst::Engine>.
100
101=head1 AUTHORS
102
103Sebastian Riedel, <sri@cpan.org>
104
105Dan Kubb, <dan.kubb-cpan@onautopilot.com>
106
107Andy Grundman, <andy@hybridized.org>
108
109=head1 THANKS
110
111Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
112
113=head1 COPYRIGHT
114
115This program is free software, you can redistribute it and/or modify it under
116the same terms as Perl itself.
117
118=cut