start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
1 package Catalyst::Engine::HTTP::Restarter;
2
3 use Class::C3;
4 use Moose;
5 extends 'Catalyst::Engine::HTTP';
6 no Moose;
7
8 use Catalyst::Engine::HTTP::Restarter::Watcher;
9
10 sub run {
11     my ( $self, $class, $port, $host, $options ) = @_;
12
13     $options ||= {};
14
15     # Setup restarter
16     unless ( my $restarter = fork ) {
17
18         # Prepare
19         close STDIN;
20         close STDOUT;
21
22         my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
23             directory => ( 
24                 $options->{restart_directory} || 
25                 File::Spec->catdir( $FindBin::Bin, '..' )
26             ),
27             follow_symlinks => $options->{follow_symlinks},
28             regex     => $options->{restart_regex},
29             delay     => $options->{restart_delay},
30         );
31
32         $host ||= '127.0.0.1';
33         while (1) {
34
35             # poll for changed files
36             my @changed_files = $watcher->watch();
37
38             # check if our parent process has died
39             exit if $^O ne 'MSWin32' and getppid == 1;
40
41             # Restart if any files have changed
42             if (@changed_files) {
43                 my $files = join ', ', @changed_files;
44                 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
45
46                 require IO::Socket::INET;
47                 require HTTP::Headers;
48                 require HTTP::Request;
49
50                 my $client = IO::Socket::INET->new(
51                     PeerAddr => $host,
52                     PeerPort => $port
53                   )
54                   or die "Can't create client socket (is server running?): ",
55                   $!;
56
57                 # build the Kill request
58                 my $req =
59                   HTTP::Request->new( 'RESTART', '/',
60                     HTTP::Headers->new( 'Connection' => 'close' ) );
61                 $req->protocol('HTTP/1.0');
62
63                 $client->send( $req->as_string )
64                   or die "Can't send restart instruction: ", $!;
65                 $client->close();
66                 exit;
67             }
68         }
69     }
70
71     return $self->next::method( $class, $port, $host, $options );
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