4691cad9902bb3851102ffdf0b2ae7e7924b5adb
[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
6 use Catalyst::Engine::HTTP::Restarter::Watcher;
7
8 around run => sub {
9     my $orig = shift;
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->$orig( $class, $port, $host, $options );
71     no Moose;
72 };
73
74 1;
75 __END__
76
77 =head1 NAME
78
79 Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
80
81 =head1 SYNOPSIS
82
83     script/myapp_server.pl -restart
84
85 =head1 DESCRIPTION
86
87 The Restarter engine will monitor files in your application for changes
88 and restart the server when any changes are detected.
89
90 =head1 METHODS
91
92 =head2 run
93
94 =head1 SEE ALSO
95
96 L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
97 L<Catalyst::Engine>.
98
99 =head1 AUTHORS
100
101 Sebastian Riedel, <sri@cpan.org>
102
103 Dan Kubb, <dan.kubb-cpan@onautopilot.com>
104
105 Andy Grundman, <andy@hybridized.org>
106
107 =head1 THANKS
108
109 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
110
111 =head1 COPYRIGHT
112
113 This program is free software, you can redistribute it and/or modify it under
114 the same terms as Perl itself.
115
116 =cut