Catalyst::Restarter::Forking: clear watcher in child process
[catagits/Catalyst-Devel.git] / lib / Catalyst / Restarter / Forking.pm
1 package Catalyst::Restarter::Forking;
2
3 use Moose;
4
5 extends 'Catalyst::Restarter';
6
7 has _child => (
8     is  => 'rw',
9     isa => 'Int',
10 );
11
12
13 sub _fork_and_start {
14     my $self = shift;
15
16     if ( my $pid = fork ) {
17         $self->_child($pid);
18     }
19     else {
20         # Only the parent process needs to watch for changes, so the child
21         # should release any resources held by the watcher:
22         $self->_clear_watcher;
23         $self->start_sub->();
24     }
25 }
26
27 sub _kill_child {
28     my $self = shift;
29
30     return unless $self->_child;
31
32     return unless kill 0, $self->_child;
33
34     die "Cannot send INT signal to ", $self->_child, ": $!"
35         unless kill 'INT', $self->_child;
36     # If we don't wait for the child to exit, we could attempt to
37     # start a new server before the old one has given up the port it
38     # was listening on.
39     wait;
40 }
41
42 __PACKAGE__->meta->make_immutable;
43
44 1;
45
46 __END__
47
48 =head1 NAME
49
50 Catalyst::Restarter::Forking - Forks and restarts the child process
51
52 =head1 DESCRIPTION
53
54 This class forks and runs the server in a child process. When it needs
55 to restart, it kills the child and creates a new one.
56
57 =head1 SEE ALSO
58
59 L<Catalyst::Restarter>, L<Catalyst>, <File::ChangeNotify>
60
61 =head1 AUTHORS
62
63 Catalyst Contributors, see Catalyst.pm
64
65 =head1 COPYRIGHT
66
67 This program is free software, you can redistribute it and/or modify
68 it under the same terms as Perl itself.
69
70 =cut