blib/lib/PAGI/Lifespan.pm

filter: Statementstmt 98.9% 90 / 91 Branchbran 81.2% 26 / 32 Conditioncond 60.0% 21 / 35 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 93.3% 14 / 15 Podpod 44.4% 4 / 9 totaltotal 79.1% 159 / 201 CC 33 SCAR 22.5
CC
33
Cov
87%
handle
18.6
__ANON__
7.2
for_scope
5.5
CRAP 9.5
« blib/lib/PAGI/Request/MultiPartHandler.pm ↑ Summary blib/lib/PAGI/App/Cascade.pm »
1 package PAGI::Lifespan;
2 35 $PAGI::Lifespan::VERSION = '0.002001';
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 sub new {
11 14 ▶ my ($class, %args) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
12  
13 14 my $app = delete $args{app};
14 14 ▶ $app = PAGI::Utils::to_app($app) if defined $app;
Branch: 1 branch
BranchTrueFalseTotal
if defined $app 8 6 14
15  
16 14 my @handlers;
17 push @handlers, {
18 startup => $args{startup},
19 shutdown => $args{shutdown},
20 14 ▶ } if $args{startup} || $args{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"startup"} or $args{"shutdown"} 7 7 14
Condition100%3/3
$args{"startup"} or $args{"shutdown"}
l!l&&r!l&&!r
527
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"}
ABresult
1X1
011
000
21  
22 14 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 sub on_startup {
32 2 ▶ my ($self, $cb) = @_;
Subroutine on_startup: called CC=1 SCAR=0.0
Pod undocumented
33 2 return $self->register(startup => $cb);
34 }
35  
36 sub on_shutdown {
37 3 ▶ my ($self, $cb) = @_;
Subroutine on_shutdown: called CC=1 SCAR=0.0
Pod undocumented
38 3 return $self->register(shutdown => $cb);
39 }
40  
41 sub register {
42 9 ▶ my ($self, %args) = @_;
Subroutine register: called CC=3 SCAR=11.2
Pod undocumented
43 9 ▶ return $self unless $args{startup} || $args{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"startup"} or $args{"shutdown"} 0 9 9
Condition66%2/3
$args{"startup"} or $args{"shutdown"}
l!l&&r!l&&!r
630
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"}
ABresult
1X1
011
000
44 9 push @{$self->{_handlers}}, {
45 startup => $args{startup},
46 shutdown => $args{shutdown},
47 9 };
48 9 return $self;
49 }
50  
51 sub for_scope {
52 5 ▶ my ($class, $scope) = @_;
Subroutine for_scope: called CC=4 SCAR=17.1
Pod undocumented
53 5 ▶ croak "scope is required" unless $scope && ref($scope) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $scope and ref $scope eq "HASH" 0 5 5
Condition33%1/3
$scope and ref $scope eq "HASH"
!ll&&!rl&&r
005
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"
ABresult
0X0
100
111
54 5 ▶ return $scope->{'pagi.lifespan.manager'} //= $class->new;
Condition33%1/3
$scope->{"pagi.lifespan.manager"} //= $class->new
l!l&&r!l&&!r
050
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
ABresult
1X1
011
000
55 }
56  
57 sub wrap {
58 3 ▶ my ($class, $app, %args) = @_;
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
59  
60 3 my $self = $class->new(app => $app, %args);
61 3 return $self->to_app;
62 }
63  
64 sub to_app {
65 8 ▶ my ($self) = @_;
Subroutine to_app: called CC=2 SCAR=7.0
Pod documented
66  
67 8 my $app = $self->{app};
68 8 ▶ croak "PAGI::Lifespan->to_app requires an app" unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 0 8 8
69  
70 13 ▶ my $wrapper = async sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
71 13 my ($scope, $receive, $send) = @_;
72  
73 13 ▶ my $type = $scope->{type} // '';
Condition50%1/2
$scope->{"type"} // ""
l!l
130
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
74  
75 13 ▶ if ($type eq 'lifespan') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "lifespan") 6 7 13
76 6 ▶ $scope->{'pagi.lifespan.manager'} //= $self;
Condition33%1/3
$scope->{"pagi.lifespan.manager"} //= $self
l!l&&r!l&&!r
060
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
ABresult
1X1
011
000
77 6 ▶ $scope->{state} //= {};
Condition100%2/2
$scope->{"state"} //= {}
l!l
15
MC/DC100%1/1
$scope->{"state"} //= {}
$scope->{"state"}
Truth table100%2/2
A
A= $scope->{"state"}
Aresult
11
00
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} // {});
Condition80%4/5
$inner_scope->{"state"} //= $self->{"_state"} // {}
l!l&&r!l&&!r
520
$self->{"_state"} // {}
l!l
11
MC/DC0%0/2
$inner_scope->{"state"} //= $self->{"_state"} // {}
$inner_scope->{"state"} $self->{"_state"}
Truth table66%2/3
A //= B
A= $inner_scope->{"state"}B= $self->{"_state"}
ABresult
1X1
011
000
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 12 ▶async sub handle {
Subroutine handle: called CC=18 SCAR=29.2
Pod undocumented
93 12 my ($self, $scope, $receive, $send) = @_;
94 12 ▶ return 0 unless $scope && ($scope->{type} // '') eq 'lifespan';
Branch: 1 branch
BranchTrueFalseTotal
unless $scope and ($scope->{"type"} // "") eq "lifespan" 0 12 12
Condition40%2/5
$scope and ($scope->{"type"} // "") eq "lifespan"
!ll&&!rl&&r
0012
$scope->{"type"} // ""
l!l
120
MC/DC0%0/2
$scope and ($scope->{"type"} // "") eq "lifespan"
$scope $scope->{"type"}
Truth table33%1/3
A and B
A= $scopeB= $scope->{"type"}
ABresult
0X0
100
111
95 12 ▶ return 0 if $scope->{'pagi.lifespan.handled'};
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"pagi.lifespan.handled"} 0 12 12
96 12 $scope->{'pagi.lifespan.handled'} = 1;
97  
98 12 my @handlers;
99 12 ▶ if (my $extra = $scope->{'pagi.lifespan.handlers'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra = $scope->{"pagi.lifespan.handlers"}) 3 9 12
100 3 push @handlers, @$extra;
101 }
102 12 ▶ push @handlers, @{$self->{_handlers} // []};
Condition50%1/2
$self->{"_handlers"} // []
l!l
120
MC/DC0%0/1
$self->{"_handlers"} // []
$self->{"_handlers"}
Truth table50%1/2
A
A= $self->{"_handlers"}
Aresult
11
00
103  
104 12 ▶ my $state = $scope->{state} //= {};
Condition100%2/2
$scope->{"state"} //= {}
l!l
111
MC/DC100%1/1
$scope->{"state"} //= {}
$scope->{"state"}
Truth table100%2/2
A
A= $scope->{"state"}
Aresult
11
00
105 12 $self->{_state} = $state;
106  
107 12 while (1) {
108 22 my $msg = await $receive->();
109 22 ▶ my $type = $msg->{type} // '';
Condition50%1/2
$msg->{"type"} // ""
l!l
220
MC/DC0%0/1
$msg->{"type"} // ""
$msg->{"type"}
Truth table50%1/2
A
A= $msg->{"type"}
Aresult
11
00
110  
111 22 ▶ if ($type eq 'lifespan.startup') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'lifespan.startup') { } 12 10 22
elsif ($type eq 'lifespan.shutdown') { } 10 0 10
112 12 for my $handler (@handlers) {
113 20 ▶ next unless $handler->{startup};
Branch: 1 branch
BranchTrueFalseTotal
unless $handler->{"startup"} 5 15 20
114 15 eval { await $handler->{startup}->($state) };
115 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 13 15
116 2 await $send->({
117 type => 'lifespan.startup.failed',
118 message => "$@",
119 });
120 2 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
BranchTrueFalseTotal
unless $handler->{"shutdown"} 4 14 18
132 14 eval { await $handler->{shutdown}->($state) };
133 14 ▶ push @errors, "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 12 14
134 }
135 10 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) { } 2 8 10
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__
« blib/lib/PAGI/Request/MultiPartHandler.pm ↑ Summary blib/lib/PAGI/App/Cascade.pm »