Commit | Line | Data |
65586a18 |
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 |
57a87bb3 |
15 | unless ( my $restarter = fork ) { |
65586a18 |
16 | |
17 | # Prepare |
18 | close STDIN; |
19 | close STDOUT; |
1cf1c56a |
20 | |
65586a18 |
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 | |
1cf1c56a |
27 | $host ||= '127.0.0.1'; |
65586a18 |
28 | while (1) { |
1cf1c56a |
29 | |
65586a18 |
30 | # poll for changed files |
31 | my @changed_files = $watcher->watch(); |
1cf1c56a |
32 | |
65586a18 |
33 | # check if our parent process has died |
1cf1c56a |
34 | exit if $^O ne 'MSWin32' and getppid == 1; |
35 | |
65586a18 |
36 | # Restart if any files have changed |
1cf1c56a |
37 | if (@changed_files) { |
65586a18 |
38 | my $files = join ', ', @changed_files; |
39 | print STDERR qq/File(s) "$files" modified, restarting\n\n/; |
1cf1c56a |
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 | ) |
49e0f58d |
49 | or die "Can't create client socket (is server running?): ", |
1cf1c56a |
50 | $!; |
51 | |
52 | # build the Kill request |
53 | my $req = |
57a87bb3 |
54 | HTTP::Request->new( 'RESTART', '/', |
1cf1c56a |
55 | HTTP::Headers->new( 'Connection' => 'close' ) ); |
56 | $req->protocol('HTTP/1.0'); |
57 | |
58 | $client->send( $req->as_string ) |
49e0f58d |
59 | or die "Can't send restart instruction: ", $!; |
1cf1c56a |
60 | $client->close(); |
65586a18 |
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 | |
b5ecfcf0 |
87 | =head2 run |
65586a18 |
88 | |
89 | =head1 SEE ALSO |
90 | |
91 | L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>, |
92 | L<Catalyst::Engine>. |
93 | |
94 | =head1 AUTHORS |
95 | |
96 | Sebastian Riedel, <sri@cpan.org> |
97 | |
98 | Dan Kubb, <dan.kubb-cpan@onautopilot.com> |
99 | |
100 | Andy Grundman, <andy@hybridized.org> |
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 |