Commit | Line | Data |
65586a18 |
1 | package Catalyst::Engine::HTTP::Restarter; |
2 | |
7fa2c9c1 |
3 | use Moose; |
4 | extends 'Catalyst::Engine::HTTP'; |
0fc2d522 |
5 | |
65586a18 |
6 | use Catalyst::Engine::HTTP::Restarter::Watcher; |
65586a18 |
7 | |
4090e3bb |
8 | around run => sub { |
9 | my $orig = shift; |
65586a18 |
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( |
ac5c933b |
22 | directory => ( |
23 | $options->{restart_directory} || |
9e800f69 |
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 | |
4090e3bb |
70 | return $self->$orig( $class, $port, $host, $options ); |
71 | no Moose; |
7fa2c9c1 |
72 | }; |
65586a18 |
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 | |
b5ecfcf0 |
92 | =head2 run |
65586a18 |
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 | |
2f381252 |
101 | Catalyst Contributors, see Catalyst.pm |
65586a18 |
102 | |
103 | =head1 THANKS |
104 | |
105 | Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent. |
106 | |
107 | =head1 COPYRIGHT |
108 | |
109 | This program is free software, you can redistribute it and/or modify it under |
110 | the same terms as Perl itself. |
111 | |
112 | =cut |