TaskPipe/TaskUtils.pm

filter: Statementstmt 56.5% 65 / 115 Branchbran 20.8% 5 / 24 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 16 / 28 Podpod 0.0% 0 / 9 totaltotal 46.7% 87 / 186 CC 16 SCAR 20.5
CC
16
Cov
49%
task_from_data
30.0
record_last_task_info
20.0
record_resume_info
12.0
CRAP 7.8
« TaskPipe/OpenProxyManager/IPList.pm ↑ Summary TaskPipe/Tool/Command_Setup.pm »
1 package TaskPipe::TaskUtils;
2  
3 41 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use TaskPipe::Task::ModuleMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use TaskPipe::RunInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 41 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 41 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 41 has sm => (is => 'rw', isa => 'TaskPipe::SchemaManager');
13 41 has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager');
14  
15 has run_info => (is => 'ro', isa => 'TaskPipe::RunInfo', default => sub{
16 354 ▶ TaskPipe::RunInfo->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 41 });
18  
19 has json_encoder => (is => 'ro', isa => 'JSON', default => sub{
20 354 ▶ my $json_enc = JSON->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
21 354 $json_enc->canonical;
22 354 return $json_enc;
23 41 });
24  
25 has settings => (is => 'ro', isa => __PACKAGE__.'::Settings', default => sub{
26 354 ▶ my $module = __PACKAGE__.'::Settings';
Subroutine __ANON__: called CC=1 SCAR=0.0
27 354 require_module( $module );
28 354 return +$module->new;
29 41 });
30  
31  
32  
33 sub task_from_data{
34 0 ▶ my ($self,$data) = @_;
Subroutine task_from_data: not called CC=5 SCAR=34.0
Pod undocumented
35  
36 0 my $logger = Log::Log4perl->get_logger;
37  
38 0 my $spec;
39 0 ▶ if ( $data->{plan_mode} eq 'tree' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'plan_mode'} eq 'tree') { } 0 0 0
40 0 $spec = $data->{plan}{task};
41 } else {
42 0 $spec = $data->{plan}[0];
43 }
44  
45 0 my $name = $spec->{_name};
46 0 ▶ confess "No name. spec was ".Dumper($spec)." plan was ".Dumper( $data->{plan} ) unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
47 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
48  
49 0 my $mod_map = TaskPipe::Task::ModuleMap->new(
50 task_name => $name
51 );
52
53 0 my $mod_name = $mod_map->load_module;
54  
55 0 $logger->debug("input_id ".$data->{input_id});
56  
57 my $task = $mod_name->new(
58 sm => +$self->sm,
59 gm => +$self->gm,
60 plan_key => $data->{plan_key},
61 input_key => $data->{input_key},
62 plan => $data->{plan},
63 branch_id => $data->{branch_id},
64 input => $data->{input},
65 input_id => $data->{input_id},
66 input_history => $data->{input_history},
67 param => $data->{param},
68 param_history => $data->{param_history},
69 xbranch_ids => $data->{xbranch_ids}
70 0 );
71  
72 0 return $task;
73 }
74  
75  
76 sub record_last_task_info{
77 0 ▶ my ($self,$data) = @_;
Subroutine record_last_task_info: not called CC=4 SCAR=30.0
Pod undocumented
78  
79 0 my $spec;
80 0 ▶ if ( $data->{plan_mode} eq 'tree' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'plan_mode'} eq 'tree') { } 0 0 0
81 0 $spec = $data->{plan}{task};
82 } else {
83 0 $spec = $data->{plan}[0];
84 }
85  
86 0 my $thread_row = $self->sm->table('thread')->find({
87 id => $self->run_info->thread_id
88 });
89  
90 0 my $details = +$spec->{_name};
91 0 ▶ if ( $spec->{_name} eq 'Record' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"_name"} eq "Record") 0 0 0
92 0 $details.=": ".$spec->{table};
93 }
94  
95 0 ▶ if ( $thread_row ){
Branch: 1 branch
BranchTrueFalseTotal
if ($thread_row) 0 0 0
96 0 $thread_row->update({
97 last_task => $details
98 });
99 }
100 }
101
102  
103  
104 sub create_xbranch_record{
105 121 ▶ my ($self,$task_data,$thread_id,$status) = @_;
Subroutine create_xbranch_record: called CC=5 SCAR=17.0
Pod undocumented
106  
107 121 my $logger = Log::Log4perl->get_logger;
108  
109 121 my $parent_xbranch_id = undef;
110 121 ▶ $parent_xbranch_id = $task_data->{xbranch_ids}->[0] if defined $task_data->{xbranch_ids}->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $task_data->{"xbranch_ids"}[0] 0 121 121
111  
112 121 my $xbranch;
113  
114 121 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$@ and $@ =~ /duplicate\sentry/i and do { do { eval { do { $xbranch = $self->sm->table("xbranch")->find_or_create({"plan_key", $task_data->{"plan_key"}, "input_key", $task_data->{"input_key"}, "thread_id", $thread_id, "branch_id", $task_data->{"branch_id"}, "input_id", $task_data->{"input_id"}, "parent_id", $parent_xbranch_id, "status", $status}, {"key", "plan_key"}) } } } } 0 121 121
Condition33%1/3
$@ and $@ =~ /duplicate\sentry/i
!ll&&!rl&&r
12100
MC/DC0%0/2
$@ and $@ =~ /duplicate\sentry/i
$@ $@ =~ /duplicate\sentry/i
Truth table33%1/3
A and B
A= $@B= $@ =~ /duplicate\sentry/i
ABresult
0X0
100
111
115 121 eval{
116  
117 $xbranch = $self->sm->table('xbranch')->find_or_create({
118 plan_key => $task_data->{plan_key},
119 input_key => $task_data->{input_key},
120 thread_id => $thread_id,
121 branch_id => $task_data->{branch_id},
122 input_id => $task_data->{input_id},
123 121 parent_id => $parent_xbranch_id,
124 status => $status
125 },
126 {key => 'plan_key'});
127 };
128  
129 # } while ($@);
130 } while ($@ && $@ =~ /duplicate\sentry/i);
131  
132 121 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 121 121
133  
134 121 return $xbranch;
135 }
136  
137  
138  
139  
140  
141 sub exec_task_from_data{
142 0 ▶ my ($self,$data) = @_;
Subroutine exec_task_from_data: not called CC=1 SCAR=6.9
Pod undocumented
143  
144 0 $self->record_last_task_info( $data );
145 0 my $task = $self->task_from_data( $data );
146  
147 try {
148 0 ▶ $task->execute;
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 } catch {
150 0 ▶ $task->handle_error($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 };
152  
153 0 return $task;
154 }
155  
156  
157  
158 sub serialize{
159 1937 ▶ my ($self,$ref) = @_;
Subroutine serialize: called CC=1 SCAR=0.0
Pod undocumented
160  
161 1937 my $serialized;
162  
163 try {
164 1937 ▶ $serialized = $self->json_encoder->encode( $ref );
Subroutine __ANON__: called CC=1 SCAR=0.0
165 } catch {
166 0 ▶ confess "Serialize error: $_\nref was ".Dumper( $ref );
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 1937 };
168  
169 1937 return $serialized;
170 }
171  
172  
173  
174 sub deserialize{
175 0 ▶ my ($self,$json) = @_;
Subroutine deserialize: not called CC=1 SCAR=6.9
Pod undocumented
176  
177 0 my $ref;
178  
179 try {
180 0 ▶ $ref = $self->json_encoder->decode( $json );
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 } catch {
182 0 ▶ confess "Error deserialising json string '$json': $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 };
184  
185 0 return $ref;
186 }
187  
188  
189  
190  
191 sub exec_xtask_script{
192 25 ▶ my ($self,$data) = @_;
Subroutine exec_xtask_script: called CC=1 SCAR=0.0
Pod undocumented
193  
194 # $self->write_task_data( $data ) if $data;
195  
196 25 my $cmd = $self->settings->xtask_script." ".$self->run_info->job_id." ".$self->run_info->thread_id." ".$self->run_info->root_dir;
197 # my $cmd = "/root/taskpipe/scratch/taskpipe-xtask.pl ".$self->run_info->job_id." ".$self->run_info->thread_id." ".$self->run_info->root_dir;
198  
199 25 $self->sm->schema->storage->disconnect;
200 25 $self->gm->schema->storage->disconnect;
201  
202 25 exec( $cmd );
203 0 exit;
204 }
205  
206  
207  
208 #sub write_xtask_data{
209 # my ($self,$data,$thread_row) = @_;
210  
211 ## confess "Need data and thread_id" unless $data && $thread_id;
212  
213 # my $ds = $self->serialize( $data );
214  
215 ## my $thread_row = $self->gm->table('thread')->find({
216 ## job_id => +$self->run_info->job_id,
217 ## id => $thread_id
218 ## });
219  
220 # confess "No thread row" unless $thread_row;
221  
222 # $thread_row->update({
223 # data => $ds
224 # });
225 #
226 #}
227  
228  
229  
230 sub add_run_info{
231 121 ▶ my ($self,$data) = @_;
Subroutine add_run_info: called CC=3 SCAR=11.0
Pod undocumented
232  
233 121 $data->{run_info} = {};
234 121 my @methods = $self->run_info->meta->get_all_methods;
235  
236 121 my @names = ();
237 121 foreach my $method ( @methods ){
238 2904 my $name = $method->name;
239 2904 ▶ if ( ref( $method ) =~ /Accessor/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref($method) =~ /Accessor/) 1089 1815 2904
240 1089 $data->{run_info}->{ $name } = $self->run_info->$name
241 }
242 }
243  
244 # $data->{thread_id} = $thread_id if $thread_id;
245  
246 }
247  
248  
249  
250 sub record_resume_info{
251 0 ▶ my ($self,$xbranch_id,$input_index,$plan_index,$last_result) = @_;
Subroutine record_resume_info: not called CC=3 SCAR=24.8
Pod undocumented
252  
253 0 ▶ confess "No xbranch_id" unless $xbranch_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $xbranch_id 0 0 0
254  
255 0 my $logger = Log::Log4perl->get_logger;
256  
257 0 $logger->debug("Recording resume info: xid $xbranch_id, ii $input_index, pi $plan_index, last_result ".Dumper( $last_result ) );
258  
259 0 my $xbranch = $self->sm->table('xbranch')->find({
260 id => $xbranch_id
261 });
262  
263 0 ▶ if ( $xbranch ){
Branch: 1 branch
BranchTrueFalseTotal
if ($xbranch) 0 0 0
264 0 my $err;
265 try{
266  
267 0 ▶ $xbranch->update({
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 last_plan_index => $plan_index,
269 last_input_index => $input_index,
270 last_result => +$self->serialize($last_result)
271 });
272  
273 } catch {
274  
275 0 ▶ confess "Database error: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
276  
277 0 };
278  
279 }
280
281 }
282  
283  
284  
285 41 1;
« TaskPipe/OpenProxyManager/IPList.pm ↑ Summary TaskPipe/Tool/Command_Setup.pm »