Add docs, and don't bother to make components mutable if you can detect packages...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
1 package Catalyst::Engine::HTTP::Restarter;
2 use Moose;
3 use Moose::Util qw/find_meta/;
4 use namespace::clean -except => 'meta';
5
6 extends 'Catalyst::Engine::HTTP';
7
8 use Catalyst::Engine::HTTP::Restarter::Watcher;
9
10 around run => sub {
11     my $orig = shift;
12     my ( $self, $class, $port, $host, $options ) = @_;
13
14     $options ||= {};
15
16     # Setup restarter
17     unless ( my $restarter = fork ) {
18
19         # Prepare
20         close STDIN;
21         close STDOUT;
22
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();
26
27         my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
28             directory => ( 
29                 $options->{restart_directory} || 
30                 File::Spec->catdir( $FindBin::Bin, '..' )
31             ),
32             follow_symlinks => $options->{follow_symlinks},
33             regex     => $options->{restart_regex},
34             delay     => $options->{restart_delay},
35         );
36
37         $host ||= '127.0.0.1';
38         while (1) {
39
40             # poll for changed files
41             my @changed_files = $watcher->watch();
42
43             # check if our parent process has died
44             exit if $^O ne 'MSWin32' and getppid == 1;
45
46             # Restart if any files have changed
47             if (@changed_files) {
48                 my $files = join ', ', @changed_files;
49                 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
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                   )
59                   or die "Can't create client socket (is server running?): ",
60                   $!;
61
62                 # build the Kill request
63                 my $req =
64                   HTTP::Request->new( 'RESTART', '/',
65                     HTTP::Headers->new( 'Connection' => 'close' ) );
66                 $req->protocol('HTTP/1.0');
67
68                 $client->send( $req->as_string )
69                   or die "Can't send restart instruction: ", $!;
70                 $client->close();
71                 exit;
72             }
73         }
74     }
75
76     return $self->$orig( $class, $port, $host, $options );
77 };
78
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
84     my @metas = map { find_meta($_) } ($class, map { blessed($_) } values %{ $class->components });
85
86     foreach my $meta (@metas) {
87         $meta->make_mutable if $meta->is_immutable;
88     }
89 }
90
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
109 =head2 run
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
118 Catalyst Contributors, see Catalyst.pm
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