Acme-Parataxis

 view release on metacpan or  search on metacpan

lib/Acme/Parataxis/Supervisor.pod  view on Meta::CPAN

=pod

=encoding utf8

=head1 NAME

Acme::Parataxis::Supervisor - OTP-Style Restart Supervision for Actors and Nested Subtrees

=head1 SYNOPSIS

    use v5.40;
    use Acme::Parataxis qw[async fiber await_sleep];
    use Acme::Parataxis::Actor;
    use Acme::Parataxis::Supervisor;

    async {
        my $sup = Acme::Parataxis::Supervisor->new(
            strategy     => 'OneForOne',    # OneForOne | OneForAll | RestForOne
            max_restarts => 3,
            within       => 30,             # seconds
        );

        # Supervise an actor factory:
        $sup->supervise(
            sub {
                Acme::Parataxis::Actor->spawn(
                    sub ( $self, $msg ) {
                        die "Crash requested\n" if $msg eq 'crash';
                        return "Handled: $msg";
                    },
                    16,
                    supervised => 1         # supervised => 1 is required for supervision!
                );
            },
            name => 'worker'
        );

        fiber {
            await_sleep(20);
            say $sup->child('worker')->ask('hello')->await;    # Handled: hello

            # Provoke a crash; the supervisor transparently replaces the worker:
            $sup->child('worker')->send('crash');
            await_sleep(20) until $sup->restarts('worker') > 0;

            say "Restarts observed: " . $sup->restarts('worker');
            say $sup->child('worker')->ask('ping')->await;     # Handled: ping

            $sup->stop;
        };

        $sup->run;    # supervises workers until stop() or restart budget exhaustion
    };

=head1 DESCRIPTION

While structured concurrency (via L<Acme::Parataxis::Nursery>) provides fail-fast semantics by tearing down a group of
fibers on the first error, an L<Acme::Parataxis::Supervisor> implements the Erlang/OTP I<heal-fast> pattern. It runs a
supervision fiber that monitors long-lived child workers and automatically restarts them according to a predefined
restart strategy when they fail.

Supervised children can be L<Acme::Parataxis::Actor>s or nested C<Acme::Parataxis::Supervisor> instances.



( run in 1.892 second using v1.01-cache-2.11-cpan-234fa220139 )