revert MyApp instantiation
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Restarter.pm
CommitLineData
65586a18 1package Catalyst::Engine::HTTP::Restarter;
2
6f1f968a 3use MRO::Compat;
4use mro 'c3';
7fa2c9c1 5use Moose;
6extends 'Catalyst::Engine::HTTP';
0fc2d522 7no Moose;
8
65586a18 9use Catalyst::Engine::HTTP::Restarter::Watcher;
65586a18 10
0fc2d522 11sub run {
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
65586a18 23 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
ac5c933b 24 directory => (
25 $options->{restart_directory} ||
9e800f69 26 File::Spec->catdir( $FindBin::Bin, '..' )
27 ),
9c71d51d 28 follow_symlinks => $options->{follow_symlinks},
65586a18 29 regex => $options->{restart_regex},
30 delay => $options->{restart_delay},
31 );
32
1cf1c56a 33 $host ||= '127.0.0.1';
65586a18 34 while (1) {
1cf1c56a 35
65586a18 36 # poll for changed files
37 my @changed_files = $watcher->watch();
1cf1c56a 38
65586a18 39 # check if our parent process has died
1cf1c56a 40 exit if $^O ne 'MSWin32' and getppid == 1;
41
65586a18 42 # Restart if any files have changed
1cf1c56a 43 if (@changed_files) {
65586a18 44 my $files = join ', ', @changed_files;
45 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
1cf1c56a 46
47 require IO::Socket::INET;
48 require HTTP::Headers;
49 require HTTP::Request;
50
51 my $client = IO::Socket::INET->new(
52 PeerAddr => $host,
53 PeerPort => $port
54 )
49e0f58d 55 or die "Can't create client socket (is server running?): ",
1cf1c56a 56 $!;
57
58 # build the Kill request
59 my $req =
57a87bb3 60 HTTP::Request->new( 'RESTART', '/',
1cf1c56a 61 HTTP::Headers->new( 'Connection' => 'close' ) );
62 $req->protocol('HTTP/1.0');
63
64 $client->send( $req->as_string )
49e0f58d 65 or die "Can't send restart instruction: ", $!;
1cf1c56a 66 $client->close();
65586a18 67 exit;
68 }
69 }
70 }
71
0fc2d522 72 return $self->next::method( $class, $port, $host, $options );
7fa2c9c1 73};
65586a18 74
751;
76__END__
77
78=head1 NAME
79
80Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
81
82=head1 SYNOPSIS
83
84 script/myapp_server.pl -restart
85
86=head1 DESCRIPTION
87
88The Restarter engine will monitor files in your application for changes
89and restart the server when any changes are detected.
90
91=head1 METHODS
92
b5ecfcf0 93=head2 run
65586a18 94
95=head1 SEE ALSO
96
97L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
98L<Catalyst::Engine>.
99
100=head1 AUTHORS
101
102Sebastian Riedel, <sri@cpan.org>
103
104Dan Kubb, <dan.kubb-cpan@onautopilot.com>
105
106Andy Grundman, <andy@hybridized.org>
107
108=head1 THANKS
109
110Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
111
112=head1 COPYRIGHT
113
114This program is free software, you can redistribute it and/or modify it under
115the same terms as Perl itself.
116
117=cut