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