Mojo.pm

filter: Statementstmt 10.2% 62 / 606 Branchbran 0.3% 1 / 262 Conditioncond 0.0% 0 / 97 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 18.5% 20 / 108 Podpod 72.7% 16 / 22 totaltotal 8.6% 99 / 1151 CC 171 SCAR 44.3
CC
171
Cov
7%
new
380.0
call_nb
342.0
_subproc
210.0
CRAP 83.6
↑ Summary Mojo/Steps.pm »
1 package JobCenter::Client::Mojo;
2 1 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.48'; # VERSION
5  
6 #
7 # Mojo's default reactor uses EV, and EV does not play nice with signals
8 # without some handholding. We either can try to detect EV and do the
9 # handholding, or try to prevent Mojo using EV.
10 #
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
12 1 ▶ $ENV{'MOJO_REACTOR'} = 'Mojo::Reactor::Poll' unless $ENV{'MOJO_REACTOR'};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"MOJO_REACTOR"} 1 0 1
13 }
14  
15 # more Mojolicious
16 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Mojo::IOLoop::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # standard perl
21 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Encode qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # from cpan
33 1 ▶use JSON::RPC2::TwoWay 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 # JSON::RPC2::TwoWay depends on JSON::MaybeXS anyways, so it can be used here
35 # without adding another dependency
36 1 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use MojoX::NetstringStream 0.06; # for the enhanced close
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # us
40 1 ▶use JobCenter::Client::Mojo::Steps;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 has [qw(
43 actions address auth clientid conn debug ioloop jobs json
44 lastping log method ns ping_timeout port rpc timeout tls token who
45 )];
46  
47 # keep in sync with the jobcenter
48 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 WORK_OK => 0, # exit codes for work method
50 WORK_PING_TIMEOUT => 92,
51 WORK_CONNECTION_CLOSED => 91,
52 1 };
53  
54 0 ▶sub new {
Subroutine new: not called CC=19 SCAR=59.4
Pod documented
55 0 my ($class, %args) = @_;
56 0 my $self = $class->SUPER::new();
57  
58 0 ▶ my $address = $args{address} // '127.0.0.1';
Condition0%0/2
$args{"address"} // "127.0.0.1"
l!l
00
MC/DC0%0/1
$args{"address"} // "127.0.0.1"
$args{"address"}
Truth table0%0/2
A
A= $args{"address"}
Aresult
11
00
59 0 ▶ my $debug = $args{debug} // 0; # or 1?
Condition0%0/2
$args{"debug"} // 0
l!l
00
MC/DC0%0/1
$args{"debug"} // 0
$args{"debug"}
Truth table0%0/2
A
A= $args{"debug"}
Aresult
11
00
60 0 ▶ $self->{ioloop} = $args{ioloop} // Mojo::IOLoop->singleton;
Condition0%0/3
$args{"ioloop"} // "Mojo::IOLoop"->singleton
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ioloop"} // "Mojo::IOLoop"->singleton
$args{"ioloop"} "Mojo::IOLoop"->singleton
Truth table0%0/3
A // B
A= $args{"ioloop"}B= "Mojo::IOLoop"->singleton
ABresult
1X1
011
000
61 0 ▶ my $json = $args{json} // 1;
Condition0%0/2
$args{"json"} // 1
l!l
00
MC/DC0%0/1
$args{"json"} // 1
$args{"json"}
Truth table0%0/2
A
A= $args{"json"}
Aresult
11
00
62 0 ▶ my $log = $args{log} // Mojo::Log->new(level => ($debug) ? 'debug' : 'info');
Branch: 1 branch
BranchTrueFalseTotal
$debug ? : 0 0 0
Condition0%0/3
$args{"log"} // "Mojo::Log"->new("level", $debug ? "debug" : "info")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"log"} // "Mojo::Log"->new("level", $debug ? "debug" : "info")
$args{"log"} "Mojo::Log"->new("level", $debug ? "debug" : "info")
Truth table0%0/3
A // B
A= $args{"log"}B= "Mojo::Log"->new("level", $debug ? "debug" : "info")
ABresult
1X1
011
000
63 0 ▶ my $method = $args{method} // 'password';
Condition0%0/2
$args{"method"} // "password"
l!l
00
MC/DC0%0/1
$args{"method"} // "password"
$args{"method"}
Truth table0%0/2
A
A= $args{"method"}
Aresult
11
00
64 0 ▶ my $port = $args{port} // 6522;
Condition0%0/2
$args{"port"} // 6522
l!l
00
MC/DC0%0/1
$args{"port"} // 6522
$args{"port"}
Truth table0%0/2
A
A= $args{"port"}
Aresult
11
00
65 0 ▶ my $timeout = $args{timeout} // 60;
Condition0%0/2
$args{"timeout"} // 60
l!l
00
MC/DC0%0/1
$args{"timeout"} // 60
$args{"timeout"}
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
66 0 ▶ my $tls = $args{tls} // 0;
Condition0%0/2
$args{"tls"} // 0
l!l
00
MC/DC0%0/1
$args{"tls"} // 0
$args{"tls"}
Truth table0%0/2
A
A= $args{"tls"}
Aresult
11
00
67 0 my $tls_ca = $args{tls_ca};
68 0 my $tls_cert = $args{tls_cert};
69 0 my $tls_key = $args{tls_key};
70 0 ▶ my $token = $args{token} or croak 'no token?';
Branch: 1 branch
BranchTrueFalseTotal
my $token = $args{"token"} or croak("no token?") 0 0 0
71 0 ▶ my $who = $args{who} or croak 'no who?';
Branch: 1 branch
BranchTrueFalseTotal
my $who = $args{"who"} or croak("no who?") 0 0 0
72  
73 0 $self->{address} = $address;
74 0 ▶ $self->{debug} = $args{debug} // 1;
Condition0%0/2
$args{"debug"} // 1
l!l
00
MC/DC0%0/1
$args{"debug"} // 1
$args{"debug"}
Truth table0%0/2
A
A= $args{"debug"}
Aresult
11
00
75 0 $self->{jobs} = {};
76 0 $self->{json} = $json;
77 $self->{jsonobject} = $args{jsonobject} //
78 JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1),
79 0 ▶ $self->{ping_timeout} = $args{ping_timeout} // 300;
Condition0%0/5
$args{"jsonobject"} // "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1)
l!l&&r!l&&!r
000
$args{"ping_timeout"} // 300
l!l
00
MC/DC0%0/2
$args{"jsonobject"} // "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1)
$args{"jsonobject"} "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1)
MC/DC0%0/1
$args{"ping_timeout"} // 300
$args{"ping_timeout"}
Truth table0%0/3
A // B
A= $args{"jsonobject"}B= "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{"ping_timeout"}
Aresult
11
00
80 0 $self->{log} = $log;
81 0 $self->{method} = $method;
82 0 $self->{port} = $port;
83 0 $self->{timeout} = $timeout;
84 0 $self->{tls} = $tls;
85 0 $self->{tls_ca} = $tls_ca;
86 0 $self->{tls_cert} = $tls_cert;
87 0 $self->{tls_key} = $tls_key;
88 0 $self->{token} = $token;
89 0 $self->{who} = $who;
90 0 ▶ $self->{autoconnect} = ($args{autoconnect} //= 1);
Condition0%0/2
$args{"autoconnect"} //= 1
l!l
00
MC/DC0%0/1
$args{"autoconnect"} //= 1
$args{"autoconnect"}
Truth table0%0/2
A
A= $args{"autoconnect"}
Aresult
11
00
91  
92 0 ▶ return $self if !$args{autoconnect};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"autoconnect"} 0 0 0
93  
94 0 $self->connect;
95  
96 0 ▶ return $self if $self->{auth};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auth"} 0 0 0
97 0 return;
98 }
99  
100 0 ▶sub connect_nb {
Subroutine connect_nb: not called CC=8 SCAR=42.8
Pod documented
101 0 my ($self, $connect_cb) = @_;
102  
103 0 ▶ croak('connect_cb should be a coderef') if ref $connect_cb ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $connect_cb ne "CODE" 0 0 0
104 0 ▶ croak('connect already in progress') if $self->{on_connect};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_connect"} 0 0 0
105  
106 0 delete $self->ioloop->{__exit__};
107 0 delete $self->{auth};
108 0 $self->{actions} = {};
109  
110 my $clarg = {
111 address => $self->{address},
112 port => $self->{port},
113 tls => $self->{tls},
114 0 };
115 0 ▶ $clarg->{tls_ca} = $self->{tls_ca} if $self->{tls_ca};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tls_ca"} 0 0 0
116 0 ▶ $clarg->{tls_cert} = $self->{tls_cert} if $self->{tls_cert};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tls_cert"} 0 0 0
117 0 ▶ $clarg->{tls_key} = $self->{tls_key} if $self->{tls_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tls_key"} 0 0 0
118  
119 0 my $debug = do {
120 0 weaken(my $self = $self);
121 0 ▶ $self->{debug} ? sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 ▶ if ($self->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log'}) { } 0 0 0
123 0 $self->log->debug(@_);
124 } else {
125 0 warn join(' ', @_)."\n";
126 }
127 } : undef
128 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} ? : 0 0 0
129  
130 my $rpc = JSON::RPC2::TwoWay->new(
131 debug => $debug,
132 json => $self->{jsonobject},
133 0 ▶ ) or croak 'no rpc?';
Branch: 1 branch
BranchTrueFalseTotal
my $rpc = "JSON::RPC2::TwoWay"->new("debug", $debug, "json", $self->{"jsonobject"}) or croak("no rpc?") 0 0 0
134 0 ▶ $rpc->register('greetings', sub { $self->rpc_greetings(@_) }, notification => 1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ $rpc->register('job_done', sub { $self->rpc_job_done(@_) }, notification => 1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 ▶ $rpc->register('ping', sub { $self->rpc_ping(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 ▶ $rpc->register('task_ready', sub { $self->rpc_task_ready(@_) }, notification => 1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
138  
139 0 my $on_connect;
140 my $clientid;
141 0 ▶ my $tmr = $self->ioloop->timer($self->{timeout} => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 $on_connect->(shift, 0, 'timeout waiting for greeting');
143 0 });
144 0 $on_connect = $self->{on_connect} = do {
145 0 weaken(my $self = $self);
146 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
147 0 my $loop = shift;
148 0 my $success = shift;
149 0 $loop->remove($tmr);
150 0 ▶ delete $self->{on_connect} if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
151 0 ▶ if (! $success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
152 0 $loop->remove($clientid);
153 0 ▶ $self->{auth} = 0 if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
154 }
155 0 ▶ $connect_cb->($success, @_) if $self and $connect_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $self and $connect_cb 0 0 0
Condition0%0/3
$self and $connect_cb
!ll&&!rl&&r
000
MC/DC0%0/2
$self and $connect_cb
$self $connect_cb
Truth table0%0/3
A and B
A= $selfB= $connect_cb
ABresult
0X0
100
111
156 0 };
157 };
158 0 my $log = $self->log;
159 $clientid = $self->ioloop->client(
160 0 ▶ $clarg => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
161 0 my ($loop, $err, $stream) = @_;
162 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
163 0 $err =~ s/\n$//s;
164 0 ▶ if (my $on_connect = $self->{on_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_connect = $self->{"on_connect"}) 0 0 0
165 0 $on_connect->($loop, '', "connection to API failed: $err");
166 }
167 0 return;
168 }
169 0 my $ns = MojoX::NetstringStream->new(stream => $stream);
170 0 $self->{ns} = $ns;
171 my $conn = $rpc->newconnection(
172 owner => $self,
173 0 ▶ write => sub { $ns->write(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 );
175 0 $self->{conn} = $conn;
176 0 ▶ $ns->on(chunk => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
177 0 my ($ns2, $chunk) = @_;
178 #say 'got chunk: ', $chunk;
179 0 my @err = $conn->handle($chunk);
180 0 ▶ $log->debug('chunk handler: ' . join(' ', grep defined, @err)) if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 0 0 0
181 0 ▶ $ns->close if $err[0];
Branch: 1 branch
BranchTrueFalseTotal
if $err[0] 0 0 0
182 0 });
183 0 ▶ $ns->on(close => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
184 # this cb is called during global destruction, at
185 # least on old perls where
186 # Mojo::Util::_global_destruction() won't work
187 0 ▶ return unless $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn 0 0 0
188 0 $conn->close;
189 0 ▶ $self->{auth} = '' if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
190 0 $log->info('connection to API closed');
191 0 $self->emit(disconnect => WORK_CONNECTION_CLOSED); # todo doc
192 0 });
193 0 });
194  
195 0 $self->{rpc} = $rpc;
196 0 $self->{clientid} = $clientid;
197 }
198  
199 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
200 0 my $self = shift;
201  
202 0 ▶ $self->on(disconnect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 my ($self, $code) = @_;
204 0 $self->ioloop->stop;
205 0 });
206  
207 0 my $done = 0;
208 0 my $log = $self->log;
209  
210 0 ▶ $self->connect_nb(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
211 0 my ($success, $msg) = @_;
212 0 $done = 1;
213 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
214 0 ▶ $log->info($msg) if defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 0 0 0
215 } else {
216 0 $log->error($msg);
217 }
218 0 });
219  
220 0 $self->log->debug('starting handshake');
221 0 ▶ $self->_loop(sub { ! $done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 $self->log->debug('done with handhake?');
223 0 $self->unsubscribe('disconnect');
224 0 return $self->is_connected;
225 }
226  
227 0 ▶sub is_connected {
Subroutine is_connected: not called CC=2 SCAR=17.9
Pod undocumented
228 0 my $self = shift;
229 0 ▶ return $self->{auth} && !$self->ioloop->{__exit__};
Condition0%0/3
$self->{'auth'} && !$self->ioloop->{'__exit__'}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'auth'} && !$self->ioloop->{'__exit__'}
$self->{'auth'} !$self->ioloop->{'__exit__'}
Truth table0%0/3
A && not(B)
A= $self->{'auth'}B= !$self->ioloop->{'__exit__'}
ABresult
0X0
100
111
230 }
231  
232 0 ▶sub rpc_greetings {
Subroutine rpc_greetings: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my ($self, $c, $i) = @_;
234 #$self->ioloop->delay(
235 0 my $ioloop = $self->ioloop;
236 JobCenter::Client::Mojo::Steps->new(ioloop => $ioloop)->steps([
237 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
238 0 my $steps = shift;
239 0 ▶ die "wrong api version $i->{version} (expected 1.1)" unless $i->{version} eq '1.1';
Branch: 1 branch
BranchTrueFalseTotal
unless $i->{"version"} eq "1.1" 0 0 0
240 0 $self->log->info('got greeting from ' . $i->{who});
241 0 $c->call('hello', {who => $self->who, method => $self->method, token => $self->token}, $steps->next());
242 },
243 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
244 0 my ($steps, $e, $r) = @_;
245 0 my $w;
246 #say 'hello returned: ', Dumper(\@_);
247 0 ▶ die "hello returned error $e->{message} ($e->{code})" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 0 0
248 0 ▶ die 'no results from hello?' unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
249 0 my $msg;
250 0 ($r, $w) = @$r;
251 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
252 0 $msg = "hello returned: $r, $w";
253 0 $self->{auth} = 1;
254 } else {
255 0 ▶ $msg = 'hello failed: ' . ($w // '');
Condition0%0/2
$w // ""
l!l
00
MC/DC0%0/1
$w // ""
$w
Truth table0%0/2
A
A= $w
Aresult
11
00
256 0 $self->{auth} = 0; # defined but false
257 }
258 0 ▶ if (my $on_connect = $self->{on_connect}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $on_connect = $self->{'on_connect'}) { } 0 0 0
elsif ($self->{'auth'}) { } 0 0 0
259 0 $on_connect->($ioloop, $self->{auth}, $msg);
260 } elsif ($self->{auth}) {
261 0 $self->log->info($msg);
262 } else {
263 0 $self->log->error($msg);
264 }
265 0 ▶ }], sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
266 0 my ($err) = @_;
267 0 $self->log->error('something went wrong in handshake: ' . $err);
268 0 $self->{auth} = '';
269 0 ▶ if (my $on_connect = $self->{on_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_connect = $self->{"on_connect"}) 0 0 0
270 0 $on_connect->($ioloop, '', $err);
271 }
272 0 });
273 }
274  
275 0 ▶sub rpc_job_done {
Subroutine rpc_job_done: not called CC=4 SCAR=30.0
Pod undocumented
276 0 my ($self, $conn, $i) = @_;
277 0 my $job_id = $i->{job_id};
278 0 my $outargs = $i->{outargs};
279 0 my $outargsj = $self->{jsonobject}->encode($outargs);
280 0 ▶ $outargs = $outargsj if $self->{json};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} 0 0 0
281 0 $outargsj = decode_utf8($outargsj); # for debug printing
282 0 my $rescb = delete $self->{jobs}->{$job_id};
283 0 ▶ if ($rescb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rescb) { } 0 0 0
284 0 $self->log->debug("got job_done: for job_id $job_id result: $outargsj");
285 0 local $@;
286 0 eval {
287 0 $rescb->($job_id, $outargs);
288 };
289 0 ▶ $self->log->info("got $@ calling result callback") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
290 } else {
291 0 $self->log->debug("got job_done for unknown job $job_id result: $outargsj");
292 }
293 }
294  
295 0 ▶sub call {
Subroutine call: not called CC=1 SCAR=6.9
Pod documented
296 0 my ($self, %args) = @_;
297 0 my ($done, $job_id, $outargs);
298 0 ▶ $args{cb1} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
299 0 ($job_id, $outargs) = @_;
300 0 ▶ $done++ unless $job_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $job_id 0 0 0
301 0 };
302 0 ▶ $args{cb2} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 0 ($job_id, $outargs) = @_;
304 0 $done++;
305 0 };
306 0 $self->call_nb(%args);
307  
308 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
309  
310 0 return $job_id, $outargs;
311 }
312  
313 0 ▶sub call_nb {
Subroutine call_nb: not called CC=18 SCAR=58.3
Pod documented
314 0 my ($self, %args) = @_;
315 0 ▶ my $wfname = $args{wfname} or die 'no workflowname?';
Branch: 1 branch
BranchTrueFalseTotal
my $wfname = $args{"wfname"} or die "no workflowname?" 0 0 0
316 0 my $vtag = $args{vtag};
317 0 ▶ my $inargs = $args{inargs} // '{}';
Condition0%0/2
$args{"inargs"} // "{}"
l!l
00
MC/DC0%0/1
$args{"inargs"} // "{}"
$args{"inargs"}
Truth table0%0/2
A
A= $args{"inargs"}
Aresult
11
00
318 0 ▶ my $callcb = $args{cb1} // die 'no call callback?';
Condition0%0/2
$args{"cb1"} // die("no call callback?")
l!l
00
MC/DC0%0/1
$args{"cb1"} // die("no call callback?")
$args{"cb1"}
Truth table0%0/2
A
A= $args{"cb1"}
Aresult
11
00
319 0 ▶ my $rescb = $args{cb2} // die 'no result callback?';
Condition0%0/2
$args{"cb2"} // die("no result callback?")
l!l
00
MC/DC0%0/1
$args{"cb2"} // die("no result callback?")
$args{"cb2"}
Truth table0%0/2
A
A= $args{"cb2"}
Aresult
11
00
320 0 ▶ my $timeout = $args{timeout} // $self->timeout * 5; # a bit hackish..
Condition0%0/2
$args{"timeout"} // $self->timeout * 5
l!l
00
MC/DC0%0/1
$args{"timeout"} // $self->timeout * 5
$args{"timeout"}
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
321 0 my $reqauth = $args{reqauth};
322 0 my $clenv = $args{clenv};
323 0 my $inargsj;
324  
325 0 ▶ if ($self->{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'json'}) { } 0 0 0
326 0 $inargsj = $inargs;
327 0 $inargs = $self->{jsonobject}->decode($inargs);
328 0 ▶ croak 'inargs is not a json object' unless ref $inargs eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inargs eq "HASH" 0 0 0
329 0 ▶ if ($clenv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clenv) 0 0 0
330 0 $clenv = $self->{jsonobject}->decode($clenv);
331 0 ▶ croak 'clenv is not a json object' unless ref $clenv eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $clenv eq "HASH" 0 0 0
332 }
333 0 ▶ if ($reqauth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqauth) 0 0 0
334 0 $reqauth = $self->{jsonobject}->decode($reqauth);
335 0 ▶ croak 'reqauth is not a json object' unless ref $reqauth eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $reqauth eq "HASH" 0 0 0
336 }
337 } else {
338 0 ▶ croak 'inargs should be a hashref' unless ref $inargs eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inargs eq "HASH" 0 0 0
339 # test encoding
340 0 $inargsj = $self->{jsonobject}->encode($inargs);
341 0 ▶ if ($clenv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clenv) 0 0 0
342 0 ▶ croak 'clenv should be a hashref' unless ref $clenv eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $clenv eq "HASH" 0 0 0
343 }
344 0 ▶ if ($reqauth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqauth) 0 0 0
345 0 ▶ croak 'reqauth should be a hashref' unless ref $reqauth eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $reqauth eq "HASH" 0 0 0
346 }
347 }
348  
349 0 $inargsj = decode_utf8($inargsj);
350 0 ▶ $self->log->debug("calling $wfname with '" . $inargsj . "'" . (($vtag) ? " (vtag $vtag)" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$vtag ? : 0 0 0
351  
352 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
353 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
354 0 my $steps = shift;
355 0 ▶ $self->conn->call('create_job', {
Branch: 2 branches
BranchTrueFalseTotal
$clenv ? : 0 0 0
$reqauth ? : 0 0 0
356 wfname => $wfname,
357 vtag => $vtag,
358 inargs => $inargs,
359 timeout => $timeout,
360 ($clenv ? (clenv => $clenv) : ()),
361 ($reqauth ? (reqauth => $reqauth) : ()),
362 }, $steps->next());
363 },
364 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
365 0 my ($steps, $e, $r) = @_;
366 0 my ($job_id, $msg);
367 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) { } 0 0 0
368 0 $self->log->error("create_job returned error: $e->{message} ($e->{code})");
369 0 $msg = "$e->{message} ($e->{code})"
370 } else {
371 0 ($job_id, $msg) = @$r; # fixme: check for arrayref?
372 0 ▶ if ($msg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg) { } 0 0 0
elsif ($job_id) { } 0 0 0
373 0 $self->log->error("create_job returned error: $msg");
374 } elsif ($job_id) {
375 0 $self->log->debug("create_job returned job_id: $job_id");
376 0 $self->jobs->{$job_id} = $rescb;
377 }
378 }
379 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
380 0 ▶ $msg = {error => $msg} unless ref $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg 0 0 0
381 0 ▶ $msg = $self->{jsonobject}->encode($msg) if $self->{json};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} 0 0 0
382 }
383 0 $callcb->($job_id, $msg);
384 0 ▶ }], sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
385 0 my ($err) = @_;
386 0 $self->log->error("Something went wrong in call_nb: $err");
387 0 $err = { error => $err };
388 0 ▶ $err = $self->{jsonobject}->encode($err) if $self->{json};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} 0 0 0
389 0 $callcb->(undef, $err);
390 0 });
391 }
392  
393 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
394 0 my ($self) = @_;
395  
396 0 $self->log->debug('closing connection');
397 0 $self->conn->close();
398 0 $self->ns->close();
399 0 %$self = ();
400 }
401  
402 0 ▶sub find_jobs {
Subroutine find_jobs: not called CC=4 SCAR=30.0
Pod documented
403 0 my ($self, $filter) = @_;
404 0 ▶ croak('no filter?') unless $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter 0 0 0
405  
406 #print 'filter: ', Dumper($filter);
407 0 ▶ $filter = $self->{jsonobject}->encode($filter) if ref $filter eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter eq "HASH" 0 0 0
408  
409 0 my ($done, $err, $jobs);
410 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
411 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 0 my $steps = shift;
413 # fixme: check results?
414 0 $self->conn->call('find_jobs', { filter => $filter }, $steps->next());
415 },
416 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
417 #say 'find_jobs call returned: ', Dumper(\@_);
418 0 my ($steps, $e, $r) = @_;
419 0 $done++; # received something so done waiting
420 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
421 0 $self->log->error("find_jobs got error $e->{message} ($e->{code})");
422 0 $err = $e->{message};
423 0 return;
424 }
425 0 $jobs = $r;
426 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 my ($err) = @_;
428 0 $done++;
429 0 $self->log->error("something went wrong with get_job_status: $err");
430 0 });
431  
432 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
433  
434 0 ▶ return $err, @$jobs if ref $jobs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $jobs eq "ARRAY" 0 0 0
435 0 return $err;
436 }
437  
438 0 ▶sub check_if_lock_exists {
Subroutine check_if_lock_exists: not called CC=4 SCAR=30.0
Pod documented
439 0 my ($self, $locktype, $lockvalue) = @_;
440 0 ▶ croak('no locktype?') unless $locktype;
Branch: 1 branch
BranchTrueFalseTotal
unless $locktype 0 0 0
441 0 ▶ croak('no lockvalue?') unless $lockvalue;
Branch: 1 branch
BranchTrueFalseTotal
unless $lockvalue 0 0 0
442  
443 0 my ($done, $found);
444 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
445 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
446 0 my $steps = shift;
447 # fixme: check results?
448 0 $self->conn->call(
449 'check_if_lock_exists',
450 { locktype => $locktype, lockvalue => $lockvalue },
451 $steps->next()
452 );
453 },
454 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
455 #say 'find_jobs call returned: ', Dumper(\@_);
456 0 my ($steps, $e, $r) = @_;
457 0 $done++; # received something so done waiting
458 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
459 0 $self->log->error("find_jobs got error $e->{message} ($e->{code})");
460 0 return;
461 }
462 0 $found = $r;
463 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
464 0 my ($err) = @_;
465 0 $done++;
466 0 $self->log->error("something went wrong with check_if_lock_exists: $err");
467 0 });
468  
469 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
470  
471 0 ▶ $found = $self->{jsonobject}->encode($found) if $self->{json};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} 0 0 0
472 0 return $found;
473 }
474  
475 0 ▶sub get_api_status {
Subroutine get_api_status: not called CC=2 SCAR=17.9
Pod undocumented
476 0 my ($self, $what) = @_;
477 0 ▶ croak('no what?') unless $what;
Branch: 1 branch
BranchTrueFalseTotal
unless $what 0 0 0
478  
479 0 my ($done, $result);
480 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
481 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
482 0 my $steps = shift;
483 0 $self->conn->call('get_api_status', { what => $what }, $steps->next());
484 },
485 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
486 #say 'call returned: ', Dumper(\@_);
487 0 my ($d, $e, $r) = @_;
488 0 $done++; # received something so done waiting
489 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
490 0 $self->log->error("get_api_status got error $e->{message} ($e->{code})");
491 0 $result = $e->{message};
492 0 return;
493 }
494 0 $result = $r;
495 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
496 0 my ($err) = @_;
497 0 $done++;
498 0 $self->log->eror("something went wrong with get_api_status: $err");
499 0 });
500  
501 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
502  
503 0 return $result;
504 }
505  
506 0 ▶sub get_job_status {
Subroutine get_job_status: not called CC=2 SCAR=17.9
Pod documented
507 0 my ($self, $job_id) = @_;
508 0 ▶ croak('no job_id?') unless $job_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $job_id 0 0 0
509  
510 0 my ($done, $job_id2, $outargs);
511 $self->get_job_status_nb(
512 job_id => $job_id,
513 0 ▶ statuscb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
514 0 ($job_id2, $outargs) = @_;
515 0 $done++;
516 0 return;
517 },
518 0 );
519 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
520 0 return $job_id2, $outargs;
521 }
522  
523 0 ▶sub get_job_status_nb {
Subroutine get_job_status_nb: not called CC=5 SCAR=34.0
Pod documented
524 0 my ($self, %args) = @_;
525 my $job_id = $args{job_id} or
526 0 ▶ croak('no job_id?');
Branch: 1 branch
BranchTrueFalseTotal
my $job_id = $args{"job_id"} or croak("no job_id?") 0 0 0
527  
528 0 my $statuscb = $args{statuscb};
529 0 ▶ croak('statuscb should be a coderef')
Branch: 1 branch
BranchTrueFalseTotal
if ref $statuscb ne "CODE" 0 0 0
530 if ref $statuscb ne 'CODE';
531  
532 0 my $notifycb = $args{notifycb};
533 0 ▶ croak('notifycb should be a coderef')
Branch: 1 branch
BranchTrueFalseTotal
if $notifycb and ref $notifycb ne "CODE" 0 0 0
Condition0%0/3
$notifycb and ref $notifycb ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$notifycb and ref $notifycb ne "CODE"
$notifycb ref $notifycb ne "CODE"
Truth table0%0/3
A and B
A= $notifycbB= ref $notifycb ne "CODE"
ABresult
0X0
100
111
534 if $notifycb and ref $notifycb ne 'CODE';
535  
536 #my ($done, $job_id2, $outargs);
537 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
538 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
539 0 my $steps = shift;
540 # fixme: check results?
541 0 ▶ $self->conn->call(
Branch: 1 branch
BranchTrueFalseTotal
$notifycb ? : 0 0 0
542 'get_job_status', {
543 job_id => $job_id,
544 notify => ($notifycb ? JSON->true : JSON->false),
545 }, $steps->next()
546 );
547 },
548 0 ▶ sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
549 #say 'call returned: ', Dumper(\@_);
550 0 my ($steps, $e, $r) = @_;
551 #$self->log->debug("get_job_satus_nb got job_id: $res msg: $msg");
552 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
553 0 $self->log->error("get_job_status got error $e->{message} ($e->{code})");
554 0 $e = { error => "$e->{message} ($e->{code})" };
555 0 ▶ $e = $self->{jsonobject}->encode($e) if $self->{json};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} 0 0 0
556 0 $statuscb->(undef, $e);
557 0 return;
558 }
559 0 my ($job_id2, $outargs) = @$r;
560 0 ▶ if ($notifycb and !$job_id2 and !$outargs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notifycb and not $job_id2 and not $outargs) 0 0 0
Condition0%0/6
$notifycb and not $job_id2 and not $outargs
!ll&&!rl&&r
000
$notifycb and not $job_id2
!ll&&!rl&&r
000
MC/DC0%0/3
$notifycb and not $job_id2 and not $outargs
$notifycb not $job_id2 not $outargs
Truth table0%0/4
A and not(B) and not(C)
A= $notifycbB= not $job_id2C= not $outargs
ABCresult
0XX0
10X0
1100
1111
561 0 $self->jobs->{$job_id} = $notifycb;
562 }
563 0 ▶ $outargs = $self->{jsonobject}->encode($outargs) if $self->{json} and ref $outargs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} and ref $outargs 0 0 0
Condition0%0/3
$self->{"json"} and ref $outargs
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"json"} and ref $outargs
$self->{"json"} ref $outargs
Truth table0%0/3
A and B
A= $self->{"json"}B= ref $outargs
ABresult
0X0
100
111
564 0 $statuscb->($job_id2, $outargs);
565 0 return;
566 0 ▶ }], sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
567 0 my ($err) = @_;
568 0 $self->log->error("Something went wrong in get_job_status_nb: $err");
569 0 $err = { error => $err };
570 0 ▶ $err = $self->{jsonobject}->encode($err) if $self->{json};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"json"} 0 0 0
571 0 $statuscb->(undef, $err);
572 0 });
573 }
574  
575 0 ▶sub ping {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
576 0 my ($self, $timeout) = @_;
577  
578 0 ▶ $timeout //= $self->timeout;
Condition0%0/3
$timeout //= $self->timeout
l!l&&r!l&&!r
000
MC/DC0%0/2
$timeout //= $self->timeout
$timeout $self->timeout
Truth table0%0/3
A //= B
A= $timeoutB= $self->timeout
ABresult
1X1
011
000
579 0 my ($done, $ret);
580  
581 0 ▶ $self->ioloop->timer($timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
582 0 $done++;
583 0 });
584  
585 0 ▶ $self->conn->call('ping', {}, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
586 0 my ($e, $r) = @_;
587 0 ▶ if (not $e and $r and $r =~ /pong/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $e and $r and $r =~ /pong/u) { } 0 0 0
Condition0%0/6
not $e and $r and $r =~ /pong/u
!ll&&!rl&&r
000
not $e and $r
!ll&&!rl&&r
000
MC/DC0%0/3
not $e and $r and $r =~ /pong/u
not $e $r $r =~ /pong/u
Truth table0%0/4
not(A) and B and C
A= not $eB= $rC= $r =~ /pong/u
ABCresult
0XX0
10X0
1100
1111
588 0 $ret = 1;
589 } else {
590 0 %$self = ();
591 }
592 0 $done++;
593 0 });
594  
595 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
596  
597 0 return $ret;
598 }
599  
600 0 ▶sub work {
Subroutine work: not called CC=5 SCAR=34.0
Pod documented
601 0 my ($self, $prepare) = @_;
602  
603 0 my $pt = $self->ping_timeout;
604 0 my $tmr;
605 0 ▶ $tmr = $self->ioloop->recurring($pt => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
606 0 my $ioloop = shift;
607 0 ▶ $self->log->debug('in ping_timeout timer: lastping: '
Condition0%0/2
$self->lastping // 0
l!l
00
MC/DC0%0/1
$self->lastping // 0
$self->lastping
Truth table0%0/2
A
A= $self->lastping
Aresult
11
00
608 . ($self->lastping // 0) . ' limit: ' . (time - $pt) );
609 0 ▶ return if ($self->lastping // 0) > time - $pt;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lastping // 0) > time - $pt 0 0 0
Condition0%0/2
$self->lastping // 0
l!l
00
MC/DC0%0/1
$self->lastping // 0
$self->lastping
Truth table0%0/2
A
A= $self->lastping
Aresult
11
00
610 0 $self->log->error('ping timeout');
611 0 $ioloop->remove($self->clientid);
612 0 $ioloop->remove($tmr);
613 0 $ioloop->{__exit__} = WORK_PING_TIMEOUT; # todo: doc
614 0 $ioloop->stop;
615 0 ▶ }) if $pt > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pt > 0 0 0 0
616 0 ▶ $self->on(disconnect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 my ($self, $code) = @_;
618 0 $self->ioloop->{__exit__} = $code;
619 0 $self->ioloop->stop;
620 0 });
621 0 ▶ return 0 if $prepare;
Branch: 1 branch
BranchTrueFalseTotal
if $prepare 0 0 0
622  
623 0 $self->ioloop->{__exit__} = WORK_OK;
624 0 $self->log->debug('JobCenter::Client::Mojo starting work');
625 0 ▶ $self->ioloop->start unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
626 0 $self->log->debug('JobCenter::Client::Mojo done?');
627 0 ▶ $self->ioloop->remove($tmr) if $tmr;
Branch: 1 branch
BranchTrueFalseTotal
if $tmr 0 0 0
628  
629 0 return $self->ioloop->{__exit__};
630 }
631  
632 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
633 0 my ($self, $exit) = @_;
634 0 $self->ioloop->{__exit__} = $exit;
635 0 $self->ioloop->stop;
636 }
637  
638 0 ▶sub create_slotgroup {
Subroutine create_slotgroup: not called CC=2 SCAR=17.9
Pod documented
639 0 my ($self, $name, $slots) = @_;
640 0 ▶ croak('no slotgroup name?') unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
641  
642 0 my ($done, $result);
643 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
644 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
645 0 my $steps = shift;
646 0 $self->conn->call('create_slotgroup', { name => $name, slots => $slots }, $steps->next());
647 },
648 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
649 #say 'call returned: ', Dumper(\@_);
650 0 my ($steps, $e, $r) = @_;
651 0 $done++; # received something so done waiting
652 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
653 0 $self->log->error("create_slotgroup got error $e->{message}");
654 0 $result = $e->{message};
655 0 return;
656 }
657 0 $result = $r;
658 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
659 0 my ($err) = @_;
660 0 $done++;
661 0 $self->log->eror("something went wrong with create_slotgroup: $err");
662 0 });
663  
664 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
665  
666 0 return $result;
667 }
668  
669 0 ▶sub announce {
Subroutine announce: not called CC=8 SCAR=42.8
Pod documented
670 0 my ($self, %args) = @_;
671 0 ▶ my $actionname = $args{actionname} or croak 'no actionname?';
Branch: 1 branch
BranchTrueFalseTotal
my $actionname = $args{"actionname"} or croak("no actionname?") 0 0 0
672 0 ▶ my $cb = $args{cb} or croak 'no cb?';
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $args{"cb"} or croak("no cb?") 0 0 0
673 #my $async = $args{async} // 0;
674 0 ▶ my $mode = $args{mode} // (($args{async}) ? 'async' : 'sync');
Branch: 1 branch
BranchTrueFalseTotal
$args{'async'} ? : 0 0 0
Condition0%0/2
$args{"mode"} // ($args{"async"} ? "async" : "sync")
l!l
00
MC/DC0%0/1
$args{"mode"} // ($args{"async"} ? "async" : "sync")
$args{"mode"}
Truth table0%0/2
A
A= $args{"mode"}
Aresult
11
00
675 0 ▶ croak "unknown callback mode $mode" unless $mode =~ /^(subproc|async|sync)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^(subproc|async|sync)$/u 0 0 0
676 0 my $undocb = $args{undocb};
677 0 my $host = hostname;
678 0 ▶ my $workername = $args{workername} // "$self->{who} $host $0 $$";
Condition0%0/2
$args{"workername"} // qq[$self->{"who"} $host $0 ${$}]
l!l
00
MC/DC0%0/1
$args{"workername"} // qq[$self->{"who"} $host $0 ${$}]
$args{"workername"}
Truth table0%0/2
A
A= $args{"workername"}
Aresult
11
00
679  
680 0 ▶ croak "already have action $actionname" if $self->actions->{$actionname};
Branch: 1 branch
BranchTrueFalseTotal
if $self->actions->{$actionname} 0 0 0
681  
682 0 my ($done, $err);
683 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
684 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
685 0 my $steps = shift;
686 $self->conn->call('announce', {
687 workername => $workername,
688 actionname => $actionname,
689 slotgroup => $args{slotgroup},
690 slots => $args{slots},
691 0 ▶ (($args{filter}) ? (filter => $args{filter}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args{'filter'} ? : 0 0 0
692 }, $steps->next());
693 },
694 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
695 #say 'call returned: ', Dumper(\@_);
696 0 my ($steps, $e, $r) = @_;
697 0 $done++; # received something so done waiting
698 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
699 0 $err = $e->{message};
700 0 $self->log->error("announce got error: $err");
701 0 return;
702 }
703 0 my ($res, $msg) = @$r;
704 0 $self->log->debug("announce got res: $res msg: $msg");
705 $self->actions->{$actionname} = {
706 cb => $cb,
707 mode => $mode,
708 undocb => $undocb,
709 0 ▶ addenv => $args{addenv} // 0,
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
Condition0%0/2
$args{"addenv"} // 0
l!l
00
MC/DC0%0/1
$args{"addenv"} // 0
$args{"addenv"}
Truth table0%0/2
A
A= $args{"addenv"}
Aresult
11
00
710 workername => $workername, # XXX required info to withdraw an action
711 } if $res;
712 0 ▶ $err = $msg unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
713 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
714 0 ($err) = @_;
715 0 $done++;
716 0 $self->log->error("something went wrong with announce: $err");
717 0 });
718  
719 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
720 0 return $err;
721 }
722  
723 0 ▶sub withdraw {
Subroutine withdraw: not called CC=4 SCAR=30.0
Pod documented
724 0 my ($self, %args) = @_;
725 0 ▶ my $actionname = $args{actionname} or croak 'no actionname?';
Branch: 1 branch
BranchTrueFalseTotal
my $actionname = $args{"actionname"} or croak("no actionname?") 0 0 0
726 0 my $log = $self->log;
727  
728 croak 'no action announced ' . $actionname unless
729 0 ▶ my $announce = $self->actions->{$actionname};
Branch: 1 branch
BranchTrueFalseTotal
unless my $announce = $self->actions->{$actionname} 0 0 0
730  
731 0 ▶ delete $self->actions->{$actionname} if $args{force}; # immediately stop accepting task_ready
Branch: 1 branch
BranchTrueFalseTotal
if $args{"force"} 0 0 0
732  
733 0 my ($done, $err);
734 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
735 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
736 0 my $steps = shift;
737 $self->conn->call('withdraw', {
738 workername => $announce->{workername},
739 0 actionname => $actionname,
740 }, $steps->next());
741 },
742 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
743 0 my ($steps, $e, $r) = @_;
744 0 $done++;
745 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
746 0 $err = $e->{message};
747 0 $log->error("withdraw got error: $err");
748 0 return;
749 }
750 0 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
751 0 $err = 'withdraw failed';
752 0 $log->error($err);
753 0 return;
754 }
755 0 delete $self->actions->{$actionname};
756 0 $log->debug("succesfully withdrew $actionname");
757 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
758 0 ($err) = @_;
759 0 $done++;
760 0 $log->error("something went wrong with withdraw: $err");
761 0 });
762  
763 0 ▶ $self->_loop(sub { !$done });
Subroutine __ANON__: not called CC=1 SCAR=6.9
764 0 return $err;
765 }
766  
767 0 ▶sub rpc_ping {
Subroutine rpc_ping: not called CC=1 SCAR=6.9
Pod undocumented
768 0 my ($self, $c, $i, $rpccb) = @_;
769 0 $self->lastping(time());
770 0 return 'pong!';
771 }
772  
773 0 ▶sub rpc_task_ready {
Subroutine rpc_task_ready: not called CC=2 SCAR=17.9
Pod undocumented
774 #say 'got task_ready: ', Dumper(\@_);
775 0 my ($self, $c, $i) = @_;
776 0 my $actionname = $i->{actionname};
777 0 my $job_id = $i->{job_id};
778 0 my $action = $self->actions->{$actionname};
779 0 ▶ unless ($action) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action) 0 0 0
780 0 $self->log->info("got task_ready for unknown action $actionname");
781 0 return;
782 }
783  
784 0 $self->log->debug("got task_ready for $actionname job_id $job_id calling get_task");
785 JobCenter::Client::Mojo::Steps->new(ioloop => $self->ioloop)->steps([
786 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
787 0 my $steps = shift;
788 0 $c->call('get_task', {actionname => $actionname, job_id => $job_id}, $steps->next());
789 },
790 0 ▶ sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
791 0 my ($steps, $e, $r) = @_;
792 #say 'get_task returned: ', Dumper(\@_);
793 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
794 0 $$self->log->debug("got $e->{message} ($e->{code}) calling get_task");
795 }
796 0 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
797 0 $self->log->debug('no task for get_task');
798 0 return;
799 }
800 0 my ($cookie, @args);
801 0 ($job_id, $cookie, @args) = @$r;
802 0 ▶ unless ($cookie) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cookie) 0 0 0
803 0 $self->log->debug('aaah? no cookie? (get_task)');
804 0 return;
805 }
806 0 ▶ pop @args unless $action->{addenv}; # remove env
Branch: 1 branch
BranchTrueFalseTotal
unless $action->{"addenv"} 0 0 0
807 0 local $@;
808 0 ▶ if ($action->{mode} eq 'subproc') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action->{'mode'} eq 'subproc') { } 0 0 0
elsif ($action->{'mode'} eq 'async') { } 0 0 0
elsif ($action->{'mode'} eq 'sync') { } 0 0 0
809 0 eval {
810 0 $self->_subproc($c, $action, $job_id, $cookie, @args);
811 };
812 0 ▶ $c->notify('task_done', { cookie => $cookie, outargs => { error => $@ } }) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
813 } elsif ($action->{mode} eq 'async') {
814 0 eval {
815 0 ▶ $action->{cb}->($job_id, @args, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
816 0 $c->notify('task_done', { cookie => $cookie, outargs => $_[0] });
817 0 });
818 };
819 0 ▶ $c->notify('task_done', { cookie => $cookie, outargs => { error => $@ } }) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
820 } elsif ($action->{mode} eq 'sync') {
821 0 my $outargs = eval { $action->{cb}->($job_id, @args) };
822 0 ▶ $outargs = { error => $@ } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
823 0 $c->notify('task_done', { cookie => $cookie, outargs => $outargs });
824 } else {
825 0 die "unkown mode $action->{mode}";
826 }
827 0 ▶ }], sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
828 0 my ($err) = @_;
829 0 $self->log->error("something went wrong with rpc_task_ready: $err");
830 0 });
831 }
832  
833 0 ▶sub _subproc {
Subroutine _subproc: not called CC=14 SCAR=53.5
834 0 my ($self, $c, $action, $job_id, $cookie, @args) = @_;
835  
836 # based on Mojo::IOLoop::Subprocess
837 0 my $ioloop = $self->ioloop;
838  
839 # Pipe for subprocess communication
840 0 ▶ pipe(my $reader, my $writer) or die "Can't create pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $reader, my $writer or die "Can't create pipe: $!" 0 0 0
841  
842 0 ▶ die "Can't fork: $!" unless defined(my $pid = fork);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $pid = fork) 0 0 0
843 0 ▶ unless ($pid) {# Child
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
844 0 $self->log->debug("in child $$");;
845 0 $ioloop->reset;
846 0 CORE::close $reader; # or we won't get a sigpipe when daddy dies..
847 0 my $undo = 0;
848 0 my $outargs = eval { $action->{cb}->($job_id, @args) };
849 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (ref $outargs eq 'HASH' and $outargs->{'error'}) { } 0 0 0
850 0 $outargs = {'error' => $@};
851 0 $undo++;
852 ▶ } elsif (ref $outargs eq 'HASH' and $outargs->{'error'}) {
Condition0%0/3
ref $outargs eq "HASH" and $outargs->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $outargs eq "HASH" and $outargs->{"error"}
ref $outargs eq "HASH" $outargs->{"error"}
Truth table0%0/3
A and B
A= ref $outargs eq "HASH"B= $outargs->{"error"}
ABresult
0X0
100
111
853 0 $undo++;
854 }
855 0 ▶ if ($undo and $action->{undocb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undo and $action->{"undocb"}) 0 0 0
Condition0%0/3
$undo and $action->{"undocb"}
!ll&&!rl&&r
000
MC/DC0%0/2
$undo and $action->{"undocb"}
$undo $action->{"undocb"}
Truth table0%0/3
A and B
A= $undoB= $action->{"undocb"}
ABresult
0X0
100
111
856 0 $self->log->info("undoing for $job_id");;
857 0 my $res = eval { $action->{undocb}->($job_id, @args); };
858 0 ▶ $res = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
859 # how should this look?
860 $outargs = {'error' => {
861 'msg' => 'undo failure',
862 'undo' => $res,
863 'olderr' => $outargs->{error},
864 0 }};
865 0 $undo = 0;
866 }
867 # stop ignoring sigpipe
868 0 ▶ $SIG{PIPE} = sub { $undo++ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
869 # if the parent is gone we get a sigpipe here:
870 0 print $writer Storable::freeze($outargs);
871 0 ▶ $writer->flush or $undo++;
Branch: 1 branch
BranchTrueFalseTotal
$writer->flush or ++$undo 0 0 0
872 0 ▶ CORE::close $writer or $undo++;
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $writer or ++$undo 0 0 0
873 0 ▶ if ($undo and $action->{undocb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undo and $action->{"undocb"}) 0 0 0
Condition0%0/3
$undo and $action->{"undocb"}
!ll&&!rl&&r
000
MC/DC0%0/2
$undo and $action->{"undocb"}
$undo $action->{"undocb"}
Truth table0%0/3
A and B
A= $undoB= $action->{"undocb"}
ABresult
0X0
100
111
874 0 $self->log->info("undoing for $job_id");;
875 0 eval { $action->{undocb}->($job_id, @args); };
876 # ignore errors because we can't report them back..
877 }
878 # FIXME: normal exit?
879 0 POSIX::_exit(0);
880 }
881  
882 # Parent
883 0 my $me = $$;
884 0 CORE::close $writer;
885 0 my $stream = Mojo::IOLoop::Stream->new($reader)->timeout(0);
886 0 $ioloop->stream($stream);
887 0 my $buffer = '';
888 0 ▶ $stream->on(read => sub { $buffer .= pop });
Subroutine __ANON__: not called CC=1 SCAR=6.9
889 $stream->on(
890 0 ▶ close => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
891 #say "close handler!";
892 0 ▶ return unless $$ == $me;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ == $me 0 0 0
893 0 waitpid $pid, 0;
894 0 my $outargs = eval { Storable::thaw($buffer) };
895 0 ▶ $outargs = { error => $@ } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
896 0 ▶ if ($outargs and ref $outargs eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($outargs and ref $outargs eq "HASH") 0 0 0
Condition0%0/3
$outargs and ref $outargs eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$outargs and ref $outargs eq "HASH"
$outargs ref $outargs eq "HASH"
Truth table0%0/3
A and B
A= $outargsB= ref $outargs eq "HASH"
ABresult
0X0
100
111
897 0 $self->log->debug('subprocess results: ' . Dumper($outargs));
898 0 eval { $c->notify(
899 'task_done',
900 { cookie => $cookie, outargs => $outargs }
901 ); }; # the connection might be gone?
902 } # else?
903 }
904 0 );
905 }
906  
907 # tick while Mojo::Reactor is still running and condition callback is true
908 0 ▶sub _loop {
Subroutine _loop: not called CC=10 SCAR=47.0
909 0 ▶ warn __PACKAGE__." recursing into IO loop" if state $looping++;
Branch: 1 branch
BranchTrueFalseTotal
if state $looping++ 0 0 0
910  
911 0 my $reactor = $_[0]->ioloop->singleton->reactor;
912 0 my $err;
913  
914 0 ▶ if (ref $reactor eq 'Mojo::Reactor::EV') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $reactor eq 'Mojo::Reactor::EV') { } 0 0 0
elsif (ref $reactor eq 'Mojo::Reactor::Poll') { } 0 0 0
915  
916 0 my $active = 1;
917  
918 0 ▶ $active = $reactor->one_tick while $_[1]->() && $active;
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->() and $active and do { $active = $reactor->one_tick } 0 0 0
Condition0%0/3
$_[1]->() and $active
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1]->() and $active
$_[1]->() $active
Truth table0%0/3
A and B
A= $_[1]->()B= $active
ABresult
0X0
100
111
919  
920 } elsif (ref $reactor eq 'Mojo::Reactor::Poll') {
921  
922 0 $reactor->{running}++;
923  
924 0 ▶ $reactor->one_tick while $_[1]->() && $reactor->is_running;
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->() and $reactor->is_running and do { $reactor->one_tick } 0 0 0
Condition0%0/3
$_[1]->() and $reactor->is_running
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1]->() and $reactor->is_running
$_[1]->() $reactor->is_running
Truth table0%0/3
A and B
A= $_[1]->()B= $reactor->is_running
ABresult
0X0
100
111
925  
926 0 ▶ $reactor->{running} &&= $reactor->{running} - 1;
Condition0%0/3
$reactor->{"running"} &&= $reactor->{"running"} - 1
!ll&&!rl&&r
000
MC/DC0%0/2
$reactor->{"running"} &&= $reactor->{"running"} - 1
$reactor->{"running"} $reactor->{"running"} - 1
Truth table0%0/3
A &&= B
A= $reactor->{"running"}B= $reactor->{"running"} - 1
ABresult
0X0
100
111
927  
928 } else {
929  
930 0 $err = "unknown reactor: ".ref $reactor;
931 }
932  
933 0 $looping--;
934 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
935 }
936  
937 #sub DESTROY {
938 # my $self = shift;
939 # say 'destroying ', $self;
940 #}
941  
942 1 1;
943  
944 =encoding utf8
945  
946 =head1 NAME
947  
948 JobCenter::Client::Mojo - JobCenter JSON-RPC 2.0 Api client using Mojo.
949  
950 =head1 SYNOPSIS
951  
952 use JobCenter::Client::Mojo;
953  
954 my $client = JobCenter::Client::Mojo->new(
955 address => ...
956 port => ...
957 who => ...
958 token => ...
959 );
960  
961 my ($job_id, $outargs) = $client->call(
962 wfname => 'test',
963 inargs => { test => 'test' },
964 );
965  
966 =head1 DESCRIPTION
967  
968 L<JobCenter::Client::Mojo> is a class to build a client to connect to the
969 JSON-RPC 2.0 Api of the L<JobCenter> workflow engine. The client can be
970 used to create and inspect jobs as well as for providing 'worker' services
971 to the JobCenter.
972  
973 =head1 METHODS
974  
975 =head2 new
976  
977 $client = JobCenter::Client::Mojo->new(%arguments);
978  
979 Class method that returns a new JobCenter::Client::Mojo object.
980  
981 Valid arguments are:
982  
983 =over 4
984  
985 =item - address: address of the Api.
986  
987 (default: 127.0.0.1)
988  
989 =item - port: port of the Api
990  
991 (default 6522)
992  
993 =item - tls: connect using tls
994  
995 (default false)
996  
997 =item - tls_ca: verify server using ca
998  
999 (default undef)
1000  
1001 =item - tls_key: private client key
1002  
1003 (default undef)
1004  
1005 =item - tls_ca: public client certificate
1006  
1007 (default undef)
1008  
1009 =item - who: who to authenticate as.
1010  
1011 (required)
1012  
1013 =item - method: how to authenticate.
1014  
1015 (default: password)
1016  
1017 =item - token: token to authenticate with.
1018  
1019 (required)
1020  
1021 =item - debug: when true prints debugging using L<Mojo::Log>
1022  
1023 (default: false)
1024  
1025 =item - ioloop: L<Mojo::IOLoop> object to use
1026  
1027 (per default the L<Mojo::IOLoop>->singleton object is used)
1028  
1029 =item - json: flag wether input is json or perl.
1030  
1031 when true expects the inargs to be valid json, when false a perl hashref is
1032 expected and json encoded. (default true)
1033  
1034 =item - jsonobject: json encoder/decoder object to use
1035  
1036 (per default a new L<JSON::MaybeXS> object is created)
1037  
1038 =item - log: L<Mojo::Log> object to use
1039  
1040 (per default a new L<Mojo::Log> object is created)
1041  
1042 =item - timeout: how long to wait for Api calls to complete
1043  
1044 (default 60 seconds)
1045  
1046 =item - ping_timeout: after this long without a ping from the Api the
1047 connection will be closed and the work() method will return
1048  
1049 (default 5 minutes)
1050  
1051 =back
1052  
1053 =head2 connect_nb
1054  
1055 $client->connect_nb(\%options, sub { ($success, $message) = @_; ... });
1056  
1057 Starts connection handshake and call the provided callback on connection or
1058 failure to connect. Throws an error if somethings goes wrong immediately,
1059 otherwise the callback is given the result.
1060  
1061 C<$success> is a true/false indicator of the result. If set C<$message> is a
1062 string describing the outcome.
1063  
1064 =head2 call
1065  
1066 ($job_id, $result) = $client->call(%args);
1067  
1068 Creates a new L<JobCenter> job and waits for the results. Throws an error
1069 if somethings goes wrong immediately. Errors encountered during later
1070 processing are returned as a L<JobCenter> error object.
1071  
1072 Valid arguments are:
1073  
1074 =over 4
1075  
1076 =item - wfname: name of the workflow to call (required)
1077  
1078 =item - inargs: input arguments for the workflow (if any)
1079  
1080 =item - vtag: version tag of the workflow to use (optional)
1081  
1082 =item - timeout: wait this many seconds for the job to finish
1083 (optional, defaults to 5 times the Api-call timeout, so default 5 minutes)
1084  
1085 =item - reqauth: authentication token to be passed on to the authentication
1086 module of the API for per job/request authentication.
1087  
1088 =item - clenv: client environment, made available as part of the job
1089 environment and inherited to child jobs.
1090  
1091 =back
1092  
1093 =head2 call_nb
1094  
1095 $job_id = $client->call_nb(%args);
1096  
1097 Creates a new L<JobCenter> job and call the provided callback on completion
1098 of the job. Throws an error if somethings goes wrong immediately. Errors
1099 encountered during later processing are returned as a L<JobCenter> error
1100 object to the callback.
1101  
1102 Valid arguments are those for L<call> and:
1103  
1104 =over 4
1105  
1106 =item - cb1: coderef to the callback to call on job creation (requird)
1107  
1108 ( cb1 => sub { ($job_id, $err) = @_; ... } )
1109  
1110 If job_id is undefined the job was not created, the error is then returned
1111 as the second return value.
1112  
1113 =item - cb2: coderef to the callback to call on job completion (requird)
1114  
1115 ( cb2 => sub { ($job_id, $outargs) = @_; ... } )
1116  
1117 =back
1118  
1119 =head2 get_job_status
1120  
1121 ($job_id, $result) = $client->get_job_status($job_id);
1122  
1123 Retrieves the status for the given $job_id. If the job_id does not exist
1124 then the returned $job_id will be undefined and $result will be an error
1125 message. If the job has not finished executing then both $job_id and
1126 $result will be undefined. Otherwise the $result will contain the result of
1127 the job. (Which may be a JobCenter error object)
1128  
1129 =head2 get_job_status_nb
1130  
1131 $client->get_job_status_nb(%args);
1132  
1133 Retrieves the status for the given $job_id.
1134  
1135 Valid arguments are:
1136  
1137 =over 4
1138  
1139 =item - job_id
1140  
1141 =item - statuscb: coderef to the callback for the current status
1142  
1143 ( statuscb => sub { ($job_id, $result) = @_; ... } )
1144  
1145 If the job_id does not exist then the returned $job_id will be undefined
1146 and $result will be an error message. If the job has not finished executing
1147 then both $job_id and $result will be undefined. Otherwise the $result will
1148 contain the result of the job. (Which may be a JobCenter error object)
1149  
1150 =item - notifycb: coderef to the callback for job completion
1151  
1152 ( statuscb => sub { ($job_id, $result) = @_; ... } )
1153  
1154 If the job was still running when the get_job_status_nb call was made then
1155 this callback will be called on completion of the job.
1156  
1157 =back
1158  
1159 =head2 check_if_lock_exists
1160  
1161 $found = $client->check_if_lock_exists($locktype, $lockvalue);
1162  
1163 Checks if a lock with the given locktype and lockvalue exists. (I.e. a job
1164 is currently running that holds that lock.
1165  
1166 Returns true if the lock exists, null if the locktype does not exist, false
1167 otherwise.
1168  
1169 =head2 find_jobs
1170  
1171 ($err, @jobs) = $client->find_jobs({'foo'=>'bar'});
1172  
1173 Finds all currently running jobs with arguments matching the filter
1174 expression. The expression is evaluated in PostgreSQL using the @> for
1175 jsonb objects, basically this means that you can only do equality tests for
1176 one or more top-level keys. If @jobs is empty $err might contain an error
1177 message.
1178  
1179 =head2 ping
1180  
1181 $status = $client->ping($timeout);
1182  
1183 Tries to ping the JobCenter API. On success return true. On failure returns
1184 the undefined value, after that the client object should be undefined.
1185  
1186 =head2 close
1187  
1188 $client->close()
1189  
1190 Closes the connection to the JobCenter API and tries to de-allocate
1191 everything. Trying to use the client afterwards will produce errors.
1192  
1193 =head2 create_slotgroup
1194  
1195 $client->create_slotgroup($name, $slots)
1196  
1197 A 'slotgroup' is a way of telling the JobCenter API how many taskss the
1198 worker can do at once. The number of slots should be a positive integer.
1199 Slotgroups names starting with a _ (underscore) are reserved for internal
1200 use. Returns a error message when then was an error, the empty string
1201 otherwise.
1202  
1203 =head2 announce
1204  
1205 Announces the capability to do an action to the Api. The provided callback
1206 will be called when there is a task to be performed. Returns an error when
1207 there was a problem announcing the action.
1208  
1209 my $err = $client->announce(
1210 workername => 'me',
1211 actionname => 'do',
1212 slots => 1
1213 cb => sub { ... },
1214 );
1215 die "could not announce $actionname?: $err" if $err;
1216  
1217 See L<jcworker> for an example.
1218  
1219 Valid arguments are:
1220  
1221 =over 4
1222  
1223 =item - workername: name of the worker
1224  
1225 (optional, defaults to client->who, processname and processid)
1226  
1227 =item - actionname: name of the action
1228  
1229 (required)
1230  
1231 =item - cb: callback to be called for the action
1232  
1233 (required)
1234  
1235 =item - mode: callback mode
1236  
1237 (optional, default 'sync')
1238  
1239 Possible values:
1240  
1241 =over 8
1242  
1243 =item - 'sync': simple blocking mode, just return the results from the
1244 callback. Use only for callbacks taking less than (about) a second.
1245  
1246 =item - 'subproc': the simple blocking callback is started in a seperate
1247 process. Useful for callbacks that take a long time.
1248  
1249 =item - 'async': the callback gets passed another callback as the last
1250 argument that is to be called on completion of the task. For advanced use
1251 cases where the worker is actually more like a proxy. The (initial)
1252 callback is expected to return soonish to the event loop, after setting up
1253 some Mojo-callbacks.
1254  
1255 =back
1256  
1257 =item - async: backwards compatible way for specifying mode 'async'
1258  
1259 (optional, default false)
1260  
1261 =item - slotgroup: the slotgroup to use for accounting of parrallel tasks
1262  
1263 (optional, conflicts with 'slots')
1264  
1265 =item - slots: the amount of tasks the worker is able to process in parallel
1266 for this action.
1267  
1268 (optional, default 1, conflicts with 'slotgroup')
1269  
1270 =item - undocb: a callback that gets called when the original callback
1271 returns an error object or throws an error.
1272  
1273 Called with the same arguments as the original callback.
1274  
1275 (optional, only valid for mode 'subproc')
1276  
1277 =item - filter: only process a subset of the action
1278  
1279 The filter expression allows a worker to specify that it can only do the
1280 actionname for a certain subset of arguments. For example, for a "mkdir"
1281 action the filter expression {'host' => 'example.com'} would mean that this
1282 worker can only do mkdir on host example.com. Filter expressions are limited
1283 to simple equality tests on one or more keys, and only those keys that are
1284 allowed in the action definition. Filtering can be allowed, be mandatory or
1285 be forbidden per action.
1286  
1287 =item - addenv: pass on action enviroment to the callback
1288  
1289 If the addenv flag is true the action callback will be given one extra
1290 argument containing the action environment as a hashref. In the async
1291 callback mode the environment will be inserted before the result callback.
1292  
1293 =back
1294  
1295 =head2 withdraw
1296  
1297 Withdraws the capability to do an action to the Api.
1298  
1299 Valid arguments are:
1300  
1301 =over 4
1302  
1303 =item - actionname: name of the action
1304  
1305 (required)
1306  
1307 =back
1308  
1309 =head2 work
1310  
1311 Starts the L<Mojo::IOLoop>. Returns a non-zero value when the IOLoop was
1312 stopped due to some error condition (like a lost connection or a ping
1313 timeout).
1314  
1315 =head3 Possible work() exit codes
1316  
1317 The JobCenter::Client::Mojo library currently defines the following exit codes:
1318  
1319 WORK_OK
1320 WORK_PING_TIMEOUT
1321 WORK_CONNECTION_CLOSED
1322  
1323 =head2 stop
1324  
1325 $client->stop($exit);
1326  
1327 Makes the work() function exit with the provided exit code.
1328  
1329 =head1 SEE ALSO
1330  
1331 =over 4
1332  
1333 =item *
1334  
1335 L<Mojo::IOLoop>, L<Mojo::IOLoop::Stream>, L<http://mojolicious.org>: the L<Mojolicious> Web framework
1336  
1337 =item *
1338  
1339 L<jcclient|https://github.com/a6502/JobCenter-Client-Mojo/blob/master/examples/jcclient>, L<jcworker|https://github.com/a6502/JobCenter-Client-Mojo/blob/master/examples/jcworker>
1340  
1341 =back
1342  
1343 L<https://github.com/a6502/JobCenter>: JobCenter Orchestration Engine
1344  
1345 =head1 ACKNOWLEDGEMENT
1346  
1347 This software has been developed with support from L<STRATO|https://www.strato.com/>.
1348 In German: Diese Software wurde mit Unterstützung von L<STRATO|https://www.strato.de/> entwickelt.
1349  
1350 =head1 THANKS
1351  
1352 Thanks to Eitan Schuler for reporting a bug and providing a pull request.
1353  
1354 =head1 AUTHORS
1355  
1356 =over 4
1357  
1358 =item *
1359  
1360 Wieger Opmeer <wiegerop@cpan.org>
1361  
1362 =back
1363  
1364 =head1 COPYRIGHT AND LICENSE
1365  
1366 This software is copyright (C) 2017-2023 by Wieger Opmeer.
1367  
1368 This is free software; you can redistribute it and/or modify it under
1369 the same terms as the Perl 5 programming language system itself.
1370  
1371 =cut
1372  
1373 1 1;
↑ Summary Mojo/Steps.pm »