| 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__ |