reverting (most of) the whitespace changes
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
CommitLineData
65586a18 1package Catalyst::Engine::HTTP::Restarter;
2
7fa2c9c1 3use Moose;
4extends 'Catalyst::Engine::HTTP';
65586a18 5use Catalyst::Engine::HTTP::Restarter::Watcher;
65586a18 6
7fa2c9c1 7around run => sub {
8 my $orig = shift;
65586a18 9 my ( $self, $class, $port, $host, $options ) = @_;
10
11 $options ||= {};
12
13 # Setup restarter
57a87bb3 14 unless ( my $restarter = fork ) {
65586a18 15
16 # Prepare
17 close STDIN;
18 close STDOUT;
1cf1c56a 19
65586a18 20 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
ac5c933b 21 directory => (
22 $options->{restart_directory} ||
9e800f69 23 File::Spec->catdir( $FindBin::Bin, '..' )
24 ),
9c71d51d 25 follow_symlinks => $options->{follow_symlinks},
65586a18 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 =
57a87bb3 57 HTTP::Request->new( 'RESTART', '/',
1cf1c56a 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
7fa2c9c1 69 return $self->$orig( $class, $port, $host, $options );
70};
65586a18 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
b5ecfcf0 90=head2 run
65586a18 91
92=head1 SEE ALSO
93
94L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
95L<Catalyst::Engine>.
96
97=head1 AUTHORS
98
99Sebastian Riedel, <sri@cpan.org>
100
101Dan Kubb, <dan.kubb-cpan@onautopilot.com>
102
103Andy Grundman, <andy@hybridized.org>
104
105=head1 THANKS
106
107Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
108
109=head1 COPYRIGHT
110
111This program is free software, you can redistribute it and/or modify it under
112the same terms as Perl itself.
113
114=cut