TaskPipe/JobManager.pm

filter: Statementstmt 79.8% 139 / 174 Branchbran 62.9% 34 / 54 Conditioncond 53.3% 8 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 84.0% 21 / 25 Podpod 0.0% 0 / 10 totaltotal 70.8% 204 / 288 CC 36 SCAR 18.4
CC
36
Cov
74%
daemonize
12.0
list_jobs
12.0
stop_job
11.5
CRAP 6.3
« TaskPipe/Tool/Command__OpenProxies.pm ↑ Summary TaskPipe/UserAgentManager.pm »
1 package TaskPipe::JobManager;
2  
3 40 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use TaskPipe::JobManager::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use TaskPipe::PortManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #has job_id => (is => 'rw', isa => 'Int');
15  
16 has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{
17 883 ▶ TaskPipe::RunInfo->new
Subroutine __ANON__: called CC=1 SCAR=0.0
18 40 });
19  
20 40 has name => (is => 'rw', isa => 'Str');
21 40 has project => (is => 'rw', isa => 'Str');
22  
23 has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager', lazy => 1, default => sub{
24 494 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 494 my $gm = TaskPipe::SchemaManager->new( scope => 'global' );
27 494 $gm->connect_schema;
28 494 return $gm;
29 40 });
30  
31 has settings => (is => 'ro', isa => 'TaskPipe::JobManager::Settings', default => sub{
32 883 ▶ TaskPipe::JobManager::Settings->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
33 40 });
34  
35 has port_manager => (is => 'ro', isa => 'TaskPipe::PortManager', lazy => 1, default => sub{
36 0 ▶ TaskPipe::PortManager->new( gm => $_[0]->gm );
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 40 });
38  
39 40 has proc_lookup => (is => 'rw', isa => 'HashRef', lazy => 1, builder => 'build_proc_lookup');
40  
41  
42  
43 sub build_proc_lookup{
44 411 ▶ my ($self) = @_;
Subroutine build_proc_lookup: called CC=2 SCAR=6.9
Pod undocumented
45  
46 411 my $t = Proc::ProcessTable->new;
47  
48 411 my $lookup = {};
49 411 foreach my $p ( @{$t->table} ){
50 4521 $lookup->{ $p->pid } = $p->cmndline;
51 }
52 411 return $lookup;
53 }
54  
55  
56  
57  
58 sub init_job{
59 882 ▶ my ($self) = @_;
Subroutine init_job: called CC=5 SCAR=16.1
Pod undocumented
60  
61 882 my $logger = Log::Log4perl->get_logger;
62  
63 882 ▶ return if $self->settings->job_tracking ne 'register';
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->job_tracking ne "register" 567 315 882
64 315 $self->gm; #:force connection attempt FIXME
65  
66 315 my $insert = {};
67 315 foreach my $param ( qw(name project) ){
68 630 ▶ confess "Need a $param" unless $self->$param;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$param 0 630 630
69 630 $insert->{$param} = $self->$param;
70 }
71  
72 315 $insert->{pid} = $$;
73 315 $insert->{orig_cmd} = "@{$self->run_info->orig_cmd}";
74 315 $insert->{created_dt} = DateTime->now;
75 315 $insert->{shell} = $self->run_info->shell;
76  
77 315 $self->clean_tables;
78  
79 315 $logger->debug("Recording start of job");
80 315 my $job_row = $self->gm->table('job')->create($insert);
81 315 ▶ confess "Job failed: Unable to register job in database" unless $job_row;
Branch: 1 branch
BranchTrueFalseTotal
unless $job_row 0 315 315
82  
83 315 $self->run_info->job_id( $job_row->id );
84 315 $logger->debug("Job ".$job_row->id." started");
85  
86 315 return $job_row->id;
87 }
88
89  
90  
91 sub end_job{
92 854 ▶ my ($self) = @_;
Subroutine end_job: called CC=3 SCAR=11.0
Pod undocumented
93  
94 854 ▶ return if $self->settings->job_tracking ne 'register';
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->job_tracking ne "register" 564 290 854
95  
96 290 ▶ confess "Need a job_id" unless defined $self->run_info->job_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->run_info->job_id 0 290 290
97  
98 290 $self->stop_spawned;
99  
100 $self->gm->schema->txn_do( sub{
101 290 ▶ $self->gm->table('spawned')->search({
Subroutine __ANON__: called CC=1 SCAR=0.0
102 process_name => "thread",
103 job_id => $self->run_info->job_id
104 })->delete_all;
105 290 $self->gm->table('job')->search({
106 id => $self->run_info->job_id
107 })->delete_all;
108 290 });
109 }
110  
111  
112  
113 sub stop_spawned {
114 427 ▶ my ($self, $job_id) = @_;
Subroutine stop_spawned: called CC=4 SCAR=14.0
Pod undocumented
115  
116 427 my $logger = Log::Log4perl->get_logger;
117  
118 427 ▶ $job_id ||= $self->run_info->job_id;
Condition66%2/3
$job_id ||= $self->run_info->job_id
l!l&&r!l&&!r
1372900
MC/DC0%0/2
$job_id ||= $self->run_info->job_id
$job_id $self->run_info->job_id
Truth table66%2/3
A ||= B
A= $job_idB= $self->run_info->job_id
ABresult
1X1
011
000
119  
120  
121 427 my $spawned_rs = $self->gm->table('spawned')->search({
122 job_id => $job_id
123 });
124  
125 427 ▶ while( my $spawned = $spawned_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $spawned = $spawned_rs->next) 343 427 770
126  
127 343 my $pid = $spawned->pid;
128 343 $logger->debug("Stopping process $pid");
129 343 kill 'KILL', $pid;
130 343 ▶ if ( $spawned->temp_dir ){
Branch: 1 branch
BranchTrueFalseTotal
if ($spawned->temp_dir) 0 343 343
131 0 rmtree( $spawned->temp_dir );
132 }
133 343 $spawned->delete;
134  
135 }
136  
137 }
138  
139  
140  
141  
142 sub daemonize{
143 0 ▶ my $self = shift;
Subroutine daemonize: not called CC=3 SCAR=24.8
Pod undocumented
144  
145 0 $self->gm->table('job')->find({
146 id => $self->run_info->job_id
147 })->update({
148 shell => $self->run_info->shell
149 });
150  
151 0 ▶ $self->fork_safely and exit;
Branch: 1 branch
BranchTrueFalseTotal
$self->fork_safely and exit 0 0 0
152 0 POSIX::setsid();
153 0 my $new_pid = $self->fork_safely;
154
155 0 ▶ if ( $new_pid ){
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pid) 0 0 0
156 0 $self->gm->table('job')->find({
157 id => $self->run_info->job_id
158 })->update({
159 pid => $new_pid
160 });
161 0 exit;
162 }
163  
164 0 umask 0;
165 0 chdir '/';
166 0 close STDIN;
167 0 close STDOUT;
168 0 close STDERR;
169  
170 }
171  
172  
173  
174 sub fork_safely{
175 0 ▶ my $self = shift;
Subroutine fork_safely: not called CC=2 SCAR=17.9
Pod undocumented
176  
177 0 my $pid = fork;
178 0 ▶ confess "Fork failed" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
179 0 return $pid;
180 }
181  
182  
183 sub process_active{
184 822 ▶ my ($self,$pid,$orig_cmd) = @_;
Subroutine process_active: called CC=4 SCAR=13.9
Pod undocumented
185  
186 822 ▶ confess "Need a pid" unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 822 822
187  
188 822 my $logger = Log::Log4perl->get_logger;
189  
190 822 my $active = 0;
191  
192 822 ▶ if ( $self->proc_lookup->{$pid} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proc_lookup->{$pid}) 685 137 822
193  
194 685 $active = 1;
195 685 ▶ $active = 0 unless $self->proc_lookup->{$pid} =~ /taskpipe/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->proc_lookup->{$pid} =~ /taskpipe/ 411 274 685
196  
197 }
198  
199 822 return $active;
200 }
201  
202
203  
204  
205  
206 sub clean_tables{
207 455 ▶ my $self = shift;
Subroutine clean_tables: called CC=8 SCAR=23.2
Pod undocumented
208  
209 455 my $logger = Log::Log4perl->get_logger;
210 455 $logger->debug("Cleaning job table...");
211  
212 455 my $procs_rs = $self->gm->table('job')->search({});;
213  
214 # remove table rows from job and thread which no longer have active processes
215 455 ▶ while( my $proc_row = $procs_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $proc_row = $procs_rs->next) 137 455 592
216  
217 137 my $parent_active = 0;
218 137 my $threads_active = 0;
219  
220 137 $parent_active = $self->process_active($proc_row->pid);
221  
222 # if parent not active, it doesn't mean the process is not running -
223 # the parent could have died but left children active
224 137 my $threads_rs;
225 137 ▶ if ( ! $parent_active ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_active) 137 0 137
226  
227 137 $threads_rs = $self->gm->table('spawned')->search({
228 process_name => 'thread',
229 job_id => $proc_row->id
230 });
231  
232 137 ▶ while( my $thread = $threads_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $thread = $threads_rs->next) 137 0 137
233 137 ▶ if ( $thread->pid && $self->process_active($thread->pid,$proc_row->orig_cmd)){
Branch: 1 branch
BranchTrueFalseTotal
if ($thread->pid and $self->process_active($thread->pid, $proc_row->orig_cmd)) 137 0 137
Condition33%1/3
$thread->pid and $self->process_active($thread->pid, $proc_row->orig_cmd)
!ll&&!rl&&r
00137
MC/DC0%0/2
$thread->pid and $self->process_active($thread->pid, $proc_row->orig_cmd)
$thread->pid $self->process_active($thread->pid, $proc_row->orig_cmd)
Truth table33%1/3
A and B
A= $thread->pidB= $self->process_active($thread->pid, $proc_row->orig_cmd)
ABresult
0X0
100
111
234 137 $threads_active = 1;
235 137 last;
236 }
237 }
238 }
239
240 137 ▶ if ( ! $parent_active && ! $threads_active ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_active or $threads_active) 0 137 137
Condition33%1/3
$parent_active or $threads_active
l!l&&r!l&&!r
01370
MC/DC0%0/2
$parent_active or $threads_active
$parent_active $threads_active
Truth table33%1/3
A or B
A= $parent_activeB= $threads_active
ABresult
1X1
011
000
241 0 $self->stop_spawned( $proc_row->id );
242 0 $self->port_manager->clear_ports( $proc_row->id );
243 0 $threads_rs->delete;
244 0 $proc_row->delete;
245 }
246 }
247  
248 455 $logger->debug("Done cleaning job table");
249 }
250  
251  
252 sub stop_job{
253 140 ▶ my ($self,$job_id) = @_;
Subroutine stop_job: called CC=11 SCAR=24.4
Pod undocumented
254  
255 140 my $logger = Log::Log4perl->get_logger;
256  
257 140 ▶ confess "job_id must be provided" unless defined $job_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $job_id 0 140 140
258  
259 140 $self->clean_tables;
260  
261 140 my %rs;
262 140 $rs{job} = $self->gm->table('job')->search({
263 id => $job_id
264 });
265 140 $rs{thread} = $self->gm->table('spawned')->search({
266 process_name => 'thread',
267 job_id => $job_id
268 });
269  
270 140 ▶ confess "Unable to find an active job with id=".$self->run_info->job_id." - perhaps this process is already dead?" unless $rs{job}->count || $rs{thread}->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $rs{"job"}->count or $rs{"thread"}->count 3 137 140
Condition66%2/3
$rs{"job"}->count or $rs{"thread"}->count
l!l&&r!l&&!r
13703
MC/DC50%1/2
$rs{"job"}->count or $rs{"thread"}->count
$rs{"job"}->count $rs{"thread"}->count
Truth table66%2/3
A or B
A= $rs{"job"}->countB= $rs{"thread"}->count
ABresult
1X1
011
000
271  
272 137 my $active;
273 137 my $count = 0;
274  
275 137 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$active and do { do { $active = 0; my $procs_rs = $self->gm->table("job")->search({"id", $job_id}); $self->proc_lookup($self->build_proc_lookup); while (my $proc_row = $procs_rs->next) { do { if ($self->process_active($proc_row->pid)) { $logger->debug('Killing job pid ' . $proc_row->pid); $active = 1; kill "KILL", $proc_row->pid; } ; my $threads_rs = $self->gm->table("spawned")->search({"process_name", "thread", "job_id", $proc_row->id}); while (my $thread = $threads_rs->next) { do { if ($thread->pid and $self->process_active($thread->pid)) { $logger->debug('Killing thread pid ' . $thread->pid); $active = 1; kill "KILL", $thread->pid; } }; } }; } ; ++$count; confess("Failed to stop job $job_id after $count attempts: unknown error") if $count > $self->settings->max_kill_job_attempts } } 137 137 274
276  
277 274 $active = 0;
278  
279 274 my $procs_rs = $self->gm->table('job')->search({
280 id => $job_id
281 });
282
283 274 $self->proc_lookup( $self->build_proc_lookup );
284 274 ▶ while( my $proc_row = $procs_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $proc_row = $procs_rs->next) 274 274 548
285  
286 274 ▶ if ( $self->process_active($proc_row->pid) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->process_active($proc_row->pid)) 0 274 274
287 0 $logger->debug("Killing job pid ".$proc_row->pid);
288 0 $active = 1;
289 0 kill 'KILL', $proc_row->pid;
290 }
291  
292 274 my $threads_rs = $self->gm->table('spawned')->search({
293 process_name => 'thread',
294 job_id => $proc_row->id
295 });
296  
297 274 ▶ while( my $thread = $threads_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $thread = $threads_rs->next) 274 274 548
298  
299 274 ▶ if ( $thread->pid && $self->process_active($thread->pid) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($thread->pid and $self->process_active($thread->pid)) 137 137 274
Condition66%2/3
$thread->pid and $self->process_active($thread->pid)
!ll&&!rl&&r
0137137
MC/DC50%1/2
$thread->pid and $self->process_active($thread->pid)
$thread->pid $self->process_active($thread->pid)
Truth table66%2/3
A and B
A= $thread->pidB= $self->process_active($thread->pid)
ABresult
0X0
100
111
300 137 $logger->debug("Killing thread pid ".$thread->pid);
301 137 $active = 1;
302 137 kill 'KILL', $thread->pid;
303 }
304 }
305 }
306  
307 274 $count++;
308 274 ▶ confess "Failed to stop job $job_id after $count attempts: unknown error" if $count > $self->settings->max_kill_job_attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $count > $self->settings->max_kill_job_attempts 0 274 274
309  
310 } while ( $active );
311
312 137 $self->stop_spawned( $job_id );
313  
314 }
315  
316  
317  
318  
319 sub list_jobs{
320 0 ▶ my ($self,@cols) = @_;
Subroutine list_jobs: not called CC=3 SCAR=24.8
Pod undocumented
321  
322 #$self->clean_tables;
323  
324 0 my $jobs_rs = $self->gm->table('job')->search;
325  
326 0 my $list = [];
327 0 ▶ while (my $job = $jobs_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $job = $jobs_rs->next) 0 0 0
328 0 my $li = [];
329 0 foreach my $col (@cols){
330 0 push @$li,$job->$col
331 }
332 0 push @$list,$li;
333 }
334  
335 0 return $list;
336 }
337  
338  
339 =head1 NAME
340  
341 TaskPipe::JobManager - manages TaskPipe jobs
342  
343 =head1 DESCRIPTION
344  
345 It is not recommended to use this module directly. See the general manpages for TaskPipe
346  
347 =head1 AUTHOR
348  
349 Tom Gracey <tomgracey@gmail.com>
350  
351 =head1 COPYRIGHT AND LICENSE
352  
353 Copyright (c) Tom Gracey 2018
354  
355 TaskPipe is free software, licensed under
356  
357 The GNU Public License Version 3
358  
359 =cut
360  
361 40 __PACKAGE__->meta->make_immutable;
362 40 1;
« TaskPipe/Tool/Command__OpenProxies.pm ↑ Summary TaskPipe/UserAgentManager.pm »