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