TaskPipe/JobManager.pm

filter: Statementstmt 82.7% 144 / 174 Branchbran 66.6% 36 / 54 Conditioncond 60.0% 9 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 10 totaltotal 74.3% 214 / 288 CC 36 SCAR 18.1
CC
36
Cov
78%
daemonize
12.0
list_jobs
12.0
stop_job
12.0
CRAP 6.1
« TaskPipe/UserAgentManager.pm ↑ Summary TaskPipe/UserAgentManager_ProxyNet.pm »
1 package TaskPipe::JobManager;
2  
3 38 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 38 ▶use TaskPipe::JobManager::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 38 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 38 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶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 827 ▶ TaskPipe::RunInfo->new
Subroutine __ANON__: called CC=1 SCAR=0.0
18 38 });
19  
20 38 has name => (is => 'rw', isa => 'Str');
21 38 has project => (is => 'rw', isa => 'Str');
22  
23 has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager', lazy => 1, default => sub{
24 456 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 456 my $gm = TaskPipe::SchemaManager->new( scope => 'global' );
27 456 $gm->connect_schema;
28 456 return $gm;
29 38 });
30  
31 has settings => (is => 'ro', isa => 'TaskPipe::JobManager::Settings', default => sub{
32 827 ▶ TaskPipe::JobManager::Settings->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
33 38 });
34  
35 has port_manager => (is => 'ro', isa => 'TaskPipe::PortManager', lazy => 1, default => sub{
36 33 ▶ TaskPipe::PortManager->new( gm => $_[0]->gm );
Subroutine __ANON__: called CC=1 SCAR=0.0
37 38 });
38  
39 38 has proc_lookup => (is => 'rw', isa => 'HashRef', lazy => 1, builder => 'build_proc_lookup');
40  
41  
42  
43 sub build_proc_lookup{
44 387 ▶ my ($self) = @_;
Subroutine build_proc_lookup: called CC=2 SCAR=6.9
Pod undocumented
45  
46 387 my $t = Proc::ProcessTable->new;
47  
48 387 my $lookup = {};
49 387 foreach my $p ( @{$t->table} ){
50 4191 $lookup->{ $p->pid } = $p->cmndline;
51 }
52 387 return $lookup;
53 }
54  
55  
56  
57  
58 sub init_job{
59 826 ▶ my ($self) = @_;
Subroutine init_job: called CC=5 SCAR=16.1
Pod undocumented
60  
61 826 my $logger = Log::Log4perl->get_logger;
62  
63 826 ▶ return if $self->settings->job_tracking ne 'register';
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->job_tracking ne "register" 525 301 826
64 301 $self->gm; #:force connection attempt FIXME
65  
66 301 my $insert = {};
67 301 foreach my $param ( qw(name project) ){
68 602 ▶ confess "Need a $param" unless $self->$param;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$param 0 602 602
69 602 $insert->{$param} = $self->$param;
70 }
71  
72 301 $insert->{pid} = $$;
73 301 $insert->{orig_cmd} = "@{$self->run_info->orig_cmd}";
74 301 $insert->{created_dt} = DateTime->now;
75 301 $insert->{shell} = $self->run_info->shell;
76  
77 301 $self->clean_tables;
78  
79 301 $logger->debug("Recording start of job");
80 301 my $job_row = $self->gm->table('job')->create($insert);
81 301 ▶ confess "Job failed: Unable to register job in database" unless $job_row;
Branch: 1 branch
BranchTrueFalseTotal
unless $job_row 0 301 301
82  
83 301 $self->run_info->job_id( $job_row->id );
84 301 $logger->debug("Job ".$job_row->id." started");
85  
86 301 return $job_row->id;
87 }
88
89  
90  
91 sub end_job{
92 803 ▶ my ($self) = @_;
Subroutine end_job: called CC=3 SCAR=11.0
Pod undocumented
93  
94 803 ▶ return if $self->settings->job_tracking ne 'register';
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->job_tracking ne "register" 525 278 803
95  
96 278 ▶ confess "Need a job_id" unless defined $self->run_info->job_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->run_info->job_id 0 278 278
97  
98 278 $self->stop_spawned;
99  
100 $self->gm->schema->txn_do( sub{
101 278 ▶ $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 278 $self->gm->table('job')->search({
106 id => $self->run_info->job_id
107 })->delete_all;
108 278 });
109 }
110  
111  
112  
113 sub stop_spawned {
114 429 ▶ my ($self, $job_id) = @_;
Subroutine stop_spawned: called CC=4 SCAR=14.0
Pod undocumented
115  
116 429 my $logger = Log::Log4perl->get_logger;
117  
118 429 ▶ $job_id ||= $self->run_info->job_id;
Condition66%2/3
$job_id ||= $self->run_info->job_id
l!l&&r!l&&!r
1512780
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 429 my $spawned_rs = $self->gm->table('spawned')->search({
122 job_id => $job_id
123 });
124  
125 429 ▶ while( my $spawned = $spawned_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $spawned = $spawned_rs->next) 311 429 740
126  
127 311 my $pid = $spawned->pid;
128 311 $logger->debug("Stopping process $pid");
129 311 kill 'KILL', $pid;
130 311 ▶ if ( $spawned->temp_dir ){
Branch: 1 branch
BranchTrueFalseTotal
if ($spawned->temp_dir) 0 311 311
131 0 rmtree( $spawned->temp_dir );
132 }
133 311 $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 774 ▶ my ($self,$pid,$orig_cmd) = @_;
Subroutine process_active: called CC=4 SCAR=13.9
Pod undocumented
185  
186 774 ▶ confess "Need a pid" unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 774 774
187  
188 774 my $logger = Log::Log4perl->get_logger;
189  
190 774 my $active = 0;
191  
192 774 ▶ if ( $self->proc_lookup->{$pid} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proc_lookup->{$pid}) 590 184 774
193  
194 590 $active = 1;
195 590 ▶ $active = 0 unless $self->proc_lookup->{$pid} =~ /taskpipe/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->proc_lookup->{$pid} =~ /taskpipe/ 354 236 590
196  
197 }
198  
199 774 return $active;
200 }
201  
202
203  
204  
205  
206 sub clean_tables{
207 419 ▶ my $self = shift;
Subroutine clean_tables: called CC=8 SCAR=20.8
Pod undocumented
208  
209 419 my $logger = Log::Log4perl->get_logger;
210 419 $logger->debug("Cleaning job table...");
211  
212 419 my $procs_rs = $self->gm->table('job')->search({});;
213  
214 # remove table rows from job and thread which no longer have active processes
215 419 ▶ while( my $proc_row = $procs_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $proc_row = $procs_rs->next) 151 419 570
216  
217 151 my $parent_active = 0;
218 151 my $threads_active = 0;
219  
220 151 $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 151 my $threads_rs;
225 151 ▶ if ( ! $parent_active ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_active) 151 0 151
226  
227 151 $threads_rs = $self->gm->table('spawned')->search({
228 process_name => 'thread',
229 job_id => $proc_row->id
230 });
231  
232 151 ▶ while( my $thread = $threads_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $thread = $threads_rs->next) 151 33 184
233 151 ▶ 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)) 118 33 151
Condition66%2/3
$thread->pid and $self->process_active($thread->pid, $proc_row->orig_cmd)
!ll&&!rl&&r
033118
MC/DC50%1/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 table66%2/3
A and B
A= $thread->pidB= $self->process_active($thread->pid, $proc_row->orig_cmd)
ABresult
0X0
100
111
234 118 $threads_active = 1;
235 118 last;
236 }
237 }
238 }
239
240 151 ▶ if ( ! $parent_active && ! $threads_active ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_active or $threads_active) 33 118 151
Condition66%2/3
$parent_active or $threads_active
l!l&&r!l&&!r
011833
MC/DC50%1/2
$parent_active or $threads_active
$parent_active $threads_active
Truth table66%2/3
A or B
A= $parent_activeB= $threads_active
ABresult
1X1
011
000
241 33 $self->stop_spawned( $proc_row->id );
242 33 $self->port_manager->clear_ports( $proc_row->id );
243 33 $threads_rs->delete;
244 33 $proc_row->delete;
245 }
246 }
247  
248 419 $logger->debug("Done cleaning job table");
249 }
250  
251  
252 sub stop_job{
253 118 ▶ my ($self,$job_id) = @_;
Subroutine stop_job: called CC=11 SCAR=24.8
Pod undocumented
254  
255 118 my $logger = Log::Log4perl->get_logger;
256  
257 118 ▶ confess "job_id must be provided" unless defined $job_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $job_id 0 118 118
258  
259 118 $self->clean_tables;
260  
261 118 my %rs;
262 118 $rs{job} = $self->gm->table('job')->search({
263 id => $job_id
264 });
265 118 $rs{thread} = $self->gm->table('spawned')->search({
266 process_name => 'thread',
267 job_id => $job_id
268 });
269  
270 118 ▶ 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 0 118 118
Condition33%1/3
$rs{"job"}->count or $rs{"thread"}->count
l!l&&r!l&&!r
11800
MC/DC0%0/2
$rs{"job"}->count or $rs{"thread"}->count
$rs{"job"}->count $rs{"thread"}->count
Truth table33%1/3
A or B
A= $rs{"job"}->countB= $rs{"thread"}->count
ABresult
1X1
011
000
271  
272 118 my $active;
273 118 my $count = 0;
274  
275 118 ▶ 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 } } 118 118 236
276  
277 236 $active = 0;
278  
279 236 my $procs_rs = $self->gm->table('job')->search({
280 id => $job_id
281 });
282
283 236 $self->proc_lookup( $self->build_proc_lookup );
284 236 ▶ while( my $proc_row = $procs_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $proc_row = $procs_rs->next) 236 236 472
285  
286 236 ▶ if ( $self->process_active($proc_row->pid) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->process_active($proc_row->pid)) 0 236 236
287 0 $logger->debug("Killing job pid ".$proc_row->pid);
288 0 $active = 1;
289 0 kill 'KILL', $proc_row->pid;
290 }
291  
292 236 my $threads_rs = $self->gm->table('spawned')->search({
293 process_name => 'thread',
294 job_id => $proc_row->id
295 });
296  
297 236 ▶ while( my $thread = $threads_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $thread = $threads_rs->next) 236 236 472
298  
299 236 ▶ if ( $thread->pid && $self->process_active($thread->pid) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($thread->pid and $self->process_active($thread->pid)) 118 118 236
Condition66%2/3
$thread->pid and $self->process_active($thread->pid)
!ll&&!rl&&r
0118118
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 118 $logger->debug("Killing thread pid ".$thread->pid);
301 118 $active = 1;
302 118 kill 'KILL', $thread->pid;
303 }
304 }
305 }
306  
307 236 $count++;
308 236 ▶ 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 236 236
309  
310 } while ( $active );
311
312 118 $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 38 __PACKAGE__->meta->make_immutable;
362 38 1;
« TaskPipe/UserAgentManager.pm ↑ Summary TaskPipe/UserAgentManager_ProxyNet.pm »