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