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