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