| 1 | package PAGI::Lifespan; | ||||||||||||||||||||||||||||||||
| 2 | 35 | $PAGI::Lifespan::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||
| 3 | 35 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 35 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 35 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 35 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 35 | ▶ | use PAGI::Utils (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 11 | 15 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 15 | my $app = delete $args{app}; | |||||||||||||||||||||||||||||||
| 14 | 15 | ▶ | $app = PAGI::Utils::to_app($app) if defined $app; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 15 | my @handlers; | |||||||||||||||||||||||||||||||
| 17 | push @handlers, { | ||||||||||||||||||||||||||||||||
| 18 | startup => $args{startup}, | ||||||||||||||||||||||||||||||||
| 19 | shutdown => $args{shutdown}, | ||||||||||||||||||||||||||||||||
| 20 | 15 | ▶ | } if $args{startup} || $args{shutdown}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"startup"} or $args{"shutdown"}
MC/DC100%2/2
$args{"startup"} or $args{"shutdown"}
$args{"startup"}
$args{"shutdown"}
Truth table100%3/3
A or B
A= $args{"startup"}B= $args{"shutdown"}
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 15 | return bless { | |||||||||||||||||||||||||||||||
| 23 | app => $app, | ||||||||||||||||||||||||||||||||
| 24 | _handlers => \@handlers, | ||||||||||||||||||||||||||||||||
| 25 | _state => undef, | ||||||||||||||||||||||||||||||||
| 26 | }, $class; | ||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub state { shift->{_state} } | ||||||||||||||||||||||||||||||
|
Subroutine
state: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | sub on_startup { | ||||||||||||||||||||||||||||||
|
Subroutine
on_startup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 32 | 3 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||
| 33 | 3 | return $self->register(startup => $cb); | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | sub on_shutdown { | ||||||||||||||||||||||||||||||
|
Subroutine
on_shutdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 37 | 3 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||
| 38 | 3 | return $self->register(shutdown => $cb); | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 10 | ▶ | sub register { | ||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 42 | 10 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||
| 43 | 10 | ▶ | return $self unless $args{startup} || $args{shutdown}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"startup"} or $args{"shutdown"}
MC/DC0%0/2
$args{"startup"} or $args{"shutdown"}
$args{"startup"}
$args{"shutdown"}
Truth table66%2/3
A or B
A= $args{"startup"}B= $args{"shutdown"}
| |||||||||||||||||||||||||||||||||
| 44 | 10 | push @{$self->{_handlers}}, { | |||||||||||||||||||||||||||||||
| 45 | startup => $args{startup}, | ||||||||||||||||||||||||||||||||
| 46 | shutdown => $args{shutdown}, | ||||||||||||||||||||||||||||||||
| 47 | 10 | }; | |||||||||||||||||||||||||||||||
| 48 | 10 | return $self; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | sub for_scope { | ||||||||||||||||||||||||||||||
|
Subroutine
for_scope: called CC=4 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 52 | 5 | my ($class, $scope) = @_; | |||||||||||||||||||||||||||||||
| 53 | 5 | ▶ | croak "scope is required" unless $scope && ref($scope) eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$scope and ref $scope eq "HASH"
MC/DC0%0/2
$scope and ref $scope eq "HASH"
$scope
ref $scope eq "HASH"
Truth table33%1/3
A and B
A= $scopeB= ref $scope eq "HASH"
| |||||||||||||||||||||||||||||||||
| 54 | 5 | ▶ | return $scope->{'pagi.lifespan.manager'} //= $class->new; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$scope->{"pagi.lifespan.manager"} //= $class->new
MC/DC0%0/2
$scope->{"pagi.lifespan.manager"} //= $class->new
$scope->{"pagi.lifespan.manager"}
$class->new
Truth table33%1/3
A //= B
A= $scope->{"pagi.lifespan.manager"}B= $class->new
| |||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 58 | 3 | my ($class, $app, %args) = @_; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 3 | my $self = $class->new(app => $app, %args); | |||||||||||||||||||||||||||||||
| 61 | 3 | return $self->to_app; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 8 | ▶ | sub to_app { | ||||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 65 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 8 | my $app = $self->{app}; | |||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | croak "PAGI::Lifespan->to_app requires an app" unless $app; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | my $wrapper = async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||
| 71 | 13 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 13 | ▶ | my $type = $scope->{type} // ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"type"} // ""
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 13 | ▶ | if ($type eq 'lifespan') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 6 | ▶ | $scope->{'pagi.lifespan.manager'} //= $self; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$scope->{"pagi.lifespan.manager"} //= $self
MC/DC0%0/2
$scope->{"pagi.lifespan.manager"} //= $self
$scope->{"pagi.lifespan.manager"}
$self
Truth table33%1/3
A //= B
A= $scope->{"pagi.lifespan.manager"}B= $self
| |||||||||||||||||||||||||||||||||
| 77 | 6 | ▶ | $scope->{state} //= {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"state"} //= {}
MC/DC100%1/1
$scope->{"state"} //= {}
$scope->{"state"}
Truth table100%2/2
A
A= $scope->{"state"}
| |||||||||||||||||||||||||||||||||
| 78 | 6 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 79 | 6 | return await $self->handle($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 7 | my $inner_scope = { %$scope }; | |||||||||||||||||||||||||||||||
| 83 | 7 | ▶ | $inner_scope->{state} //= ($self->{_state} // {}); | ||||||||||||||||||||||||||||||
|
Condition100%4/4
$inner_scope->{"state"} //= $self->{"_state"} // {}
$self->{"_state"} // {}
MC/DC100%1/1
$inner_scope->{"state"} //= $self->{"_state"} // {}
$inner_scope->{"state"}
Truth table100%2/2
A
A= $inner_scope->{"state"}
| |||||||||||||||||||||||||||||||||
| 84 | 7 | $self->{_state} = $inner_scope->{state}; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 7 | await $app->($inner_scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 87 | 8 | }; | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 8 | return $wrapper; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 13 | ▶ | async sub handle { | ||||||||||||||||||||||||||||||
|
Subroutine
handle: called CC=18 SCAR=29.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 93 | 13 | my ($self, $scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 94 | 13 | ▶ | return 0 unless $scope && ($scope->{type} // '') eq 'lifespan'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$scope and ($scope->{"type"} // "") eq "lifespan"
$scope->{"type"} // ""
MC/DC0%0/2
$scope and ($scope->{"type"} // "") eq "lifespan"
$scope
$scope->{"type"}
Truth table33%1/3
A and B
A= $scopeB= $scope->{"type"}
| |||||||||||||||||||||||||||||||||
| 95 | 13 | ▶ | return 0 if $scope->{'pagi.lifespan.handled'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 13 | $scope->{'pagi.lifespan.handled'} = 1; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 13 | my @handlers; | |||||||||||||||||||||||||||||||
| 99 | 13 | ▶ | if (my $extra = $scope->{'pagi.lifespan.handlers'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 3 | push @handlers, @$extra; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | 13 | ▶ | push @handlers, @{$self->{_handlers} // []}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_handlers"} // []
MC/DC0%0/1
$self->{"_handlers"} // []
$self->{"_handlers"}
Truth table50%1/2
A
A= $self->{"_handlers"}
| |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 13 | ▶ | my $state = $scope->{state} //= {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"state"} //= {}
MC/DC100%1/1
$scope->{"state"} //= {}
$scope->{"state"}
Truth table100%2/2
A
A= $scope->{"state"}
| |||||||||||||||||||||||||||||||||
| 105 | 13 | $self->{_state} = $state; | |||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 13 | while (1) { | |||||||||||||||||||||||||||||||
| 108 | 23 | my $msg = await $receive->(); | |||||||||||||||||||||||||||||||
| 109 | 23 | ▶ | my $type = $msg->{type} // ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$msg->{"type"} // ""
MC/DC0%0/1
$msg->{"type"} // ""
$msg->{"type"}
Truth table50%1/2
A
A= $msg->{"type"}
| |||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 23 | ▶ | if ($type eq 'lifespan.startup') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 112 | 13 | for my $handler (@handlers) { | |||||||||||||||||||||||||||||||
| 113 | 21 | ▶ | next unless $handler->{startup}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 16 | eval { await $handler->{startup}->($state) }; | |||||||||||||||||||||||||||||||
| 115 | 16 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 3 | await $send->({ | |||||||||||||||||||||||||||||||
| 117 | type => 'lifespan.startup.failed', | ||||||||||||||||||||||||||||||||
| 118 | message => "$@", | ||||||||||||||||||||||||||||||||
| 119 | }); | ||||||||||||||||||||||||||||||||
| 120 | 3 | return; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | 10 | await $send->({ type => 'lifespan.startup.complete' }); | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | elsif ($type eq 'lifespan.shutdown') { | ||||||||||||||||||||||||||||||||
| 126 | # Run every shutdown handler (best-effort cleanup: one failing | ||||||||||||||||||||||||||||||||
| 127 | # handler must not prevent the others from releasing resources), | ||||||||||||||||||||||||||||||||
| 128 | # collecting any errors so they can be reported rather than swallowed. | ||||||||||||||||||||||||||||||||
| 129 | 10 | my @errors; | |||||||||||||||||||||||||||||||
| 130 | 10 | for my $handler (reverse @handlers) { | |||||||||||||||||||||||||||||||
| 131 | 18 | ▶ | next unless $handler->{shutdown}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 14 | eval { await $handler->{shutdown}->($state) }; | |||||||||||||||||||||||||||||||
| 133 | 14 | ▶ | push @errors, "$@" if $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | 10 | ▶ | if (@errors) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 2 | await $send->({ | |||||||||||||||||||||||||||||||
| 137 | type => 'lifespan.shutdown.failed', | ||||||||||||||||||||||||||||||||
| 138 | message => join("\n", @errors), | ||||||||||||||||||||||||||||||||
| 139 | }); | ||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||||||
| 142 | 8 | await $send->({ type => 'lifespan.shutdown.complete' }); | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | 10 | return 1; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | 35 | } | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 35 | 1; | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||||||