Commit | Line | Data |
65586a18 |
1 | package Catalyst::Engine::HTTP::Restarter; |
7fa2c9c1 |
2 | use Moose; |
7d9921b1 |
3 | use Moose::Util qw/find_meta/; |
4 | use namespace::clean -except => 'meta'; |
5 | |
7fa2c9c1 |
6 | extends 'Catalyst::Engine::HTTP'; |
0fc2d522 |
7 | |
65586a18 |
8 | use Catalyst::Engine::HTTP::Restarter::Watcher; |
65586a18 |
9 | |
4090e3bb |
10 | around run => sub { |
11 | my $orig = shift; |
65586a18 |
12 | my ( $self, $class, $port, $host, $options ) = @_; |
13 | |
14 | $options ||= {}; |
15 | |
16 | # Setup restarter |
57a87bb3 |
17 | unless ( my $restarter = fork ) { |
65586a18 |
18 | |
19 | # Prepare |
20 | close STDIN; |
21 | close STDOUT; |
1cf1c56a |
22 | |
c03163b8 |
23 | # Best effort if we can't trap compiles.. |
24 | $self->_make_components_mutable($class) |
25 | if !Catalyst::Engine::HTTP::Restarter::Watcher::DETECT_PACKAGE_COMPILATION(); |
7d9921b1 |
26 | |
65586a18 |
27 | my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new( |
ac5c933b |
28 | directory => ( |
29 | $options->{restart_directory} || |
9e800f69 |
30 | File::Spec->catdir( $FindBin::Bin, '..' ) |
31 | ), |
9c71d51d |
32 | follow_symlinks => $options->{follow_symlinks}, |
65586a18 |
33 | regex => $options->{restart_regex}, |
34 | delay => $options->{restart_delay}, |
35 | ); |
36 | |
1cf1c56a |
37 | $host ||= '127.0.0.1'; |
65586a18 |
38 | while (1) { |
1cf1c56a |
39 | |
65586a18 |
40 | # poll for changed files |
41 | my @changed_files = $watcher->watch(); |
1cf1c56a |
42 | |
65586a18 |
43 | # check if our parent process has died |
1cf1c56a |
44 | exit if $^O ne 'MSWin32' and getppid == 1; |
45 | |
65586a18 |
46 | # Restart if any files have changed |
1cf1c56a |
47 | if (@changed_files) { |
65586a18 |
48 | my $files = join ', ', @changed_files; |
49 | print STDERR qq/File(s) "$files" modified, restarting\n\n/; |
1cf1c56a |
50 | |
51 | require IO::Socket::INET; |
52 | require HTTP::Headers; |
53 | require HTTP::Request; |
54 | |
55 | my $client = IO::Socket::INET->new( |
56 | PeerAddr => $host, |
57 | PeerPort => $port |
58 | ) |
49e0f58d |
59 | or die "Can't create client socket (is server running?): ", |
1cf1c56a |
60 | $!; |
61 | |
62 | # build the Kill request |
63 | my $req = |
57a87bb3 |
64 | HTTP::Request->new( 'RESTART', '/', |
1cf1c56a |
65 | HTTP::Headers->new( 'Connection' => 'close' ) ); |
66 | $req->protocol('HTTP/1.0'); |
67 | |
68 | $client->send( $req->as_string ) |
49e0f58d |
69 | or die "Can't send restart instruction: ", $!; |
1cf1c56a |
70 | $client->close(); |
65586a18 |
71 | exit; |
72 | } |
73 | } |
74 | } |
75 | |
4090e3bb |
76 | return $self->$orig( $class, $port, $host, $options ); |
7fa2c9c1 |
77 | }; |
65586a18 |
78 | |
7d9921b1 |
79 | # Naive way of trying to avoid Moose blowing up when you re-require components |
80 | # which have been made immutable. |
81 | sub _make_components_mutable { |
82 | my ($self, $class) = @_; |
83 | |
1ed2055a |
84 | my @metas = map { find_meta($_) } ($class, map { blessed($_) } values %{ $class->components }); |
7d9921b1 |
85 | |
86 | foreach my $meta (@metas) { |
87 | $meta->make_mutable if $meta->is_immutable; |
88 | } |
89 | } |
b9f43019 |
90 | |
65586a18 |
91 | 1; |
92 | __END__ |
93 | |
94 | =head1 NAME |
95 | |
96 | Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine |
97 | |
98 | =head1 SYNOPSIS |
99 | |
100 | script/myapp_server.pl -restart |
101 | |
102 | =head1 DESCRIPTION |
103 | |
104 | The Restarter engine will monitor files in your application for changes |
105 | and restart the server when any changes are detected. |
106 | |
107 | =head1 METHODS |
108 | |
b5ecfcf0 |
109 | =head2 run |
65586a18 |
110 | |
111 | =head1 SEE ALSO |
112 | |
113 | L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>, |
114 | L<Catalyst::Engine>. |
115 | |
116 | =head1 AUTHORS |
117 | |
2f381252 |
118 | Catalyst Contributors, see Catalyst.pm |
65586a18 |
119 | |
120 | =head1 THANKS |
121 | |
122 | Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent. |
123 | |
124 | =head1 COPYRIGHT |
125 | |
126 | This program is free software, you can redistribute it and/or modify it under |
127 | the same terms as Perl itself. |
128 | |
129 | =cut |