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( |
9e800f69 |
22 | directory => ( |
23 | $options->{restart_directory} || |
24 | File::Spec->catdir( $FindBin::Bin, '..' ) |
25 | ), |
9c71d51d |
26 | follow_symlinks => $options->{follow_symlinks}, |
65586a18 |
27 | regex => $options->{restart_regex}, |
28 | delay => $options->{restart_delay}, |
29 | ); |
30 | |
1cf1c56a |
31 | $host ||= '127.0.0.1'; |
65586a18 |
32 | while (1) { |
1cf1c56a |
33 | |
65586a18 |
34 | # poll for changed files |
35 | my @changed_files = $watcher->watch(); |
1cf1c56a |
36 | |
65586a18 |
37 | # check if our parent process has died |
1cf1c56a |
38 | exit if $^O ne 'MSWin32' and getppid == 1; |
39 | |
65586a18 |
40 | # Restart if any files have changed |
1cf1c56a |
41 | if (@changed_files) { |
65586a18 |
42 | my $files = join ', ', @changed_files; |
43 | print STDERR qq/File(s) "$files" modified, restarting\n\n/; |
1cf1c56a |
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 | ) |
49e0f58d |
53 | or die "Can't create client socket (is server running?): ", |
1cf1c56a |
54 | $!; |
55 | |
56 | # build the Kill request |
57 | my $req = |
57a87bb3 |
58 | HTTP::Request->new( 'RESTART', '/', |
1cf1c56a |
59 | HTTP::Headers->new( 'Connection' => 'close' ) ); |
60 | $req->protocol('HTTP/1.0'); |
61 | |
62 | $client->send( $req->as_string ) |
49e0f58d |
63 | or die "Can't send restart instruction: ", $!; |
1cf1c56a |
64 | $client->close(); |
65586a18 |
65 | exit; |
66 | } |
67 | } |
68 | } |
69 | |
70 | return $self->NEXT::run( $class, $port, $host, $options ); |
71 | } |
72 | |
73 | 1; |
74 | __END__ |
75 | |
76 | =head1 NAME |
77 | |
78 | Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine |
79 | |
80 | =head1 SYNOPSIS |
81 | |
82 | script/myapp_server.pl -restart |
83 | |
84 | =head1 DESCRIPTION |
85 | |
86 | The Restarter engine will monitor files in your application for changes |
87 | and restart the server when any changes are detected. |
88 | |
89 | =head1 METHODS |
90 | |
b5ecfcf0 |
91 | =head2 run |
65586a18 |
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 |