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