TaskPipe/Task.pm

filter: Statementstmt 29.3% 156 / 531 Branchbran 8.7% 13 / 148 Conditioncond 18.4% 7 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 52.0% 39 / 75 Podpod 0.0% 0 / 37 totaltotal 25.2% 215 / 852 CC 96 SCAR 33.3
CC
96
Cov
25%
test_data
110.0
execute_as_task
90.0
cache_xresults
76.0
CRAP 28.1
« TaskPipe/Task_Record_Array.pm ↑ Summary TaskPipe/InterpParam/MatchHandler.pm »
1 package TaskPipe::Task;
2  
3 38 our $VERSION = 0.08;
4  
5 38 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 38 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 38 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 38 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 38 ▶use TaskPipe::Task::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 38 ▶use TaskPipe::Task::ModuleMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 38 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 38 ▶use TaskPipe::InterpParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 38 ▶use TaskPipe::LoggerManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 38 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 38 ▶use TaskPipe::ThreadManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 38 ▶use TaskPipe::Iterator_Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 38 ▶use TaskPipe::RunInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 38 ▶use TaskPipe::TaskUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 38 ▶use TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 38 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 38 with 'MooseX::ConfigCascade';
27  
28 has sm => (is => 'rw', isa => 'TaskPipe::SchemaManager', default => sub{
29 0 ▶ my $sm = TaskPipe::SchemaManager->new( scope => 'project' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 $sm->connect_schema;
31 0 return $sm;
32 38 });
33 has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager', default => sub{
34 0 ▶ my $gm = TaskPipe::SchemaManager->new( scope => 'global' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 $gm->connect_schema;
36 0 return $gm;
37 38 });
38  
39 has run_info => (is => 'ro', isa => 'TaskPipe::RunInfo', default => sub{
40 305 ▶ TaskPipe::RunInfo->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
41 38 });
42  
43 306 ▶has settings => (is => 'ro', isa => 'TaskPipe::Task::Settings', default => sub{ TaskPipe::Task::Settings->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
44  
45 has utils => (is => 'ro', isa => 'TaskPipe::TaskUtils', lazy => 1, default => sub{
46 301 ▶ TaskPipe::TaskUtils->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
47 sm => $_[0]->sm,
48 gm => $_[0]->gm
49 );
50 38 });
51  
52  
53 38 has plan => (is => 'rw', isa => 'ArrayRef|HashRef');
54 38 has plan_dd => (is => 'rw', isa => 'Str');
55 38 has plan_md5 => (is => 'rw', isa => 'Str');
56 38 has plan_key => (is => 'rw', isa => 'Str');
57  
58 306 ▶has input => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
59 38 has input_dd => (is => 'rw', isa => 'Str');
60 38 has input_md5 => (is => 'rw', isa => 'Str');
61 38 has input_id => (is => 'rw', isa => 'Str');
62 38 has input_key => (is => 'rw', isa => 'Str');
63  
64 306 ▶has input_history => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 306 ▶has param => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
67 38 has param_dd => (is => 'rw', isa => 'Str');
68 #has param_md5 => (is => 'rw', isa => 'Str');
69 306 ▶has param_history => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
70  
71 38 has branch_id => (is => 'rw', isa => 'Str', default => '1-0' );
72 306 ▶has xbranch_ids => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
73  
74 306 ▶has pinterp => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
75 38 has pinterp_dd => (is => 'rw', isa => 'Str');
76 38 has pinterp_md5 => (is => 'rw', isa => 'Str');
77  
78  
79 38 has results => (is => 'rw', isa => 'ArrayRef');
80 38 has results_pointer => (is => 'rw', isa => 'Int', default => 0);
81  
82 has thread_manager => (is => 'rw', isa => 'TaskPipe::ThreadManager', lazy => 1, default => sub{
83 301 ▶ TaskPipe::ThreadManager->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
84 gm => $_[0]->gm,
85 sm => $_[0]->sm,
86 utils => $_[0]->utils,
87 logger_manager => $_[0]->logger_manager
88 );
89 38 });
90  
91  
92 has logger_manager => (is => 'ro', isa => 'TaskPipe::LoggerManager', default => sub{
93 306 ▶ TaskPipe::LoggerManager->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
94 38 });
95  
96  
97  
98 ####### Attributes related to testing #############
99 38 has test_label => (
100 is => 'ro',
101 isa => 'Str'
102 );
103  
104 has test_pinterp => ( # override to make
105 is => 'ro', # task testable
106 isa => 'ArrayRef[HashRef]', #
107 306 ▶ default => sub{[{}]} #
Subroutine __ANON__: called CC=1 SCAR=0.0
108 38 );
109 has test_settings => (
110 is => 'ro',
111 isa => __PACKAGE__.'::TestSettings',
112 lazy => 1,
113 default => sub {
114 0 ▶ my $module = __PACKAGE__.'::TestSettings';
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 require_module( $module );
116 0 $module->new;
117 }
118 38 );
119 ###################################################
120  
121  
122 sub name{
123 0 ▶ my ($n) = ref($_[0]) =~ /^${\__PACKAGE__}_(\w+)$/; $n; }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
124  
125  
126 sub test{
127 0 ▶ my ($self) = @_;
Subroutine test: not called CC=7 SCAR=40.3
Pod undocumented
128  
129 0 my $logger = Log::Log4perl->get_logger;
130  
131 0 my $output = "";
132  
133 0 foreach my $test_pinterp ( @{$self->test_data} ){
134  
135 0 $self->pinterp( $test_pinterp );
136 0 my $results = $self->action;
137 0 ▶ $self->results( $results ) if $results;
Branch: 1 branch
BranchTrueFalseTotal
if $results 0 0 0
138  
139 0 ▶ $output.= "\n\n" if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 0 0
140 0 $output.= "\n";
141 0 $output.= "=" x 60;
142 0 $output.= "\nTesting ".ref($self).":\n\n";
143 0 $output.= "Test Pinterp: ".Dumper( $test_pinterp )."\n\n";
144  
145 0 my $max = $self->settings->test_result_limit;
146  
147 0 for my $i (1..$max){
148 0 my $result = $self->next_result;
149 0 ▶ last unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
150 0 $output .= "RESULT $i:\n";
151 0 $output .= Dumper( $result )."\n\n";
152 }
153  
154 }
155 0 ▶ $output.= $self->add_test_output if $self->can('add_test_output');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("add_test_output") 0 0 0
156 0 $output.= "=" x 60;
157  
158 0 $logger->info( $output );
159  
160 }
161  
162  
163  
164  
165  
166  
167 sub test_data{
168 0 ▶ my ($self) = @_;
Subroutine test_data: not called CC=10 SCAR=47.0
Pod undocumented
169  
170 0 my $logger = Log::Log4perl->get_logger;
171 0 my $pinterp = $self->test_pinterp;
172
173 0 ▶ if ( ! $pinterp ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($pinterp) 0 0 0
174 0 $logger->warn("No test_pinterp defined. Assuming this task does not need it");
175 0 return [{}];
176 }
177  
178 0 ▶ if ( ! @$pinterp ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@$pinterp) 0 0 0
179 0 $logger->warn("test_pinterp defined but empty. Attempting to proceed without test data");
180 0 return [{}]
181 }
182  
183 0 ▶ if ( ref $pinterp eq ref [] ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pinterp eq ref []) { } 0 0 0
elsif (ref $pinterp eq ref {}) { } 0 0 0
184 0 ▶ if ( $self->test_label ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->test_label) { } 0 0 0
185 0 ▶ confess "test data is labelled by numeric index, but label supplied (".$self->test_label.") was not numeric" unless $self->test_label =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->test_label =~ /^\d+$/ 0 0 0
186 0 my $di = $pinterp->[ $self->test_label ];
187 0 ▶ confess "No data found at test index ".$self->test_label unless defined $di;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $di 0 0 0
188 0 return [ $di ];
189 } else {
190 0 return $pinterp;
191 }
192 } elsif( ref $pinterp eq ref {} ){
193 0 ▶ if ( $self->test_label ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->test_label) { } 0 0 0
194 0 my $di = $pinterp->{ $self->test_label };
195 0 ▶ confess "Could not find a test named ".$self->test_label unless defined $di;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $di 0 0 0
196 0 return [ $di ];
197 } else {
198 0 return +values %$pinterp;
199 }
200 }
201 }
202  
203  
204 sub execute{
205 301 ▶ my ($self) = @_;
Subroutine execute: called CC=2 SCAR=7.6
Pod undocumented
206  
207 301 ▶ if ( $self->is_manager ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_manager) { } 301 0 301
208  
209 301 $self->execute_as_manager;
210  
211 } else {
212  
213 0 $self->execute_as_task;
214  
215 }
216  
217 }
218  
219  
220  
221 sub set_input_md5{
222 0 ▶ my ($self) = @_;
Subroutine set_input_md5: not called CC=1 SCAR=6.9
Pod undocumented
223  
224 0 my $full = [ $self->input, @{$self->input_history} ];
225 0 my $serialized = $self->utils->serialize( $full );
226 0 $self->input_dd( $serialized );
227 0 $self->input_md5( md5_base64( encode("utf8",$serialized) ) );
228  
229 }
230  
231  
232 sub set_param_md5{
233 0 ▶ my ($self) = @_;
Subroutine set_param_md5: not called CC=1 SCAR=6.9
Pod undocumented
234  
235 0 my $full = [ $self->param, @{$self->param_history} ];
236 0 my $serialized = $self->utils->serialize( $full );
237 0 $self->param_dd( $serialized );
238 }
239  
240  
241 sub set_plan_md5{
242 0 ▶ my ($self) = @_;
Subroutine set_plan_md5: not called CC=1 SCAR=6.9
Pod undocumented
243  
244 0 my $serialized = $self->utils->serialize( $self->plan );
245 0 $self->plan_dd( $serialized );
246 0 $self->plan_md5( md5_base64( encode("utf8",$serialized) ) );
247 }
248
249  
250  
251 sub seen_xbranch{
252 0 ▶ my ($self) = @_;
Subroutine seen_xbranch: not called CC=7 SCAR=40.3
Pod undocumented
253  
254 0 my $logger = Log::Log4perl->get_logger;
255  
256 0 my $parent_xbranch_id = undef;
257 0 ▶ $parent_xbranch_id = $self->xbranch_ids->[0] if defined $self->xbranch_ids->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->xbranch_ids->[0] 0 0 0
258  
259 0 my $xbranch;
260  
261 my $input_key;
262 0 my $plan_key;
263 0 ▶ if ( $self->settings->xbranch_key_mode eq 'md5' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->xbranch_key_mode eq 'md5') { } 0 0 0
264 0 $self->set_input_md5;
265 0 $self->set_plan_md5;
266 0 $input_key = $self->input_md5;
267 0 $plan_key = $self->plan_md5;
268 } else {
269 0 $input_key = $self->input_id,
270 $plan_key = $self->branch_id
271 }
272
273 0 $logger->trace("Recording plan_key $plan_key input_key $input_key");
274  
275 #$self->show_xbranches("before find_or_create");
276  
277  
278 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$@ and do { do { eval { do { $xbranch = $self->sm->table("xbranch")->find_or_create({"plan_key", $plan_key, "input_key", $input_key, "branch_id", $self->branch_id, "input_id", $self->input_id, "parent_id", $parent_xbranch_id, "plan_dd", $self->plan_dd, "input_dd", $self->input_dd, "param_dd", $self->param_dd, "thread_id", $self->run_info->thread_id}, {"key", "plan_key"}) } } } } 0 0 0
279 0 eval{
280  
281 0 $xbranch = $self->sm->table('xbranch')->find_or_create({
282 plan_key => $plan_key,
283 input_key => $input_key,
284 #param_md5 => $self->param_md5,
285 branch_id => $self->branch_id,
286 input_id => $self->input_id,
287 parent_id => $parent_xbranch_id,
288 plan_dd => $self->plan_dd,
289 input_dd => $self->input_dd,
290 param_dd => $self->param_dd,
291 thread_id => $self->run_info->thread_id
292 },
293 {key => 'plan_key'});
294 };
295  
296 # if ($@ && $@ =~ /duplicate\sentry/i){
297  
298 # die "duplicate entry: $@";
299  
300 # }
301  
302 } while ($@);
303 # } while ($@ && $@ =~ /duplicate\sentry/i);
304  
305 0 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
306  
307 0 unshift @{$self->xbranch_ids}, $xbranch->id;
308  
309 0 my $seen = 0;
310  
311 0 ▶ if ( $xbranch->status && $xbranch->status eq 'seen' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($xbranch->status and $xbranch->status eq "seen") 0 0 0
Condition0%0/3
$xbranch->status and $xbranch->status eq "seen"
!ll&&!rl&&r
000
MC/DC0%0/2
$xbranch->status and $xbranch->status eq "seen"
$xbranch->status $xbranch->status eq "seen"
Truth table0%0/3
A and B
A= $xbranch->statusB= $xbranch->status eq "seen"
ABresult
0X0
100
111
312 0 $seen = 1;
313 }
314  
315 0 return $seen;
316 }
317  
318  
319 ############################# TEST ######################
320 sub show_xbranches{
321 0 ▶ my ($self,$msg) = @_;
Subroutine show_xbranches: not called CC=2 SCAR=17.9
Pod undocumented
322 0 my $logger = Log::Log4perl->get_logger;
323  
324  
325 0 my $xbrancheschk = $self->sm->table('xbranch')->search({});
326 0 my $chk = '';
327 0 ▶ while( my $xbranchchk = $xbrancheschk->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $xbranchchk = $xbrancheschk->next) 0 0 0
328 0 $chk.=$self->utils->serialize( { $xbranchchk->get_columns })."\n";
329 }
330 0 $logger->trace("xbranches $msg:\n$chk");
331  
332 }
333 #########################################################
334  
335  
336  
337 sub clear_xbranch_error{
338 0 ▶ my ($self) = @_;
Subroutine clear_xbranch_error: not called CC=1 SCAR=6.9
Pod undocumented
339 0 $self->sm->table('xbranch_error')->search({
340 xbranch_id => $self->xbranch_ids->[0]
341 })->delete_all;
342  
343 }
344  
345  
346  
347  
348 sub execute_as_task{
349 0 ▶ my ($self) = @_;
Subroutine execute_as_task: not called CC=9 SCAR=45.0
Pod undocumented
350  
351 0 $self->run_info->task_name( $self->name );
352 0 $self->run_info->task_details('');
353  
354 0 $self->logger_manager->init_logger;
355 0 my $logger = Log::Log4perl->get_logger;
356  
357 0 $logger->trace("Starting execute_as_task subroutine");
358 0 $logger->info("Starting task");
359  
360 # if ( $self->seen_xbranch ){
361 # $logger->info("Already seen xbranch ".$self->xbranch_ids->[0]." - skipping");
362 # return;
363 # }
364  
365 # $logger->trace("After seen_xbranch check");
366  
367 0 $self->clear_xbranch_error;
368  
369 0 my %to_interp = %{$self->param};
370 0 foreach my $key (keys %to_interp){
371 0 ▶ delete $to_interp{$key} if $key =~ /^\_/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^\_/ 0 0 0
372 }
373
374  
375 0 my $interp = TaskPipe::InterpParam->new(
376 input => $self->input,
377 input_history => $self->input_history,
378 param => $self->param,
379 param_history => $self->param_history
380 );
381  
382 0 $logger->trace("After interp");
383  
384 0 $self->pinterp( $interp->interp );
385  
386 # my $results;
387 # my $from_cache = 0;
388 # my $should_cache = $self->settings->cache_results;
389 #
390 # if ( $should_cache ){
391 # $results = $self->get_cached_results;
392 # $from_cache = 1;
393 # }
394 #my $results;
395 0 my $results = $self->get_cached_xresults;
396 0 $logger->trace("Before action");
397  
398 0 $logger->debug("Plan: ".Dumper( $self->plan ));
399  
400 0 my $from_cache = 0;
401 0 ▶ if ( $results ){
Branch: 1 branch
BranchTrueFalseTotal
if ($results) { } 0 0 0
402 0 $logger->debug("Results (from cache): ".Dumper( $results ));
403 0 $from_cache = 1;
404 } else {
405 0 $results = $self->action;
406 0 $logger->debug("Results (from actioning): ".Dumper( $results ));
407 }
408  
409 0 ▶ $self->results( $results ) if $results;
Branch: 1 branch
BranchTrueFalseTotal
if $results 0 0 0
410  
411 #$self->cache_results if $should_cache && ! $from_cache && $self->results;
412 0 ▶ $self->cache_xresults if $results && ! $from_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $results and not $from_cache 0 0 0
Condition0%0/3
$results and not $from_cache
!ll&&!rl&&r
000
MC/DC0%0/2
$results and not $from_cache
$results not $from_cache
Truth table0%0/3
A and not(B)
A= $resultsB= not $from_cache
ABresult
0X0
100
111
413  
414 #$logger->debug("After cache_results");
415  
416 0 my $to_pipe = $self->get_plan_to_pipe;
417 0 ▶ $self->pipe_to( $to_pipe ) if $to_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $to_pipe 0 0 0
418  
419  
420 0 ▶ if ( $self->xbranch_has_error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xbranch_has_error) 0 0 0
421 0 $self->set_xbranch_status( 'errors were encountered on xbranch' );
422 } #else {
423 # $self->mark_seen; # if ! $from_cache;
424 # }
425  
426 #$self->show_xbranches("at end of task");
427 0 $logger->info("Ending task");
428  
429 0 Log::Log4perl->remove_logger( $logger );
430  
431 }
432  
433  
434 sub get_cached_results{
435 0 ▶ my ($self) = @_;
Subroutine get_cached_results: not called CC=4 SCAR=30.0
Pod undocumented
436  
437 0 my $logger = Log::Log4perl->get_logger;
438  
439 0 my $serialized = $self->utils->serialize( $self->pinterp );
440 0 $self->pinterp_dd( $serialized );
441 0 $self->pinterp_md5( md5_base64( encode("utf8",$serialized ) ) );
442  
443 0 my $cached = $self->sm->table('pinterp')->search({
444 task_name => $self->name,
445 pinterp_md5 => $self->pinterp_md5
446 })->first;
447
448 0 my $results;
449 0 ▶ if ( $cached ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) { } 0 0 0
450
451 0 my $results_rs = $self->sm->table('result')->search({
452 pinterp_id => $cached->id
453 });
454  
455 0 ▶ if ( $results_rs ){
Branch: 1 branch
BranchTrueFalseTotal
if ($results_rs) 0 0 0
456 0 $results = [];
457 0 ▶ while( my $serialized = $results_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $serialized = $results_rs->next) 0 0 0
458 0 push @$results, +$self->utils->deserialize( $serialized );
459 }
460 0 $logger->debug("Got cached results - pinterp id ".$cached->id);
461 }
462 } else {
463 0 $logger->debug("Pinterp not cached");
464 }
465  
466 0 return $results;
467 }
468  
469
470  
471 sub cache_results{
472 #my ($self,$results) = @_;
473 0 ▶ my ($self) = @_;
Subroutine cache_results: not called CC=2 SCAR=17.9
Pod undocumented
474  
475 0 my $results = $self->all_results;
476  
477 0 my $serialized = $self->utils->serialize( $self->pinterp );
478 0 $self->pinterp_dd( $serialized );
479 0 $self->pinterp_md5( md5_base64( encode("utf8",$serialized ) ) );
480  
481  
482 0 my $guard = $self->sm->schema->txn_scope_guard;
483 0 my $pinterp = $self->sm->table('pinterp')->create({
484 task_name => $self->name,
485 pinterp_md5 => $self->pinterp_md5,
486 pinterp_dd => $self->pinterp_dd
487 });
488  
489 0 foreach my $result (@$results){
490
491 0 $self->sm->table('result')->create({
492 pinterp_id => $pinterp->id,
493 result => +$self->utils->serialize( $result )
494 });
495 }
496 0 $guard->commit;
497 }
498  
499  
500  
501 sub cache_xresults{
502 0 ▶ my ($self) = @_;
Subroutine cache_xresults: not called CC=9 SCAR=43.3
Pod undocumented
503  
504 0 ▶ if ( @{$self->results} ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->results;}) 0 0 0
505 38 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=4.1
506  
507 0 $self->sm->table('xresult')->create({
508 xbranch_id => +$self->xbranch_ids->[0],
509 thread_id => +$self->run_info->thread_id,
510 result => +$self->utils->serialize( $self->results->[0] )
511 });
512  
513 38 ▶ } catch ( DBIx::Error $err where { $_->state =~ /^23/ }){
Subroutine BEGIN: called CC=1 SCAR=6.0
Branch: 3 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 0 0 0
if ('TryCatch'->check_tc('DBIx::Error') and do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }) { } 0 0 0
elsif ('TryCatch'->check_tc('DBIx::Error') and do { local $_ = $TryCatch::Error; not $_->state =~ /^23/ }) { } 0 0 0
Condition0%0/3
"TryCatch"->check_tc("DBIx::Error") and do {
!ll&&!rl&&r
000
MC/DC0%0/2
"TryCatch"->check_tc("DBIx::Error") and do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }
"TryCatch"->check_tc("DBIx::Error") do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }
Truth table0%0/3
A and B
A= "TryCatch"->check_tc("DBIx::Error")B= do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }
ABresult
0X0
100
111
514  
515 0 my $parent_xbranch_id = undef;
516 0 ▶ $parent_xbranch_id = $self->xbranch_ids->[1] if @{$self->xbranch_ids} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->xbranch_ids;} > 1 0 0 0
517  
518 0 $self->sm->table('xbranch')->create({
519 id => +$self->xbranch_ids->[0],
520 plan_key => +$self->plan_key,
521 input_key => +$self->input_key,
522 thread_id => +$self->run_info->thread_id,
523 branch_id => +$self->branch_id,
524 input_id => +$self->input_id,
525 parent_id => $parent_xbranch_id,
526 status => undef
527 });
528  
529 0 $self->sm->table('xresult')->create({
530 xbranch_id => +$self->xbranch_ids->[0],
531 thread_id => +$self->run_info->thread_id,
532 result => +$self->utils->serialize( $self->results->[0] )
533 });
534  
535 38 ▶ } catch ( DBIx::Error $err where { $_->state !~ /^23/ } ){
Subroutine BEGIN: called CC=1 SCAR=4.9
Condition0%0/3
"TryCatch"->check_tc("DBIx::Error") and do {
!ll&&!rl&&r
000
MC/DC0%0/2
"TryCatch"->check_tc("DBIx::Error") and do { local $_ = $TryCatch::Error; not $_->state =~ /^23/ }
"TryCatch"->check_tc("DBIx::Error") do { local $_ = $TryCatch::Error; not $_->state =~ /^23/ }
Truth table0%0/3
A and B
A= "TryCatch"->check_tc("DBIx::Error")B= do { local $_ = $TryCatch::Error; not $_->state =~ /^23/ }
ABresult
0X0
100
111
536  
537 0 confess "error $err. SQLSTATE WAS: ".$err->state;
538  
539 0 };
540
541 0 for my $i (1..$#{$self->results}){
542 # foreach my $result (@{$self->results}){
543  
544 0 my $result = $self->results->[$i];
545 0 $self->sm->table('xresult')->create({
546 xbranch_id => +$self->xbranch_ids->[0],
547 thread_id => +$self->run_info->thread_id,
548 result => +$self->utils->serialize( $result )
549 });
550 }
551 }
552 }
553  
554  
555 sub get_cached_xresults{
556 0 ▶ my ($self) = @_;
Subroutine get_cached_xresults: not called CC=3 SCAR=24.8
Pod undocumented
557  
558 0 my $results_rs = $self->sm->table('xresult')->search({
559 xbranch_id => $self->xbranch_ids->[0]
560 });
561  
562 0 my $results = [];
563 0 ▶ while( my $result_row = $results_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $result_row = $results_rs->next) 0 0 0
564 0 push @$results, +$self->utils->deserialize( $result_row->result );
565 }
566  
567 0 ▶ return undef unless @$results;
Branch: 1 branch
BranchTrueFalseTotal
unless @$results 0 0 0
568 0 return $results;
569 }
570  
571  
572  
573 #sub delete_cached_xresults{
574 # my ($self) = @_;
575  
576 # my $results_rs = $self->sm->table('xresult')->search({
577 # xbranch_id => $self->xbranch_ids->[0]
578 # });
579  
580 # $results_rs->delete_all if $results_rs;
581 #}
582  
583
584  
585  
586  
587 sub get_plan_to_pipe{
588 0 ▶ my ($self) = @_;
Subroutine get_plan_to_pipe: not called CC=5 SCAR=34.0
Pod undocumented
589  
590 0 my $to_pipe;
591 0 ▶ if ( $self->settings->plan_mode eq 'tree' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->plan_mode eq 'tree') { } 0 0 0
592 0 my $orig_pipe_to = $self->plan->{pipe_to};
593 0 ▶ if ( $orig_pipe_to ){
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_pipe_to) 0 0 0
594 0 my $clone = clone $orig_pipe_to;
595 0 ▶ if ( ref( $clone ) =~ /hash/i ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref($clone) =~ /hash/i) { } 0 0 0
596 0 $to_pipe = [ $clone ];
597 } else {
598 0 $to_pipe = $clone;
599 }
600 }
601 } else {
602 0 my $clone = clone $self->plan;
603 0 shift @$clone;
604 0 ▶ $to_pipe = [ $clone ] if @$clone;
Branch: 1 branch
BranchTrueFalseTotal
if @$clone 0 0 0
605 }
606  
607 0 return $to_pipe;
608  
609 }
610  
611  
612  
613 sub execute_as_manager{
614 301 ▶ my ($self) = @_;
Subroutine execute_as_manager: called CC=10 SCAR=24.1
Pod undocumented
615 301 my $logger = Log::Log4perl->get_logger;
616 301 $logger->debug( "Executing as manager");
617  
618 301 ▶ confess "Cannot execute: No plan to execute was provided" unless defined $self->plan;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->plan 0 301 301
619  
620 301 ▶ if ( $self->settings->plan_mode eq 'branch' && ref($self->plan) eq ref {} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->plan_mode eq "branch" and ref $self->plan eq ref {}) 0 301 301
Condition66%2/3
$self->settings->plan_mode eq "branch" and ref $self->plan eq ref {}
!ll&&!rl&&r
52960
MC/DC0%0/2
$self->settings->plan_mode eq "branch" and ref $self->plan eq ref {}
$self->settings->plan_mode eq "branch" ref $self->plan eq ref {}
Truth table66%2/3
A and B
A= $self->settings->plan_mode eq "branch"B= ref $self->plan eq ref {}
ABresult
0X0
100
111
621 0 confess "B<You have set> C<plan_mode=branch> B<but you appear to be attempting to pass a hash to ".__PACKAGE__." (ie it looks like your plan is in> C<tree> B<format.) Either you need to change> C<plan_mode> to C<tree>B<, or you may have a syntax error in your plan>";
622 }
623  
624 confess "Cannot execute: Plan is empty" if (
625 5 ▶ $self->settings->plan_mode eq 'tree' && ! %{$self->plan}
Condition66%2/3
$self->settings->plan_mode eq "branch" and not @{$self->plan;}
!ll&&!rl&&r
52960
MC/DC0%0/2
$self->settings->plan_mode eq "branch" and not @{$self->plan;}
$self->settings->plan_mode eq "branch" not @{$self->plan;}
Truth table66%2/3
A and not(B)
A= $self->settings->plan_mode eq "branch"B= not @{$self->plan;}
ABresult
0X0
100
111
626 301 ▶ || $self->settings->plan_mode eq 'branch' && ! @{$self->plan}
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->plan_mode eq "tree" and not %{$self->plan;} or $self->settings->plan_mode eq "branch" and not @{$self->plan;} 0 301 301
Condition50%3/6
$self->settings->plan_mode eq "tree" and not %{$self->plan;} or $self->settings->plan_mode eq "branch" and not @{$self->plan;}
l!l&&r!l&&!r
00301
$self->settings->plan_mode eq "tree" and not %{$self->plan;}
!ll&&!rl&&r
29650
MC/DC0%0/3
$self->settings->plan_mode eq "tree" and not %{$self->plan;} or $self->settings->plan_mode eq "branch" and not @{$self->plan;}
$self->settings->plan_mode eq "tree" not %{$self->plan;} $self->settings->plan_mode eq "branch" and not @{$self->plan;}
Truth table20%1/5
A and not(B) or C
A= $self->settings->plan_mode eq "tree"B= not %{$self->plan;}C= $self->settings->plan_mode eq "branch" and not @{$self->plan;}
ABCresult
11X1
0X11
1011
0X00
1000
627 );
628  
629 301 my $status = $self->thread_manager->init;
630  
631 301 ▶ if ( $status eq 'stop' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq 'stop') { } 112 189 301
632 112 $logger->warn("It looks like a previous completed run exists for this project, so I won't continue. If you want to start a fresh run, you should first clear the cache tables (type \"help clear tables\")");
633 } else {
634  
635 189 ▶ if ( $status eq 'start' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "start") 117 72 189
636 117 my $plan = clone +$self->plan;
637 117 my $data = $self->prep_child_task_data( $plan, {},0, 0 );
638 117 $self->thread_manager->execute( $data );
639 }
640  
641 189 $self->thread_manager->manage;
642  
643 }
644 #$self->show_xbranches("at very end");
645 278 $logger->info("Manager: Finished run");
646  
647 }
648  
649  
650  
651  
652  
653 sub get_cached_pinterp{
654 0 ▶ my ($self) = @_;
Subroutine get_cached_pinterp: not called CC=4 SCAR=30.0
Pod undocumented
655 0 my $logger = Log::Log4perl->get_logger;
656 0 my $serialized = $self->utils->serialize( $self->pinterp );
657 0 $self->pinterp_dd( $serialized );
658 0 $self->pinterp_md5( md5_base64( encode("utf8",$serialized ) ) );
659  
660 0 my $cached = $self->sm->table('pinterp')->search({
661 task_name => $self->name,
662 pinterp_md5 => $self->pinterp_md5
663 })->first;
664
665 0 ▶ if ( ! $cached ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cached) 0 0 0
666 0 $logger->debug( "pinterp is not cached");
667 0 $self->cached_pinterp( undef );
668 0 return;
669 }
670  
671 0 ▶ if ( ! $cached->status || $cached->status ne 'fully-cached'){
Branch: 1 branch
BranchTrueFalseTotal
if (not $cached->status or $cached->status ne "fully-cached") 0 0 0
Condition0%0/3
not $cached->status or $cached->status ne "fully-cached"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $cached->status or $cached->status ne "fully-cached"
not $cached->status $cached->status ne "fully-cached"
Truth table0%0/3
not(A) or B
A= not $cached->statusB= $cached->status ne "fully-cached"
ABresult
1X1
011
000
672 0 $self->cached_pinterp( undef );
673 0 return;
674 }
675  
676 0 $logger->debug( "pinterp is cached");
677 0 $self->cached_pinterp( $cached );
678 }
679  
680  
681  
682  
683 sub clear_partially_cached{
684  
685 0 ▶ my ($self,$cached) = @_;
Subroutine clear_partially_cached: not called CC=2 SCAR=17.9
Pod undocumented
686  
687 0 my $guard = $self->sm->schema->txn_scope_guard;
688 0 my $groups = $self->sm->table('result_group')->search({
689 pinterp_id => $cached->id
690 });
691  
692 0 ▶ while( my $group = $groups->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $groups->next) 0 0 0
693
694 0 my $results = $self->sm->table('result')->search({
695 group_id => $group->id
696 })->delete_all;
697  
698 }
699  
700 0 $groups->delete_all;
701 0 $cached->delete;
702 0 $guard->commit;
703 }
704  
705  
706
707 sub cache_pinterp{
708 0 ▶ my ($self) = @_;
Subroutine cache_pinterp: not called CC=1 SCAR=6.9
Pod undocumented
709  
710 0 my $serialized = $self->utils->serialize( $self->pinterp );
711 0 $self->pinterp_dd( $serialized );
712  
713 0 $self->pinterp_md5( md5_base64($self->pinterp_dd) );
714  
715 0 my $cached = $self->sm->table('pinterp')->create({
716 task_name => $self->name,
717 pinterp_md5 => $self->pinterp_md5,
718 pinterp_dd => $self->pinterp_dd,
719 status => 'partially-cached'
720 });
721  
722 0 $self->cached_pinterp( $cached );
723 }
724  
725  
726  
727
728
729 sub next_result{# override in child
730 0 ▶ my ($self) = @_;
Subroutine next_result: not called CC=2 SCAR=17.9
Pod undocumented
731  
732 0 ▶ if ( $self->is_manager ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_manager) { } 0 0 0
733  
734 0 return +$self->next_result_as_manager;
735  
736 } else {
737  
738 0 return +$self->next_result_as_task;
739  
740 }
741 }
742  
743  
744  
745 sub next_result_as_manager{
746 0 ▶ my ($self) = @_;
Subroutine next_result_as_manager: not called CC=2 SCAR=17.9
Pod undocumented
747  
748 0 my %input = %{$self->input};
749 0 $self->input({});
750 0 ▶ return +%input?\%input:undef;
Branch: 1 branch
BranchTrueFalseTotal
%input ? : 0 0 0
751 }
752  
753  
754  
755 sub next_result_as_task{
756 0 ▶ my ($self) = @_;
Subroutine next_result_as_task: not called CC=3 SCAR=24.8
Pod undocumented
757  
758 0 ▶ confess "next_result called in TaskPipe::Task but ->results is not defined. Did you forget to define a next_result method in your task?" unless $self->results;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->results 0 0 0
759  
760 0 ▶ return undef if $self->results_pointer > $#{$self->results};
Branch: 1 branch
BranchTrueFalseTotal
if $self->results_pointer > $#{$self->results;} 0 0 0
761 0 my $result = $self->results->[ $self->results_pointer ];
762 0 $self->results_pointer( $self->results_pointer + 1 );
763 0 return $result;
764 }
765  
766  
767  
768  
769  
770 sub count_results{
771 0 ▶ my ($self) = @_;
Subroutine count_results: not called CC=3 SCAR=24.8
Pod undocumented
772  
773 0 my $logger = Log::Log4perl->get_logger;
774  
775 0 ▶ return -1 unless $self->results;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->results 0 0 0
776  
777 0 ▶ $logger->debug("results: ".Dumper( $self->results )) if $self->name eq 'Scrape_SearchSuggestions';
Branch: 1 branch
BranchTrueFalseTotal
if $self->name eq "Scrape_SearchSuggestions" 0 0 0
778  
779 0 return +scalar(@{$self->results});
780 }
781  
782  
783 sub reset_results{
784 0 ▶ my ($self,$index) = @_;
Subroutine reset_results: not called CC=3 SCAR=24.8
Pod undocumented
785 0 ▶ confess "reset_results called in TaskPipe::Task but ->results is not defined. Did you forget to define a count_results method in your task?" unless $self->results;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->results 0 0 0
786  
787 0 ▶ $index ||= 0;
Condition0%0/2
$index ||= 0
l!l
00
MC/DC0%0/1
$index ||= 0
$index
Truth table0%0/2
A
A= $index
Aresult
11
00
788 0 $self->results_pointer( $index );
789 }
790  
791  
792  
793 sub is_manager{ #it's a manager if it is the root task
794 #ie it doesn't have a type, so it's class
795 #should be just 'Task'
796  
797 301 ▶ my ($self) = @_;
Subroutine is_manager: called CC=2 SCAR=7.2
Pod undocumented
798 301 ▶ return ref($self) eq __PACKAGE__?1:0;
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq __PACKAGE__ ? : 301 0 301
799 }
800  
801  
802 sub pipe_through{
803 0 ▶ my ($self) = @_;
Subroutine pipe_through: not called CC=3 SCAR=24.8
Pod undocumented
804  
805 0 my %input = %{$self->input};
806 0 my $plan = clone $self->plan;
807 0 my $task = $self->task_from_plan( $plan );
808 0 $task->input( \%input );
809  
810 0 ▶ if ( $self->settings->plan_mode eq 'tree' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->plan_mode eq 'tree') { } 0 0 0
811 0 $task->param( clone $plan->{task} );
812 } else {
813 0 $task->param( clone $plan->[0] );
814 }
815  
816 0 eval{ $task->execute };
817 0 ▶ $task->handle_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
818  
819 }
820  
821  
822  
823 sub pipe_to{
824 0 ▶ my ($self,$plan) = @_;
Subroutine pipe_to: not called CC=3 SCAR=24.8
Pod undocumented
825  
826 0 my $logger = Log::Log4perl->get_logger;
827  
828 0 my $tm = $self->thread_manager;
829
830 0 my $ni = $self->count_results;
831
832 0 my ($i0, $p0, $last_result ) = $self->get_resume_info;
833 0 $logger->trace("xbranch_id ".$self->xbranch_ids->[0]." plan id ".$self->branch_id." input id ".$self->input_id." Got resume info i0 $i0 p0 $p0 last_result ".Dumper($last_result));
834  
835 0 my $input_item;
836  
837 0 my $count = 0;
838  
839 0 $logger->trace("Results: ".Dumper( $self->results ));
840  
841 0 for my $pi ($p0..$#$plan){
842 0 $logger->trace("in loop, pi $pi");
843  
844 0 $self->reset_results( $i0, $last_result );
845 0 my $ii = $i0;
846  
847 0 while(1){
848  
849 0 $input_item = $self->next_result;
850 0 $logger->trace("input item: ".Dumper( $input_item ));
851 0 ▶ last unless $input_item;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_item 0 0 0
852  
853 0 $ii++;
854  
855 0 my $data = $self->prep_child_task_data(
856 $plan->[$pi],
857 $input_item,
858 $ii,
859 $pi
860 );
861  
862 0 $tm->execute( $data );
863 0 $self->utils->record_resume_info(
864 $self->xbranch_ids->[0],
865 $ii,
866 $pi,
867 $input_item
868 );
869  
870 }
871  
872 0 $i0 = 0; # make sure to start inputs from scratch on the next plan iteration
873  
874 }
875  
876 }
877  
878  
879  
880 sub get_resume_info{
881 0 ▶ my ($self) = @_;
Subroutine get_resume_info: not called CC=6 SCAR=37.4
Pod undocumented
882  
883 0 my $logger = Log::Log4perl->get_logger;
884  
885 0 my $xbranch = $self->sm->table('xbranch')->find({
886 id => $self->xbranch_ids->[0]
887 });
888  
889 0 my $plan_index = 0;
890 0 my $input_index = 0;
891 0 my $last_result;
892  
893 0 ▶ if ( $xbranch ){
Branch: 1 branch
BranchTrueFalseTotal
if ($xbranch) 0 0 0
894 0 ▶ $plan_index = $xbranch->last_plan_index || 0;
Condition0%0/2
$xbranch->last_plan_index || 0
l!l
00
MC/DC0%0/1
$xbranch->last_plan_index || 0
$xbranch->last_plan_index
Truth table0%0/2
A
A= $xbranch->last_plan_index
Aresult
11
00
895 0 ▶ $input_index = $xbranch->last_input_index || 0;
Condition0%0/2
$xbranch->last_input_index || 0
l!l
00
MC/DC0%0/1
$xbranch->last_input_index || 0
$xbranch->last_input_index
Truth table0%0/2
A
A= $xbranch->last_input_index
Aresult
11
00
896 0 ▶ $last_result = $xbranch->last_result || undef;
Condition0%0/2
$xbranch->last_result || undef
l!l
00
MC/DC0%0/1
$xbranch->last_result || undef
$xbranch->last_result
Truth table0%0/2
A
A= $xbranch->last_result
Aresult
11
00
897 }
898  
899 0 ▶ $last_result = $self->utils->deserialize( $last_result ) if $last_result;
Branch: 1 branch
BranchTrueFalseTotal
if $last_result 0 0 0
900  
901 #$logger->debug("GOT RESUME INFO: xid ".$self->xbranch_ids->[0]." ii $input_index pi $plan_index last_result ".Dumper( $last_result ) );
902
903 0 return ($input_index,$plan_index,$last_result);
904  
905 }
906  
907  
908 #sub execute_child_task{
909 # my ($self,$plan,$input) = @_;
910  
911 # my $logger = Log::Log4perl->get_logger;
912  
913 # my $task = $self->task_from_plan($plan);
914  
915 # $task->input($input);
916 # $self->add_history( $task );
917  
918 # my $param;
919 # if ( $self->settings->plan_mode eq 'tree' ){
920 # $task->param( clone $plan->{task} );
921 # } else {
922 # $task->param( clone $plan->[0] );
923 # }
924  
925 # my $err;
926 # try {
927 # $task->execute;
928 # } catch( $err ) {
929 # $task->handle_error($err);
930 # };
931  
932 # Log::Log4perl->remove_logger( $logger );
933 # $task->sm->schema->storage->disconnect;
934 # $task->gm->schema->storage->disconnect;
935  
936 #}
937  
938  
939  
940 sub prep_child_task_data{
941 117 ▶ my ($self,$plan,$input,$ii,$pi) = @_;
Subroutine prep_child_task_data: called CC=4 SCAR=13.9
Pod undocumented
942  
943 117 my $i_current = clone $self->input;
944 117 my $i_history = clone $self->input_history;
945 117 unshift @$i_history, $i_current;
946  
947 117 my $p_current = clone $self->param;
948 117 my $p_history = clone $self->param_history;
949 117 unshift @$p_history, $p_current;
950  
951 117 my $xbranches = clone $self->xbranch_ids;
952  
953 117 my $param;
954 117 ▶ if ( $self->settings->plan_mode eq 'tree' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->plan_mode eq 'tree') { } 5 112 117
955 5 $param = clone $plan->{task};
956 } else {
957 112 $param = clone $plan->[0];
958 }
959  
960 117 my ($branch_id) = $self->branch_id =~ /^(\d+)/;
961 117 $branch_id++;
962 117 $branch_id.='-'.$pi;
963  
964 117 my $input_id;
965 117 ▶ if ( ! $self->input_id ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->input_id) { } 117 0 117
966 117 $input_id = $ii;
967 } else {
968 0 $input_id = $self->input_id.'-'.$ii;
969 }
970  
971 117 my $plan_key = $branch_id;
972 117 my $input_key = $input_id;
973 117 ▶ if ( $self->settings->xbranch_key_mode eq 'md5' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->xbranch_key_mode eq "md5") 117 0 117
974 117 my $full = [ $input, @$i_history ];
975 117 $input_key = md5_base64( encode("utf8",+$self->utils->serialize( $full ) ) );
976 117 $plan_key = md5_base64( encode("utf8",+$self->utils->serialize( $plan ) ) );
977 }
978  
979 return {
980 117 plan_key => $plan_key,
981 input_key => $input_key,
982 plan => $plan,
983 input => $input,
984 ii => $ii,
985 pi => $pi,
986 branch_id => $branch_id,
987 input_id => $input_id,
988 input_history => $i_history,
989 param_history => $p_history,
990 xbranch_ids => $xbranches,
991 param => $param,
992 plan_mode => +$self->settings->plan_mode
993 };
994 }
995  
996  
997  
998  
999  
1000 sub mark_seen{
1001 0 ▶ my ($self) = @_;
Subroutine mark_seen: not called CC=5 SCAR=34.0
Pod undocumented
1002
1003 0 my $logger = Log::Log4perl->get_logger;
1004  
1005 0 my $xbranch = $self->sm->table('xbranch')->search({
1006 id => $self->xbranch_ids->[0]
1007 });
1008 0 my $child_xbranches = $self->sm->table('xbranch')->search({
1009 parent_id => $self->xbranch_ids->[0]
1010 });
1011  
1012 # if ( $self->branch_id !~ /^2/ ){
1013 0 ▶ if ( $self->settings->seen_xbranch_policy eq 'skip' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->settings->seen_xbranch_policy eq 'skip') { } 0 0 0
elsif ($self->settings->seen_xbranch_policy eq 'delete') { } 0 0 0
1014  
1015 0 $logger->debug("Updating xbranch status to 'seen'");
1016  
1017 0 ▶ if ( $xbranch ){
Branch: 1 branch
BranchTrueFalseTotal
if ($xbranch) 0 0 0
1018 0 $xbranch->update({ status => 'seen' });
1019 }
1020 } elsif ( $self->settings->seen_xbranch_policy eq 'delete' ){
1021 0 $logger->debug("Deleting xbranch record");
1022
1023 0 $xbranch->update({ status => 'seen' });
1024  
1025 0 ▶ while( my $child_xbranch = $child_xbranches->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $child_xbranch = $child_xbranches->next) 0 0 0
1026 0 $self->sm->table('xresult')->search({
1027 xbranch_id => $child_xbranch->id
1028 })->delete_all;
1029 }
1030 0 $child_xbranches->delete_all;
1031 0 $self->sm->table('xresult')->search({
1032 xbranch_id => $self->xbranch_ids->[0]
1033 })->delete_all;
1034  
1035 } else {
1036 0 confess "Unrecognised seen_xbranch_policy: ".$self->settings->seen_xbranch_policy;
1037 }
1038 # }
1039  
1040 }
1041  
1042  
1043  
1044  
1045 sub add_history{
1046 0 ▶ my ($self,$task) = @_;
Subroutine add_history: not called CC=1 SCAR=6.9
Pod undocumented
1047  
1048 0 my $i_current = clone $self->input;
1049 0 my $i_history = clone $self->input_history;
1050 0 unshift @$i_history, $i_current;
1051 0 $task->input_history( $i_history );
1052  
1053 0 my $p_current = clone $self->param;
1054 0 my $p_history = clone $self->param_history;
1055 0 unshift @$p_history, $p_current;
1056 0 $task->param_history( $p_history );
1057  
1058 0 my $xbranches = clone $self->xbranch_ids;
1059 0 $task->xbranch_ids( $xbranches );
1060 }
1061  
1062  
1063  
1064 sub task_from_plan{
1065 0 ▶ my ($self,$plan) = @_;
Subroutine task_from_plan: not called CC=5 SCAR=34.0
Pod undocumented
1066  
1067 0 my $spec;
1068 0 ▶ if ( $self->settings->plan_mode eq 'tree' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->plan_mode eq 'tree') { } 0 0 0
1069 0 $spec = $plan->{task};
1070 } else {
1071 0 $spec = $plan->[0];
1072 }
1073  
1074 0 my $name = $spec->{_name};
1075 0 ▶ confess "No name. spec was ".Dumper($spec)." plan was ".Dumper( $plan ) unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
1076 0 ▶ confess "task spec should be a hash" unless $spec && ref($spec) =~ /hash/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec and ref($spec) =~ /hash/i 0 0 0
Condition0%0/3
$spec and ref($spec) =~ /hash/i
!ll&&!rl&&r
000
MC/DC0%0/2
$spec and ref($spec) =~ /hash/i
$spec ref($spec) =~ /hash/i
Truth table0%0/3
A and B
A= $specB= ref($spec) =~ /hash/i
ABresult
0X0
100
111
1077  
1078 0 my $mod_map = TaskPipe::Task::ModuleMap->new(
1079 task_name => $name
1080 );
1081
1082 0 my $mod_name = $mod_map->load_module;
1083  
1084 0 my $task = $mod_name->new(
1085 sm => $self->sm,
1086 gm => $self->gm,
1087 thread_manager => $self->thread_manager,
1088 logger_manager => $self->logger_manager,
1089 plan => $plan
1090 );
1091  
1092 0 return $task;
1093  
1094 }
1095  
1096  
1097  
1098 sub set_xbranch_status{
1099 0 ▶ my ($self,$status) = @_;
Subroutine set_xbranch_status: not called CC=2 SCAR=17.9
Pod undocumented
1100  
1101 0 my $xbranch = $self->sm->table('xbranch')->find({
1102 id => $self->xbranch_ids->[0]
1103 });
1104  
1105 0 ▶ if ( $xbranch ){
Branch: 1 branch
BranchTrueFalseTotal
if ($xbranch) 0 0 0
1106 0 $xbranch->update({
1107 status => $status
1108 });
1109 }
1110 }
1111  
1112  
1113  
1114  
1115 sub handle_error{
1116 0 ▶ my ($self,$error_msg) = @_;
Subroutine handle_error: not called CC=3 SCAR=24.8
Pod undocumented
1117  
1118 0 my $logger = Log::Log4perl->get_logger;
1119  
1120 0 $logger->error($error_msg);
1121  
1122 0 my $guard = $self->sm->schema->txn_scope_guard;
1123  
1124 my $error = $self->sm->table('error')->create({
1125 job_id => $self->run_info->job_id,
1126 0 history_index => scalar( @{$self->input_history} ),
1127 tag => $self->param->{_tag},
1128 task_name => $self->name,
1129 input_dd => Dumper( $self->input ),
1130 param_dd => Dumper( $self->param ),
1131 pinterp_dd => Dumper( $self->pinterp ),
1132 0 xbranch_ids => join(',',@{$self->xbranch_ids}),
1133 thread_id => $self->run_info->thread_id,
1134 message => $error_msg
1135 });
1136  
1137 0 foreach my $xbranch_id ( @{$self->xbranch_ids} ){
1138  
1139 0 $self->sm->table('xbranch_error')->create({
1140 xbranch_id => $xbranch_id,
1141 error_id => $error->id
1142 });
1143 }
1144 0 $guard->commit;
1145  
1146 0 ▶ $self->thread_manager->stop_threads if $self->settings->on_task_error eq 'stop';
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->on_task_error eq "stop" 0 0 0
1147 }
1148  
1149  
1150  
1151 sub xbranch_has_error{
1152 0 ▶ my ($self) = @_;
Subroutine xbranch_has_error: not called CC=2 SCAR=17.9
Pod undocumented
1153  
1154 0 my $has_error = 0;
1155  
1156 0 my $num_errors = $self->sm->table('xbranch_error')->search({
1157 xbranch_id => $self->xbranch_ids->[0]
1158 })->count;
1159  
1160 0 ▶ $has_error = 1 if $num_errors > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_errors > 0 0 0 0
1161 0 return $has_error;
1162 }
1163  
1164  
1165  
1166  
1167 sub action{
1168 0 ▶ my ($self) = @_;
Subroutine action: not called CC=1 SCAR=6.9
Pod undocumented
1169  
1170 0 confess "action in ".__PACKAGE__." should NOT be called. Override this method in child class";
1171  
1172 }
1173  
1174  
1175  
1176  
1177  
1178 =head1 NAME
1179  
1180 TaskPipe::Task - the class which all TaskPipe tasks should inherit from
1181  
1182 =head1 DESCRIPTION
1183  
1184 Inherit from this class when creating new tasks. Your child task should contain an C<action> subroutine, which returns either:
1185  
1186 =over
1187  
1188 =item 1.
1189  
1190 An arrayref of results
1191  
1192 =item 2.
1193  
1194 A L<TaskPipe::Iterator> of results. (See the L<TaskPipe::Iterator> manpage for more information
1195  
1196 =back
1197  
1198 You should write your task in the following format:
1199  
1200 package TaskPipe::Task_MyTaskName
1201  
1202 use Moose;
1203 extends 'TaskPipe::Task';
1204  
1205
1206 sub action {
1207 my ($self) = @_;
1208  
1209 # access plan parameters in here
1210 # via $self->param
1211  
1212 my $some_val = $self->param->{some_param};
1213  
1214 # ... do something with $some_val ...
1215  
1216 # ...
1217  
1218 return \@results;
1219 }
1220  
1221 __PACKAGE__->meta->make_immutable;
1222 1;
1223  
1224 Then you reference it in your plan via something like:
1225  
1226 # (in tree format):
1227  
1228 task:
1229 _name: MyTaskName
1230 some_param: 46
1231  
1232 pipe_to:
1233  
1234 # ...
1235  
1236 Note that if you are creating a task to scrape a website, it is recommended to inherit from L<TaskPipe::Task_Scrape> instead of inheriting from L<TaskPipe::Task> directly. See L<TaskPipe::Task_Scrape> for more information
1237  
1238 =head1 SEE ALSO
1239  
1240 See the other tasks that are provided to make sure you are not creating a task that exists already:
1241  
1242 L<TaskPipe::Task_Scrape>
1243 L<TaskPipe::Task_Record>
1244 L<TaskPipe::Task_SourceFromDB>
1245 L<TaskPipe::Task_SourceFromFile>
1246  
1247 =head1 AUTHOR
1248  
1249 Tom Gracey <tomgracey@gmail.com>
1250  
1251 =head1 COPYRIGHT AND LICENSE
1252  
1253 Copyright (c) Tom Gracey 2018
1254  
1255 TaskPipe is free software, licensed under
1256  
1257 The GNU Public License Version 3
1258  
1259 =cut
1260  
1261 38 __PACKAGE__->meta->make_immutable;
1262 38 1;
« TaskPipe/Task_Record_Array.pm ↑ Summary TaskPipe/InterpParam/MatchHandler.pm »