blib/lib/Ekorn/Pipeline/Workers/Worker.pm

filter: Statementstmt 100% 79 / 79 Branchbran 82.3% 28 / 34 Conditioncond 66.6% 16 / 24 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 86.7% 144 / 166 CC 28 SCAR 21.8
CC
28
Cov
90%
run
17.6
__ANON__
6.0
_crossing
4.0
CRAP 8.9
« blib/lib/Ekorn/Pipeline/Workers/Pool.pm ↑ Summary blib/lib/Ekorn/Pipeline/Workers.pm »
1 178 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Workers::Worker;
4  
5 178 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 178 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 178 ▶use Ekorn::Pipeline;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 178 ▶use Ekorn::Pipeline::Gather ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 178 ▶use Ekorn::Pipeline::Source ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 178 ▶use Ekorn::Pipeline::Workers::Wire qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 write_frame read_frame resolve unplaced
12 178 token_variable);
13  
14 178 our $VERSION = 'v0.1.0';
15  
16  
17 # A worker: a new perl, run as
18 #
19 # perl -I... -e 'require Ekorn::Pipeline::Workers::Worker; Ekorn::Pipeline::Workers::Worker::run()' PORT
20 #
21 # with its token in its environment, never on its command line, which
22 # every user of the computer can read (D16). It takes the token out of
23 # %ENV, so the block, and what it starts, never sees it.
24 #
25 # It connects to the parent, says who it is, takes its setup, loads the
26 # functions it names, runs init, and says it's ready; then takes a
27 # value, runs the block on it, and replies, until the parent closes its
28 # socket; then done.
29 #
30 # The block runs inside a run of its own, made for each value, so that
31 # Ekorn itself gives it the parent's context, through a source, and
32 # ending, by calling it from a flush when the parent was ending. The
33 # result's number and original are set by hand, in the one variable of
34 # Ekorn's that isn't lexical. A stop or an error is caught around the
35 # block, before the inner run sees it, and sent back as it was, an
36 # error object too, if it can be frozen (D13). A stop crosses as its
37 # values, which the stage throws again as an Ekorn::Pipeline::Stop; one
38 # that isn't an array carries none, as in Ekorn (D18).
39 178 ▶sub run () {
Subroutine run: called CC=17 SCAR=28.7
Pod documented
40 178 my($port) = @ARGV;
41 178 ▶ my $token = delete $ENV{token_variable()} // '';
Condition50%1/2
delete $ENV{token_variable()} // ""
l!l
1780
MC/DC0%0/1
delete $ENV{token_variable()} // ""
delete $ENV{token_variable()}
Truth table50%1/2
A
A= delete $ENV{token_variable()}
Aresult
11
00
42 178 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("PeerAddr", "127.0.0.1", "PeerPort", $port, "Proto", "tcp") or die "A worker of Ekorn::Pipeline::Workers couldn't connect: $!\n" 178 0 178
43 PeerAddr => '127.0.0.1', PeerPort => $port,
44 Proto => 'tcp',
45 ) or die "A worker of Ekorn::Pipeline::Workers couldn't connect: $!\n";
46 178 binmode $socket;
47 178 ▶ write_frame($socket, Storable::nfreeze([$token, $$])) or exit 2;
Branch: 1 branch
BranchTrueFalseTotal
write_frame($socket, Storable::nfreeze([$token, $$])) or exit 2 178 0 178
48 178 ▶ my $frame = read_frame($socket) // exit 0;
Condition50%1/2
read_frame($socket) // exit 0
l!l
1780
MC/DC0%0/1
read_frame($socket) // exit 0
read_frame($socket)
Truth table50%1/2
A
A= read_frame($socket)
Aresult
11
00
49 178 my $setup = Storable::thaw($frame);
50 178 my(@state, $block, $done);
51 178 my $ready = eval {
52 178 ▶ $block = resolve($setup->{block}) // die "there's no $setup->{block}\n";
Condition50%1/2
resolve($setup->{"block"}) // die(qq[there's no $setup->{"block"}\n])
l!l
1780
MC/DC0%0/1
resolve($setup->{"block"}) // die(qq[there's no $setup->{"block"}\n])
resolve($setup->{"block"})
Truth table50%1/2
A
A= resolve($setup->{"block"})
Aresult
11
00
53 $done = $setup->{done}
54 178 ▶ && (resolve($setup->{done}) // die "there's no $setup->{done}\n");
Condition60%3/5
$setup->{'done'} && (resolve($setup->{'done'}) // die("there's no $setup->{'done'}\n"))
!ll&&!rl&&r
17305
resolve($setup->{"done"}) // die(qq[there's no $setup->{"done"}\n])
l!l
50
MC/DC50%1/2
$setup->{'done'} && (resolve($setup->{'done'}) // die("there's no $setup->{'done'}\n"))
$setup->{'done'} resolve($setup->{"done"})
Truth table66%2/3
A && B
A= $setup->{'done'}B= resolve($setup->{"done"})
ABresult
0X0
100
111
55 178 ▶ if ($setup->{init}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setup->{"init"}) 30 148 178
56 30 ▶ my $init = resolve($setup->{init}) // die "there's no $setup->{init}\n";
Condition50%1/2
resolve($setup->{"init"}) // die(qq[there's no $setup->{"init"}\n])
l!l
300
MC/DC0%0/1
resolve($setup->{"init"}) // die(qq[there's no $setup->{"init"}\n])
resolve($setup->{"init"})
Truth table50%1/2
A
A= resolve($setup->{"init"})
Aresult
11
00
57 30 @state = $init->(@{ $setup->{init_args} });
58 }
59 176 1;
60 };
61 178 ▶ write_frame($socket, Storable::nfreeze($ready ? [1] : [0, "$@"])) or exit 2;
Branch: 2 branches
BranchTrueFalseTotal
write_frame($socket, Storable::nfreeze($ready ? [1] : [0, "$@"])) or exit 2 178 0 178
$ready ? : 176 2 178
62 178 ▶ exit 0 unless $ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $ready 2 176 178
63 176 my($value, $where, $kind, $payload);
64 337 ▶ my $call = sub () {
Subroutine __ANON__: called CC=6 SCAR=18.0
65 337 my @results = eval {
66 local $Ekorn::Pipeline::CURRENT_RESULT
67 = defined $where->{number}
68 337 ▶ ? [$where->{number} - 1, $where->{original}]
Branch: 1 branch
BranchTrueFalseTotal
defined $where->{'number'} ? : 337 0 337
69 : undef;
70 337 local $_ = $value;
71 337 $block->($value, @state);
72 };
73 325 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 8 317 325
74 ($kind, $payload)
75 = ref $error && $error isa 'Ekorn::Pipeline::Stop'
76 8 ▶ ? (stop => eval { [@{ $error }[1 .. $#$error]] } // [])
Branch: 1 branch
BranchTrueFalseTotal
ref $error && $error isa 'Ekorn::Pipeline::Stop' ? : 2 6 8
Condition80%4/5
ref $error and $error isa "Ekorn::Pipeline::Stop"
!ll&&!rl&&r
332
eval { do { [@{$error;}[1 .. $error->$#*]] } } // []
l!l
20
MC/DC100%2/2
ref $error and $error isa "Ekorn::Pipeline::Stop"
ref $error $error isa "Ekorn::Pipeline::Stop"
MC/DC0%0/1
eval { do { [@{$error;}[1 .. $error->$#*]] } } // []
eval { do { [@{$error;}[1 .. $error->$#*]] } }
Truth table100%3/3
A and B
A= ref $errorB= $error isa "Ekorn::Pipeline::Stop"
ABresult
0X0
100
111
Truth table50%1/2
A
A= eval { do { [@{$error;}[1 .. $error->$#*]] } }
Aresult
11
00
77 : (err => _crossing($error));
78 8 return;
79 }
80 317 ($kind, $payload) = (ok => \@results);
81 317 return;
82 176 };
83 337 ▶ my $stage = Ekorn::Pipeline::Gather->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 return (
85 337 ▶ sub { $where->{ending} ? () : $call->() },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$where->{'ending'} ? : 1 336 337
86 325 ▶ sub { $where->{ending} ? $call->() : () },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$where->{'ending'} ? : 1 324 325
87 337 );
88 176 });
89 337 ▶ my $source = Ekorn::Pipeline::Source->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 337 my $given = 0;
91 337 ▶ return {step => sub { $given++ ? () : 1 }, context => $where->{context}};
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$given++ ? : 325 337 662
92 176 });
93 176 my $run = Ekorn::Pipeline->new($stage);
94 176 ▶ while (defined(my $job = read_frame($socket))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $job = read_frame($socket))) 337 164 501
95 337 (my $seq, $value, $where) = @{ Storable::thaw($job) };
96 337 ($kind, $payload)
97 = (err => "$setup->{stage} ran nothing for $setup->{what} #$seq\n");
98 337 $run->iterator($source)->all;
99 my $reply
100 325 ▶ = eval { Storable::nfreeze([$seq, $kind, $payload]) }
Condition66%2/3
eval { do { Storable::nfreeze([$seq, $kind, $payload]) } } // Storable::nfreeze([$seq, "err", qq[$setup->{"stage"} can't send back the results of $setup->{"what"} #${seq}: ] . unplaced($@) . "\n"])
l!l&&r!l&&!r
32320
MC/DC0%0/2
eval { do { Storable::nfreeze([$seq, $kind, $payload]) } } // Storable::nfreeze([$seq, "err", qq[$setup->{"stage"} can't send back the results of $setup->{"what"} #${seq}: ] . unplaced($@) . "\n"])
eval { do { Storable::nfreeze([$seq, $kind, $payload]) } } Storable::nfreeze([$seq, "err", qq[$setup->{"stage"} can't send back the results of $setup->{"what"} #${seq}: ] . unplaced($@) . "\n"])
Truth table66%2/3
A // B
A= eval { do { Storable::nfreeze([$seq, $kind, $payload]) } }B= Storable::nfreeze([$seq, "err", qq[$setup->{"stage"} can't send back the results of $setup->{"what"} #${seq}: ] . unplaced($@) . "\n"])
ABresult
1X1
011
000
101 // Storable::nfreeze([
102 $seq,
103 err =>
104 "$setup->{stage} can't send back the results of $setup->{what} #$seq: "
105 . unplaced($@) . "\n",
106 ]);
107 325 ▶ write_frame($socket, $reply) or last;
Branch: 1 branch
BranchTrueFalseTotal
write_frame($socket, $reply) or last 325 0 325
108 }
109 164 ▶ $done->(@state) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 5 159 164
110 164 STDOUT->flush;
111 164 STDERR->flush;
112 164 exit 0;
113 }
114  
115  
116 # An error as it was, an object included, so the program can catch it
117 # as it would a plain stage's (Ekorn-Pipeline's 2026-0065, D13); one
118 # that can't be frozen, such as an object holding a handle, crosses as
119 # its message, ending in a newline, so the stage adds no line of its
120 # own.
121 6 ▶sub _crossing ($error) {
Subroutine _crossing: called CC=4 SCAR=13.9
122 6 ▶ return $error if !ref $error || eval { Storable::nfreeze([$error]); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if not ref $error or eval { do { Storable::nfreeze([$error]); 1 } } 5 1 6
Condition100%3/3
not ref $error or eval { do { Storable::nfreeze([$error]); 1 } }
l!l&&r!l&&!r
321
MC/DC100%2/2
not ref $error or eval { do { Storable::nfreeze([$error]); 1 } }
not ref $error eval { do { Storable::nfreeze([$error]); 1 } }
Truth table100%3/3
not(A) or B
A= not ref $errorB= eval { do { Storable::nfreeze([$error]); 1 } }
ABresult
1X1
011
000
123 1 my $message = "$error";
124 1 ▶ return $message =~ m( \n \z )x ? $message : "$message\n";
Branch: 1 branch
BranchTrueFalseTotal
$message =~ / \n \z /ux ? : 0 1 1
125 }
126  
127 178 1;
128  
129 __END__
« blib/lib/Ekorn/Pipeline/Workers/Pool.pm ↑ Summary blib/lib/Ekorn/Pipeline/Workers.pm »