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