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