blib/lib/PAGI/Lifespan.pm

filter: Statementstmt 98.9% 90 / 91 Branchbran 81.2% 26 / 32 Conditioncond 61.7% 21 / 34 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 93.3% 14 / 15 Podpod 44.4% 4 / 9 totaltotal 80.4% 160 / 199 CC 33 SCAR 22.5
CC
33
Cov
87%
handle
18.6
__ANON__
7.1
for_scope
5.5
CRAP 9.5
« blib/lib/PAGI/Request/MultiPartHandler.pm ↑ Summary lib/PAGI/Middleware/AccessLog.pm »
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
BranchTrueFalseTotal
if defined $app 8 7 15
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
BranchTrueFalseTotal
if $args{"startup"} or $args{"shutdown"} 7 8 15
Condition100%3/3
$args{"startup"} or $args{"shutdown"}
l!l&&r!l&&!r
528
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 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
BranchTrueFalseTotal
unless $args{"startup"} or $args{"shutdown"} 0 10 10
Condition66%2/3
$args{"startup"} or $args{"shutdown"}
l!l&&r!l&&!r
730
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 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
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 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
BranchTrueFalseTotal
unless $app 0 8 8
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"} // ""
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} // {});
Condition100%4/4
$inner_scope->{"state"} //= $self->{"_state"} // {}
l!l
52
$self->{"_state"} // {}
l!l
11
MC/DC100%1/1
$inner_scope->{"state"} //= $self->{"_state"} // {}
$inner_scope->{"state"}
Truth table100%2/2
A
A= $inner_scope->{"state"}
Aresult
11
00
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
BranchTrueFalseTotal
unless $scope and ($scope->{"type"} // "") eq "lifespan" 0 13 13
Condition40%2/5
$scope and ($scope->{"type"} // "") eq "lifespan"
!ll&&!rl&&r
0013
$scope->{"type"} // ""
l!l
130
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 13 ▶ return 0 if $scope->{'pagi.lifespan.handled'};
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"pagi.lifespan.handled"} 0 13 13
96 13 $scope->{'pagi.lifespan.handled'} = 1;
97  
98 13 my @handlers;
99 13 ▶ if (my $extra = $scope->{'pagi.lifespan.handlers'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra = $scope->{"pagi.lifespan.handlers"}) 3 10 13
100 3 push @handlers, @$extra;
101 }
102 13 ▶ push @handlers, @{$self->{_handlers} // []};
Condition50%1/2
$self->{"_handlers"} // []
l!l
130
MC/DC0%0/1
$self->{"_handlers"} // []
$self->{"_handlers"}
Truth table50%1/2
A
A= $self->{"_handlers"}
Aresult
11
00
103  
104 13 ▶ my $state = $scope->{state} //= {};
Condition100%2/2
$scope->{"state"} //= {}
l!l
121
MC/DC100%1/1
$scope->{"state"} //= {}
$scope->{"state"}
Truth table100%2/2
A
A= $scope->{"state"}
Aresult
11
00
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"} // ""
l!l
230
MC/DC0%0/1
$msg->{"type"} // ""
$msg->{"type"}
Truth table50%1/2
A
A= $msg->{"type"}
Aresult
11
00
110  
111 23 ▶ if ($type eq 'lifespan.startup') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'lifespan.startup') { } 13 10 23
elsif ($type eq 'lifespan.shutdown') { } 10 0 10
112 13 for my $handler (@handlers) {
113 21 ▶ next unless $handler->{startup};
Branch: 1 branch
BranchTrueFalseTotal
unless $handler->{"startup"} 5 16 21
114 16 eval { await $handler->{startup}->($state) };
115 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 13 16
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
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 lib/PAGI/Middleware/AccessLog.pm »