| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package POE::Component::Supervisor; | ||||||||||||||||||||
| 4 | 23 | ▶ | use MooseX::POE; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 23 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 23 | ▶ | use POE::Component::Supervisor::Supervised; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 23 | ▶ | use POE::Component::Supervisor::Handle; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 23 | ▶ | use Devel::PartialDump; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 23 | ▶ | use Hash::Util::FieldHash::Compat qw(idhash); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 23 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 23 | our $VERSION = "0.08"; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 23 | with qw( | |||||||||||||||||||
| 20 | POE::Component::Supervisor::Interface | ||||||||||||||||||||
| 21 | MooseX::POE::Aliased | ||||||||||||||||||||
| 22 | POE::Component::Supervisor::LogDispatch | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | sub run { | ||||||||||||||||||||
| 26 | 0 | ▶ | my $self = shift->new(@_); | ||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 27 | 0 | $poe_kernel->run; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # by default when all the children die we exit as well | ||||||||||||||||||||
| 31 | 93 | ▶ | sub _build_alias { undef } | ||||||||||||||||||
|
Subroutine
_build_alias: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 23 | has restart_policy => ( | |||||||||||||||||||
| 34 | isa => enum(__PACKAGE__ . "::RestartPolicy" => qw(one all rest)), | ||||||||||||||||||||
| 35 | is => "rw", | ||||||||||||||||||||
| 36 | default => "one", | ||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | has children => ( | ||||||||||||||||||||
| 40 | isa => "ArrayRef", | ||||||||||||||||||||
| 41 | init_arg => undef, | ||||||||||||||||||||
| 42 | is => "ro", | ||||||||||||||||||||
| 43 | auto_deref => 1, | ||||||||||||||||||||
| 44 | 93 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 23 | ); | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 23 | has _children_tmp => ( | |||||||||||||||||||
| 48 | isa => "ArrayRef", | ||||||||||||||||||||
| 49 | is => "rw", | ||||||||||||||||||||
| 50 | init_arg => undef, | ||||||||||||||||||||
| 51 | clearer => "_clear_children_tmp", | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 23 | has _last_child_id => ( | |||||||||||||||||||
| 55 | isa => "Int", | ||||||||||||||||||||
| 56 | is => "rw", | ||||||||||||||||||||
| 57 | default => 0, | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub _next_child_id { | ||||||||||||||||||||
| 61 | 621 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_next_child_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 621 | $self->_last_child_id( $self->_last_child_id + 1 ); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | has _children_hash => ( | ||||||||||||||||||||
| 66 | isa => "HashRef", | ||||||||||||||||||||
| 67 | is => "ro", | ||||||||||||||||||||
| 68 | init_arg => undef, | ||||||||||||||||||||
| 69 | 93 | ▶ | default => sub { idhash my %h }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 23 | ); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | sub _child_id { | ||||||||||||||||||||
| 73 | 4024 | ▶ | my ( $self, $child ) = @_; | ||||||||||||||||||
|
Subroutine
_child_id: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 4024 | ▶ | if ( defined ( my $id = $self->_children_hash->{$child}{id} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 4024 | return $id; | |||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||
| 78 | 0 | confess "unknown child $child"; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | sub _child_handle { | ||||||||||||||||||||
| 83 | 533 | ▶ | my ( $self, $child ) = @_; | ||||||||||||||||||
|
Subroutine
_child_handle: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 84 | 533 | $self->_children_hash->{$child}{handle}; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # used to track which children are currently being stopped for the purpose of | ||||||||||||||||||||
| 88 | # restarting, because we first have to stop everything and then we start them again | ||||||||||||||||||||
| 89 | has _stopping_for_restart => ( | ||||||||||||||||||||
| 90 | isa => "HashRef", | ||||||||||||||||||||
| 91 | is => "ro", | ||||||||||||||||||||
| 92 | init_arg => undef, | ||||||||||||||||||||
| 93 | 93 | ▶ | default => sub { idhash my %h }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 94 | 23 | ); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # when children that are being restarted have stopped they are tracked here | ||||||||||||||||||||
| 97 | # when the last child is stopped this collection of children will be started based on the order of 'children' | ||||||||||||||||||||
| 98 | has _pending_restart => ( | ||||||||||||||||||||
| 99 | isa => "HashRef", | ||||||||||||||||||||
| 100 | is => "ro", | ||||||||||||||||||||
| 101 | init_arg => undef, | ||||||||||||||||||||
| 102 | 93 | ▶ | default => sub { idhash my %h }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 103 | 23 | ); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | sub START { | ||||||||||||||||||||
| 106 | 93 | ▶ | my ( $self, $kernel ) = @_[OBJECT, KERNEL]; | ||||||||||||||||||
|
Subroutine
START: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 93 | $kernel->sig( DIE => "exception" ); | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 93 | $self->logger->info("starting supervisor $self in process $$"); | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 93 | ▶ | if ( my $children = $self->_children_tmp ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 93 | $self->_clear_children_tmp; | |||||||||||||||||||
| 114 | 93 | $self->start(@$children); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | sub STOP { | ||||||||||||||||||||
| 119 | 93 | ▶ | my $self = $_[OBJECT]; | ||||||||||||||||||
|
Subroutine
STOP: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 93 | $self->logger->info("stopping supervisor $self in process $$"); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | event exception => sub { | ||||||||||||||||||||
| 125 | 9 | ▶ | my ( $self, $error_info ) = @_[OBJECT, ARG1]; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
exception: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 9 | $self->logger->error("Error in supervisor child session, event $error_info->{event} of $error_info->{dest_session}: $error_info->{error_str}, sent from $error_info->{source_session} state $error_info->{from_state} at $error_info->{file} line $error_info->{line}"); | |||||||||||||||||||
| 128 | 23 | }; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | sub _register_child { | ||||||||||||||||||||
| 131 | 621 | ▶ | my ( $self, $new_child ) = @_; | ||||||||||||||||||
|
Subroutine
_register_child: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 621 | $self->logger->debug("registering child $new_child"); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 621 | ▶ | $self->_children_hash->{$new_child} ||= do { | ||||||||||||||||||
|
Condition33%1/3
$self->_children_hash->{$new_child} ||= do {
MC/DC0%0/2
$self->_children_hash->{$new_child} ||= do {
push @{$self->children;}, $new_child;
$self->_new_child_registration($new_child)
}
$self->_children_hash->{$new_child}
do {
push @{$self->children;}, $new_child;
$self->_new_child_registration($new_child)
}
Truth table33%1/3
A ||= B
A= $self->_children_hash->{$new_child}B= do {
push @{$self->children;}, $new_child;
$self->_new_child_registration($new_child)
}
| |||||||||||||||||||||
| 136 | 621 | push @{ $self->children }, $new_child; | |||||||||||||||||||
| 137 | 621 | $self->_new_child_registration($new_child); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | sub _new_child_registration { | ||||||||||||||||||||
| 142 | 621 | ▶ | my ( $self, $new_child ) = @_; | ||||||||||||||||||
|
Subroutine
_new_child_registration: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 143 | 621 | return { id => $self->_next_child_id }; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | sub _unregister_child { | ||||||||||||||||||||
| 147 | 493 | ▶ | my ( $self, $child ) = @_; | ||||||||||||||||||
|
Subroutine
_unregister_child: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 493 | $self->logger->debug("unregistering child $child"); | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 493 | ▶ | if ( delete $self->_children_hash->{$child} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 493 | @{ $self->children } = grep { $_ != $child } @{ $self->children }; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | sub BUILD { | ||||||||||||||||||||
| 158 | 93 | ▶ | my ( $self, $params ) = @_; | ||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 93 | ▶ | if ( my $children = $params->{children} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 93 | $self->_children_tmp($children); | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | sub start { | ||||||||||||||||||||
| 166 | 93 | ▶ | my ( $self, @children ) = @_; | ||||||||||||||||||
|
Subroutine
start: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 93 | foreach my $child ( @children ) { | |||||||||||||||||||
| 169 | 621 | ▶ | next if $self->_children_hash->{$child}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 621 | $self->_register_child($child); | |||||||||||||||||||
| 171 | 621 | $self->yield( spawn => $child ); | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | sub stop { | ||||||||||||||||||||
| 176 | 266 | ▶ | my ( $self, @children ) = @_; | ||||||||||||||||||
|
Subroutine
stop: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 266 | ▶ | @children = reverse $self->children unless @children; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 266 | foreach my $child ( @children ) { | |||||||||||||||||||
| 181 | 477 | ▶ | if ( my $entry = $self->_children_hash->{$child} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 477 | ▶ | if ( my $handle = $entry->{handle} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 477 | $entry->{stopping} = 1; | |||||||||||||||||||
| 184 | 477 | $entry->{handle}->stop; | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | # remove it from the children list, so that it isn't restarted due to a | ||||||||||||||||||||
| 187 | # 'rest' or 'all' policy because of some other childs' exit | ||||||||||||||||||||
| 188 | # _unregister_child will eventually try to do this too, but that's OK | ||||||||||||||||||||
| 189 | # because it *should* do it if the child has been stopped unexpectedly | ||||||||||||||||||||
| 190 | # and is temporary/transient | ||||||||||||||||||||
| 191 | 477 | @{ $self->children } = grep { $_ != $child } @{ $self->children }; | |||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||
| 193 | # it's already dead, just delete it | ||||||||||||||||||||
| 194 | 0 | $self->_unregister_child($child); | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | sub notify_spawned { | ||||||||||||||||||||
| 201 | 1085 | ▶ | my ( $self, @args ) = @_; | ||||||||||||||||||
|
Subroutine
notify_spawned: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 202 | 1085 | $self->yield( spawned => @args ); | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | sub notify_stopped { | ||||||||||||||||||||
| 206 | 1042 | ▶ | my ( $self, @args ) = @_; | ||||||||||||||||||
|
Subroutine
notify_stopped: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 207 | 1042 | $self->yield( stopped => @args ); | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | event spawned => sub { | ||||||||||||||||||||
| 211 | 1052 | ▶ | my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
spawned: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 1052 | $kernel->refcount_increment( $self->get_session_id(), "handles" ); | |||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 1052 | $self->logger->info("child " . $self->_child_id($child) . " spawned " . Devel::PartialDump::dump(@args)); | |||||||||||||||||||
| 216 | 23 | }; | |||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | event spawn => sub { | ||||||||||||||||||||
| 219 | 621 | ▶ | my ( $self, $child ) = @_[OBJECT, ARG0 ]; | ||||||||||||||||||
|
Subroutine
spawn: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 621 | $self->logger->debug("instructing child " . $self->_child_id($child) . " to spawn"); | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | 621 | $self->_children_hash->{$child}{handle} = $child->spawn( supervisor => $self ); | |||||||||||||||||||
| 224 | 23 | }; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | event respawn => sub { | ||||||||||||||||||||
| 227 | 291 | ▶ | my ( $self, $child ) = @_[OBJECT, ARG0]; | ||||||||||||||||||
|
Subroutine
respawn: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 291 | $self->_pending_restart->{$child} = $child; | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 291 | ▶ | if ( scalar keys %{ $self->_stopping_for_restart } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 232 | # if we're waiting on more children to exit, just mark this child as ready to restart | ||||||||||||||||||||
| 233 | 133 | $self->logger->debug("child " . $self->_child_id($child) . " respawn postponed, other children still not stopped"); | |||||||||||||||||||
| 234 | } else { | ||||||||||||||||||||
| 235 | # otherwise we can now restart all the children which are ready to be restarted | ||||||||||||||||||||
| 236 | 158 | $self->logger->debug("no more unstopped children, ready to respawn"); | |||||||||||||||||||
| 237 | 158 | my @children_to_restart = grep { defined } delete @{ $self->_pending_restart }{ $self->children }; | |||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 158 | foreach my $child ( @children_to_restart ) { | |||||||||||||||||||
| 240 | 291 | $self->yield( _respawn => $child ); | |||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | 23 | }; | |||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | event _respawn => sub { | ||||||||||||||||||||
| 246 | 341 | ▶ | my ( $self, $child ) = @_[OBJECT, ARG0]; | ||||||||||||||||||
|
Subroutine
_respawn: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | 341 | $self->logger->info("respawning child " . $self->_child_id($child)); | |||||||||||||||||||
| 249 | 341 | $self->_children_hash->{$child}{handle} = $child->respawn( supervisor => $self ); | |||||||||||||||||||
| 250 | 23 | }; | |||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | event stopped => sub { | ||||||||||||||||||||
| 253 | 663 | ▶ | my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
stopped: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 663 | $kernel->refcount_decrement( $self->get_session_id(), "handles" ); | |||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | 663 | delete $self->_children_hash->{$child}{handle}; | |||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | 663 | ▶ | if ( $self->_children_hash->{$child}{stopping} ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 260 | 311 | $self->call( stopped_per_request => $child, @args ); | |||||||||||||||||||
| 261 | } elsif ( my $restarting = delete $self->_stopping_for_restart->{$child} ) { | ||||||||||||||||||||
| 262 | 274 | $self->call( stopped_for_restart => $child, @args ); | |||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||
| 264 | 78 | $self->call( stopped_unexpectedly => $child, @args ); | |||||||||||||||||||
| 265 | } | ||||||||||||||||||||
| 266 | 23 | }; | |||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | event stopped_per_request => sub { | ||||||||||||||||||||
| 269 | 144 | ▶ | my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | 144 | $self->logger->info("child " . $self->_child_id($child) . " exited as requested"); | |||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | 144 | $self->_unregister_child($child); | |||||||||||||||||||
| 274 | 144 | $kernel->refcount_decrement( $self->get_session_id(), "children" ); | |||||||||||||||||||
| 275 | 23 | }; | |||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | event stopped_unexpectedly => sub { | ||||||||||||||||||||
| 278 | 61 | ▶ | my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
stopped_unexpectedly: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | 61 | my $id = $self->_child_id($child); | |||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | 61 | $self->logger->notice("child $id exited on its own"); | |||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | 61 | ▶ | if ( $self->should_restart_child($child, @args) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 285 | 47 | ▶ | if ( $self->child_exit_is_fatal($child, @args) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | 0 | $self->logger->error("child $id exit is fatal, raising error"); | |||||||||||||||||||
| 287 | 0 | $self->yield( fatal_exit => $child, @args ); | |||||||||||||||||||
| 288 | } else { | ||||||||||||||||||||
| 289 | 47 | my $policy = $self->restart_policy; | |||||||||||||||||||
| 290 | 47 | $self->logger->info("child $id will be restarted, restart policy is $policy"); | |||||||||||||||||||
| 291 | 47 | $self->yield( "restart_$policy" => $child, @args ); | |||||||||||||||||||
| 292 | } | ||||||||||||||||||||
| 293 | } else { | ||||||||||||||||||||
| 294 | 14 | $self->logger->info("child $id won't be restarted"); | |||||||||||||||||||
| 295 | 14 | $self->_unregister_child($child); | |||||||||||||||||||
| 296 | 14 | $kernel->refcount_decrement( $self->get_session_id(), "children" ); | |||||||||||||||||||
| 297 | } | ||||||||||||||||||||
| 298 | 23 | }; | |||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | event stopped_for_restart => sub { | ||||||||||||||||||||
| 301 | 427 | ▶ | my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | 427 | $self->logger->info("child " . $self->_child_id($child) . " exited for restart as requested"); | |||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | 427 | $self->yield( respawn => $child ); | |||||||||||||||||||
| 306 | 23 | }; | |||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | event restart_one => sub { | ||||||||||||||||||||
| 309 | 533 | ▶ | my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
restart_one: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | 533 | ▶ | if ( my $handle = $self->_child_handle($child) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 312 | 427 | $self->logger->info("stopping child " . $self->_child_id($child) . " for restart"); | |||||||||||||||||||
| 313 | 427 | $self->_stopping_for_restart->{$child} = 1; | |||||||||||||||||||
| 314 | 427 | $handle->stop_for_restart(); | |||||||||||||||||||
| 315 | } else { | ||||||||||||||||||||
| 316 | 106 | $self->logger->debug("child " . $self->_child_id($child) . " is already dead, marking for respawn"); | |||||||||||||||||||
| 317 | 106 | $self->yield( respawn => $child ); | |||||||||||||||||||
| 318 | } | ||||||||||||||||||||
| 319 | 23 | }; | |||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | event restart_all => sub { | ||||||||||||||||||||
| 322 | 33 | ▶ | my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 323 | |||||||||||||||||||||
| 324 | 33 | foreach my $child ( reverse $self->children ) { | |||||||||||||||||||
| 325 | 330 | $self->yield( restart_one => $child ); | |||||||||||||||||||
| 326 | } | ||||||||||||||||||||
| 327 | 23 | }; | |||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | event restart_rest => sub { | ||||||||||||||||||||
| 330 | 26 | ▶ | my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_]; | ||||||||||||||||||
|
Subroutine
restart_rest: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 331 | |||||||||||||||||||||
| 332 | 26 | my @children = $self->children; | |||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | 26 | ▶ | shift @children while $children[0] != $child; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 335 | |||||||||||||||||||||
| 336 | 26 | foreach my $child ( reverse @children ) { | |||||||||||||||||||
| 337 | 156 | $self->yield( restart_one => $child ); | |||||||||||||||||||
| 338 | } | ||||||||||||||||||||
| 339 | 23 | }; | |||||||||||||||||||
| 340 | |||||||||||||||||||||
| 341 | event fatal_exit => sub { | ||||||||||||||||||||
| 342 | # stop everything | ||||||||||||||||||||
| 343 | # exit with error | ||||||||||||||||||||
| 344 | # FIXME how do we exit abstractly? yield to some callback? | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | 0 | ▶ | die "supervisor seppuku"; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 347 | 23 | }; | |||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | sub child_exit_is_fatal { | ||||||||||||||||||||
| 350 | 106 | ▶ | my ( $self, $child, @args ) = @_; | ||||||||||||||||||
|
Subroutine
child_exit_is_fatal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | # check if the child exceeded the maximal number of restarts by looking at | ||||||||||||||||||||
| 353 | # $self->_children_hash->{$child}{token_bucket}, the child descriptor's | ||||||||||||||||||||
| 354 | # restart policy (if its transient or not, etc), $args{exit_code} being an | ||||||||||||||||||||
| 355 | # error, etc | ||||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | 106 | return 0; | |||||||||||||||||||
| 358 | } | ||||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | sub should_restart_child { | ||||||||||||||||||||
| 361 | 122 | ▶ | my ( $self, $child, @args ) = @_; | ||||||||||||||||||
|
Subroutine
should_restart_child: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | 122 | $child->should_restart(@args); | |||||||||||||||||||
| 364 | } | ||||||||||||||||||||
| 365 | |||||||||||||||||||||
| 366 | event _child => sub { | ||||||||||||||||||||
| 367 | 0 | ▶ | my ( $self, $verb, $child ) = @_[OBJECT, ARG0, ARG1]; | ||||||||||||||||||
|
Subroutine
_child: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | 0 | $self->logger->debug("supervisor $self had child event for $child: $verb"); | |||||||||||||||||||
| 370 | 23 | }; | |||||||||||||||||||
| 371 | |||||||||||||||||||||
| 372 | 23 | __PACKAGE__ | |||||||||||||||||||
| 373 | |||||||||||||||||||||
| 374 | __END__ | ||||||||||||||||||||
| 375 | |||||||||||||||||||||
| 376 | =pod | ||||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | =head1 NAME | ||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | POE::Component::Supervisor - Erlang inspired babysitting | ||||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | =head1 SYNOPSIS | ||||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | use POE; | ||||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | use POE::Component::Supervisor; | ||||||||||||||||||||
| 387 | |||||||||||||||||||||
| 388 | POE::Component::Supervisor->new( | ||||||||||||||||||||
| 389 | children => [ | ||||||||||||||||||||
| 390 | POE::Component::Supervisor::Supervised::Proc->new( ... ), # monitor UNIX procs | ||||||||||||||||||||
| 391 | POE::Component::Supervisor::Supervised::Session->new( ... ), # monitor POE sessions | ||||||||||||||||||||
| 392 | ], | ||||||||||||||||||||
| 393 | ); | ||||||||||||||||||||
| 394 | |||||||||||||||||||||
| 395 | =head1 DESCRIPTION | ||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | This is a port of the Erlang process supervisor | ||||||||||||||||||||
| 398 | (L<http://www.erlang.org/doc/design_principles/sup_princ.html>). | ||||||||||||||||||||
| 399 | |||||||||||||||||||||
| 400 | This will monitor instances of children, restarting them as necessary should | ||||||||||||||||||||
| 401 | they exit. | ||||||||||||||||||||
| 402 | |||||||||||||||||||||
| 403 | Restart throttling is not yet implemented but planned for a future version. | ||||||||||||||||||||
| 404 | |||||||||||||||||||||
| 405 | =head1 OBJECT HIERARCHY | ||||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | A supervisor has any number of supervised child descriptors, which in turn | ||||||||||||||||||||
| 408 | instantiate handles for each spawned instance of the child. | ||||||||||||||||||||
| 409 | |||||||||||||||||||||
| 410 | Supervised children are essential object factories for handles. They spawn new | ||||||||||||||||||||
| 411 | instances of the child they describe by instantiating handles. | ||||||||||||||||||||
| 412 | |||||||||||||||||||||
| 413 | A handle will do the actual management of the child, sending events to the | ||||||||||||||||||||
| 414 | supervisor when the child is terminated, and also facilitate explicit | ||||||||||||||||||||
| 415 | termination of the child's instance. | ||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | Based on its C<restart_policy> the supervisor may order other handles to also | ||||||||||||||||||||
| 418 | stop, and ask various child descriptors to respawn certain children. | ||||||||||||||||||||
| 419 | |||||||||||||||||||||
| 420 | =head1 POE REFERENCE COUNTING | ||||||||||||||||||||
| 421 | |||||||||||||||||||||
| 422 | When no more children are being supervised the L<POE> reference count for the | ||||||||||||||||||||
| 423 | supervisor's session will go down to zero. If no C<alias> is set up then the | ||||||||||||||||||||
| 424 | session will close. If an C<alias> is set and no other sessions are doing | ||||||||||||||||||||
| 425 | anything the session will also close. See L<POE>, and L<MooseX::POE::Aliased>. | ||||||||||||||||||||
| 426 | |||||||||||||||||||||
| 427 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 428 | |||||||||||||||||||||
| 429 | =over 4 | ||||||||||||||||||||
| 430 | |||||||||||||||||||||
| 431 | =item alias | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | See L<MooseX::POE::Aliased>. | ||||||||||||||||||||
| 434 | |||||||||||||||||||||
| 435 | This defaults to C<undef>, unlike the role, so that a supervisor session will | ||||||||||||||||||||
| 436 | close automatically once it has no more children to supervise. | ||||||||||||||||||||
| 437 | |||||||||||||||||||||
| 438 | =item use_logger_singleton | ||||||||||||||||||||
| 439 | |||||||||||||||||||||
| 440 | See L<MooseX::LogDispatch>. | ||||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | Changes the default to true, to allow usage of an already configured | ||||||||||||||||||||
| 443 | L<Log::Dispatch::Config> setup. | ||||||||||||||||||||
| 444 | |||||||||||||||||||||
| 445 | =item restart_policy | ||||||||||||||||||||
| 446 | |||||||||||||||||||||
| 447 | This is one of C<one>, C<all> or C<rest>. | ||||||||||||||||||||
| 448 | |||||||||||||||||||||
| 449 | If the L<POE::Component::Supervisor::Supervised> object describing the child | ||||||||||||||||||||
| 450 | deems the child should be restarted, then the value of this attribute controls | ||||||||||||||||||||
| 451 | which other children to also restart. | ||||||||||||||||||||
| 452 | |||||||||||||||||||||
| 453 | C<one> denotes that only the child which died will be restarted. | ||||||||||||||||||||
| 454 | |||||||||||||||||||||
| 455 | C<rest> will cause all the children appearing after the child which died in the | ||||||||||||||||||||
| 456 | children array to be restarted, but not the children preceding it. | ||||||||||||||||||||
| 457 | |||||||||||||||||||||
| 458 | C<all> will restart all the children. | ||||||||||||||||||||
| 459 | |||||||||||||||||||||
| 460 | =item children | ||||||||||||||||||||
| 461 | |||||||||||||||||||||
| 462 | This is the array of children being supervised. | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | It is a required argument. | ||||||||||||||||||||
| 465 | |||||||||||||||||||||
| 466 | Note that the array reference will be modified if new children are introduced | ||||||||||||||||||||
| 467 | and when children are removed (even during normal shutdown), so pass in a copy | ||||||||||||||||||||
| 468 | of an array if this is a problem for you. | ||||||||||||||||||||
| 469 | |||||||||||||||||||||
| 470 | The order of the children matters, see C<restart_policy>. | ||||||||||||||||||||
| 471 | |||||||||||||||||||||
| 472 | =back | ||||||||||||||||||||
| 473 | |||||||||||||||||||||
| 474 | =head1 METHODS | ||||||||||||||||||||
| 475 | |||||||||||||||||||||
| 476 | =item new %args | ||||||||||||||||||||
| 477 | |||||||||||||||||||||
| 478 | =item start @children | ||||||||||||||||||||
| 479 | |||||||||||||||||||||
| 480 | Spawn and supervise the children described by the descriptors in @children. | ||||||||||||||||||||
| 481 | |||||||||||||||||||||
| 482 | =item stop [ @children ] | ||||||||||||||||||||
| 483 | |||||||||||||||||||||
| 484 | Stop the specified children. | ||||||||||||||||||||
| 485 | |||||||||||||||||||||
| 486 | If no arguments are provided all the children are stopped. | ||||||||||||||||||||
| 487 | |||||||||||||||||||||
| 488 | =item should_restart_child $child, %args | ||||||||||||||||||||
| 489 | |||||||||||||||||||||
| 490 | Delegates to C<$child> by calling | ||||||||||||||||||||
| 491 | L<POE::Component::Supervisor::Supervised/should_restart>. | ||||||||||||||||||||
| 492 | |||||||||||||||||||||
| 493 | =item child_exit_is_fatal | ||||||||||||||||||||
| 494 | |||||||||||||||||||||
| 495 | Currently always returns false. In the future restart throttling will be | ||||||||||||||||||||
| 496 | implemented using this method. | ||||||||||||||||||||
| 497 | |||||||||||||||||||||
| 498 | If C<true> is returned an error will be thrown by the supervisor. | ||||||||||||||||||||
| 499 | |||||||||||||||||||||
| 500 | =head1 EVENTS | ||||||||||||||||||||
| 501 | |||||||||||||||||||||
| 502 | The L<POE> event api is currently internal. All manipulation of the supervisor | ||||||||||||||||||||
| 503 | object should be done using the api described in L<METHODS>. | ||||||||||||||||||||
| 504 | |||||||||||||||||||||
| 505 | =head1 SEE ALSO | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | L<http://www.erlang.org/doc/design_principles/sup_princ.html> | ||||||||||||||||||||
| 508 | |||||||||||||||||||||
| 509 | =head1 AUTHOR | ||||||||||||||||||||
| 510 | |||||||||||||||||||||
| 511 | Stevan Little E<lt>stevan@iinteractive.comE<gt> | ||||||||||||||||||||
| 512 | |||||||||||||||||||||
| 513 | Yuval Kogman E<lt>yuval.kogman@iinteractive.com<gt> | ||||||||||||||||||||
| 514 | |||||||||||||||||||||
| 515 | =head1 COPYRIGHT | ||||||||||||||||||||
| 516 | |||||||||||||||||||||
| 517 | Copyright (c) 2008, 2010 Infinity Interactive, Yuval Kogman. All rights | ||||||||||||||||||||
| 518 | reserved This program is free software; you can redistribute it and/or | ||||||||||||||||||||
| 519 | modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 520 | |||||||||||||||||||||
| 521 | =cut | ||||||||||||||||||||