blib/lib/Langertha/Skeid/CustomerSource/DBI.pm

filter: Statementstmt 28.9% 53 / 183 Branchbran 6.9% 5 / 72 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 44.8% 13 / 29 Podpod 83.3% 5 / 6 totaltotal 21.1% 76 / 360 CC 56 SCAR 33.4
CC
56
Cov
19%
__ANON__
90.0
__ANON__
56.0
dsn
42.0
CRAP 28.4
« lib/Langertha/Skeid/OpenAPI.pm ↑ Summary blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm »
1 package Langertha::Skeid::CustomerSource::DBI;
2 4 our $VERSION = '0.004';
3 # ABSTRACT: Customer source fetcher on DBI - connect and query in a child process, LISTEN in another
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Langertha::Skeid::CustomerSource;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 4 has config => (is => 'ro', required => 1);
15  
16 4 has _fetch_sub => (is => 'rw');
17 4 has _fetch_timer => (is => 'rw');
18 4 has _listener => (is => 'rw');
19 4 has _listen_timer => (is => 'rw');
20 4 has _notify => (is => 'rw');
21 1 ▶has _pid => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 # What a child may overrun its connect timeout by before the parent kills it.
24 4 our $KILL_MARGIN_S = 2;
25  
26  
27 1 ▶sub statement {
Subroutine statement: called CC=1 SCAR=0.0
Pod documented
28 1 my ($self) = @_;
29 1 return Langertha::Skeid::CustomerSource->statement($self->config->{relation});
30 }
31  
32  
33 0 ▶sub dsn {
Subroutine dsn: not called CC=6 SCAR=37.4
Pod documented
34 0 my ($self) = @_;
35 0 my $dsn = $self->config->{dsn};
36 0 ▶ return $dsn unless $dsn =~ /\Adbi:Pg:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn =~ /\Adbi:Pg:/i 0 0 0
37 0 ▶ return $dsn if $dsn =~ /\bconnect_timeout=/i;
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ /\bconnect_timeout=/i 0 0 0
38 0 ▶ my $seconds = ceil($self->config->{connect_timeout_s} // 5);
Condition0%0/2
$self->config->{"connect_timeout_s"} // 5
l!l
00
MC/DC0%0/1
$self->config->{"connect_timeout_s"} // 5
$self->config->{"connect_timeout_s"}
Truth table0%0/2
A
A= $self->config->{"connect_timeout_s"}
Aresult
11
00
39 0 ▶ $seconds = 2 if $seconds < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds < 2 0 0 0
40 0 ▶ return $dsn . ($dsn =~ /[:;]\z/ ? '' : ';') . "connect_timeout=$seconds";
Branch: 1 branch
BranchTrueFalseTotal
$dsn =~ /[:;]\z/ ? : 0 0 0
41 }
42  
43  
44 0 ▶sub fetch {
Subroutine fetch: not called CC=4 SCAR=30.0
Pod documented
45 0 my ($self, $cb) = @_;
46 0 $self->_drop_foreign;
47 0 $self->_kill_fetch;
48 0 ▶ $self->_listen unless $self->_listener;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listener 0 0 0
49  
50 0 my $sql = $self->statement;
51 0 my $cfg = $self->config;
52 0 my $dsn = $self->dsn;
53 0 ▶ my $ms = int(1000 * ($cfg->{connect_timeout_s} // 5));
Condition0%0/2
$cfg->{"connect_timeout_s"} // 5
l!l
00
MC/DC0%0/1
$cfg->{"connect_timeout_s"} // 5
$cfg->{"connect_timeout_s"}
Truth table0%0/2
A
A= $cfg->{"connect_timeout_s"}
Aresult
11
00
54 0 my %conn = (dsn => $dsn, user => $cfg->{user}, password => $cfg->{password});
55  
56 0 my $sub = Mojo::IOLoop->subprocess;
57 0 $self->_fetch_sub($sub);
58 0 my $done = 0;
59 0 weaken(my $weak = $self);
60  
61 0 ▶ my $limit = ($cfg->{connect_timeout_s} // 5) + $KILL_MARGIN_S;
Condition0%0/2
$cfg->{"connect_timeout_s"} // 5
l!l
00
MC/DC0%0/1
$cfg->{"connect_timeout_s"} // 5
$cfg->{"connect_timeout_s"}
Truth table0%0/2
A
A= $cfg->{"connect_timeout_s"}
Aresult
11
00
62 0 ▶ $self->_fetch_timer(Mojo::IOLoop->timer($limit => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
63 0 ▶ return unless $weak && !$done;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and not $done 0 0 0
Condition0%0/3
$weak and not $done
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and not $done
$weak not $done
Truth table0%0/3
A and not(B)
A= $weakB= not $done
ABresult
0X0
100
111
64 0 $done = 1;
65 0 $weak->_fetch_timer(undef);
66 0 $weak->_kill($sub);
67 0 $weak->_fetch_sub(undef);
68 0 $cb->('fetch child overran its deadline and was killed');
69 0 }));
70  
71 $sub->run(
72 0 ▶ sub { return _query(\%conn, $sql, $ms) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
74 0 my ($sp, $err, $rows) = @_;
75 0 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
76 0 $done = 1;
77 0 ▶ if ($weak) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak) 0 0 0
78 0 my $t = $weak->_fetch_timer;
79 0 ▶ Mojo::IOLoop->remove($t) if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 0 0 0
80 0 $weak->_fetch_timer(undef);
81 0 $weak->_fetch_sub(undef);
82 }
83 # Mojo passes '' for no error.
84 0 ▶ return $cb->(_scrub($err, $conn{password})) if defined($err) && length("$err");
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and length "$err" 0 0 0
Condition0%0/3
defined $err and length "$err"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $err and length "$err"
defined $err length "$err"
Truth table0%0/3
A and B
A= defined $errB= length "$err"
ABresult
0X0
100
111
85 0 ▶ return $cb->('the fetch returned no rows list') unless ref($rows) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows eq "ARRAY" 0 0 0
86 0 return $cb->(undef, $rows);
87 },
88 0 );
89 0 return;
90 }
91  
92  
93 0 ▶sub on_notify {
Subroutine on_notify: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($self, $cb) = @_;
95 0 $self->_notify($cb);
96 0 return;
97 }
98  
99  
100 1 ▶sub release {
Subroutine release: called CC=3 SCAR=11.8
Pod documented
101 1 my ($self) = @_;
102 1 $self->_kill_fetch;
103 1 my $timer = $self->_listen_timer;
104 1 ▶ Mojo::IOLoop->remove($timer) if defined $timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timer 0 1 1
105 1 $self->_listen_timer(undef);
106 1 ▶ if (my $l = $self->_listener) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $self->_listener) 0 1 1
107 0 $self->_listener(undef); # first: its finish callback sees it was released on purpose
108 0 $self->_kill($l);
109 }
110 1 return;
111 }
112  
113 1 ▶sub _kill_fetch {
Subroutine _kill_fetch: called CC=3 SCAR=12.0
114 1 my ($self) = @_;
115 1 my $timer = $self->_fetch_timer;
116 1 ▶ Mojo::IOLoop->remove($timer) if defined $timer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timer 0 1 1
117 1 $self->_fetch_timer(undef);
118 1 ▶ if (my $sub = $self->_fetch_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->_fetch_sub) 0 1 1
119 0 $self->_fetch_sub(undef);
120 0 $self->_kill($sub);
121 }
122 1 return;
123 }
124  
125 0 ▶sub _kill {
Subroutine _kill: not called CC=3 SCAR=24.8
126 0 my ($self, $sub) = @_;
127 0 my $pid = eval { $sub->pid };
128 0 ▶ kill 'KILL', $pid if $pid && $self->_pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid and $self->_pid == $$ 0 0 0
Condition0%0/3
$pid and $self->_pid == $$
!ll&&!rl&&r
000
MC/DC0%0/2
$pid and $self->_pid == $$
$pid $self->_pid == $$
Truth table0%0/3
A and B
A= $pidB= $self->_pid == $$
ABresult
0X0
100
111
129 0 return;
130 }
131  
132 # Children belong to the process that started them: after a fork the parent's are not ours to
133 # kill or wait on, and the objects are forgotten.
134 0 ▶sub _drop_foreign {
Subroutine _drop_foreign: not called CC=2 SCAR=17.9
135 0 my ($self) = @_;
136 0 ▶ if ($self->_pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pid != $$) 0 0 0
137 0 $self->_fetch_sub(undef);
138 0 $self->_fetch_timer(undef);
139 0 $self->_listener(undef);
140 0 $self->_listen_timer(undef);
141 0 $self->_pid($$);
142 }
143 0 return;
144 }
145  
146 0 ▶sub _scrub {
Subroutine _scrub: not called CC=4 SCAR=30.0
147 0 my ($text, $password) = @_;
148 0 $text = "$text";
149 0 ▶ $text =~ s/\Q$password\E/***/g if defined($password) && length($password);
Branch: 1 branch
BranchTrueFalseTotal
if defined $password and length $password 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
150 0 $text =~ s/\b(password|pwd)=[^;'"\s)]*/$1=***/gi;
151 0 $text =~ s/\s+/ /g;
152 0 $text =~ s/\s+\z//;
153 0 ▶ return length($text) ? $text : 'fetch failed';
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 0 0 0
154 }
155  
156 # --- the child: blocking, never on the loop's process ----------------------------------------
157  
158 0 ▶sub _connect {
Subroutine _connect: not called CC=3 SCAR=24.8
159 0 my ($conn) = @_;
160 0 require DBI;
161 0 ▶ return DBI->connect($conn->{dsn}, $conn->{user} // '', $conn->{password} // '',
Condition0%0/4
$conn->{"user"} // ""
l!l
00
$conn->{"password"} // ""
l!l
00
MC/DC0%0/1
$conn->{"user"} // ""
$conn->{"user"}
MC/DC0%0/1
$conn->{"password"} // ""
$conn->{"password"}
Truth table0%0/2
A
A= $conn->{"user"}
Aresult
11
00
Truth table0%0/2
A
A= $conn->{"password"}
Aresult
11
00
162 { RaiseError => 1, PrintError => 0, AutoCommit => 1 });
163 }
164  
165 0 ▶sub _query {
Subroutine _query: not called CC=5 SCAR=34.0
166 0 my ($conn, $sql, $ms) = @_;
167 0 my $dbh = _connect($conn);
168 0 my $rows = eval {
169 0 ▶ $dbh->do("SET statement_timeout = $ms") if $conn->{dsn} =~ /\Adbi:Pg:/i;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"dsn"} =~ /\Adbi:Pg:/i 0 0 0
170 0 my $sth = $dbh->prepare($sql);
171 0 $sth->execute;
172 0 my @out;
173 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
174 0 ▶ push @out, { map { $_ => (ref($row->{$_}) eq 'ARRAY' ? [ @{ $row->{$_} } ] : $row->{$_}) }
Branch: 1 branch
BranchTrueFalseTotal
ref $row->{$_} eq 'ARRAY' ? : 0 0 0
175 keys %$row };
176 }
177 0 \@out;
178 };
179 0 my $err = $@;
180 0 eval { $dbh->disconnect };
181 0 ▶ die $err unless $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows 0 0 0
182 0 return $rows;
183 }
184  
185 # --- the listener -----------------------------------------------------------------------------
186  
187 0 ▶sub _listen {
Subroutine _listen: not called CC=4 SCAR=30.0
188 0 my ($self) = @_;
189 0 my $channel = $self->config->{listen};
190 0 ▶ return unless defined($channel) && length($channel);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel and length $channel 0 0 0
Condition0%0/3
defined $channel and length $channel
!ll&&!rl&&r
000
MC/DC0%0/2
defined $channel and length $channel
defined $channel length $channel
Truth table0%0/3
A and B
A= defined $channelB= length $channel
ABresult
0X0
100
111
191  
192 0 my %conn = (dsn => $self->dsn, user => $self->config->{user}, password => $self->config->{password});
193 0 my $sub = Mojo::IOLoop->subprocess;
194 0 $self->_listener($sub);
195 0 weaken(my $weak = $self);
196 0 my $parent = $$;
197 0 my $ready = 0;
198  
199 0 ▶ $sub->on(progress => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
200 0 my ($sp, $msg) = @_;
201 0 ▶ return unless $weak && ($weak->_listener // 0) == $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and ($weak->_listener // 0) == $sub 0 0 0
Condition0%0/5
$weak and ($weak->_listener // 0) == $sub
!ll&&!rl&&r
000
$weak->_listener // 0
l!l
00
MC/DC0%0/2
$weak and ($weak->_listener // 0) == $sub
$weak $weak->_listener
Truth table0%0/3
A and B
A= $weakB= $weak->_listener
ABresult
0X0
100
111
202 0 ▶ if (($msg // '') eq 'ready') {
Branch: 2 branches
BranchTrueFalseTotal
if (($msg // '') eq 'ready') { } 0 0 0
elsif (($msg // '') eq 'notify') { } 0 0 0
Condition0%0/2
$msg // ""
l!l
00
MC/DC0%0/1
$msg // ""
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
203 0 $ready = 1;
204 ▶ } elsif (($msg // '') eq 'notify') {
Condition0%0/2
$msg // ""
l!l
00
MC/DC0%0/1
$msg // ""
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
205 0 my $cb = $weak->_notify;
206 0 ▶ $cb->() if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
207 }
208 0 });
209  
210 # A listener that never got as far as LISTEN is a blocked connect: killed like a fetch.
211 0 ▶ my $limit = ($self->config->{connect_timeout_s} // 5) + $KILL_MARGIN_S;
Condition0%0/2
$self->config->{"connect_timeout_s"} // 5
l!l
00
MC/DC0%0/1
$self->config->{"connect_timeout_s"} // 5
$self->config->{"connect_timeout_s"}
Truth table0%0/2
A
A= $self->config->{"connect_timeout_s"}
Aresult
11
00
212 0 ▶ my $kill = Mojo::IOLoop->timer($limit => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
213 0 ▶ return unless $weak && !$ready && ($weak->_listener // 0) == $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and not $ready and ($weak->_listener // 0) == $sub 0 0 0
Condition0%0/8
$weak and not $ready and ($weak->_listener // 0) == $sub
!ll&&!rl&&r
000
$weak and not $ready
!ll&&!rl&&r
000
$weak->_listener // 0
l!l
00
MC/DC0%0/3
$weak and not $ready and ($weak->_listener // 0) == $sub
$weak not $ready $weak->_listener
Truth table0%0/4
A and not(B) and C
A= $weakB= not $readyC= $weak->_listener
ABCresult
0XX0
10X0
1100
1111
214 0 $weak->_kill($sub);
215 0 });
216  
217 $sub->run(
218 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
219 0 my ($sp) = @_;
220 0 my $dbh = _connect(\%conn);
221 0 $dbh->do('LISTEN ' . $channel);
222 0 $sp->progress('ready');
223 0 my $select = IO::Select->new;
224 0 ▶ open(my $sock, '<&=', $dbh->{pg_socket}) or die "no socket to wait on\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $sock, "<&=", $dbh->{"pg_socket"} or die "no socket to wait on\n" 0 0 0
225 0 $select->add($sock);
226 0 ▶ while (getppid() == $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (getppid == $parent) 0 0 0
227 0 $select->can_read(2);
228 0 my $n = 0;
229 0 ▶ $n++ while $dbh->pg_notifies;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->pg_notifies and do { ++$n } 0 0 0
230 0 ▶ $sp->progress('notify') if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 0 0
231 }
232 0 eval { $dbh->disconnect };
233 0 return 1;
234 },
235 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
236 0 my ($sp, $err) = @_;
237 0 Mojo::IOLoop->remove($kill);
238 0 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
239 # Released on purpose: _listener was cleared first. Otherwise it ended by itself, and the
240 # next fetch starts it again; the poll carries on meanwhile.
241 0 ▶ $weak->_listener(undef) if ($weak->_listener // 0) == $sub;
Branch: 1 branch
BranchTrueFalseTotal
if ($weak->_listener // 0) == $sub 0 0 0
Condition0%0/2
$weak->_listener // 0
l!l
00
MC/DC0%0/1
$weak->_listener // 0
$weak->_listener
Truth table0%0/2
A
A= $weak->_listener
Aresult
11
00
242 },
243 0 );
244 0 return;
245 }
246  
247 1 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.1
Pod undocumented
248 1 my ($self, $global) = @_;
249 1 ▶ return if $global;
Branch: 1 branch
BranchTrueFalseTotal
if $global 0 1 1
250 1 $self->release;
251 }
252  
253  
254 4 1;
255  
256 __END__
« lib/Langertha/Skeid/OpenAPI.pm ↑ Summary blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm »