File Coverage

blib/lib/HPC/Runner/Command/watch_db.pm
Criterion Covered Total %
statement 9 58 15.5
branch 0 20 0.0
condition n/a
subroutine 3 9 33.3
pod n/a
total 12 87 13.7


line stmt bran cond sub pod time code
1             package HPC::Runner::Command::watch_db;
2              
3 1     1   22213 use MooseX::App::Command;
  1         3  
  1         19  
4 1     1   13292 use Data::Dumper;
  1         3  
  1         94  
5 1     1   8 use Log::Log4perl qw(:easy);
  1         3  
  1         17  
6              
7             extends 'HPC::Runner::Command';
8             with 'HPC::Runner::Command::Plugin::Logger::Sqlite';
9              
10             command_short_description 'Watch the sqlitedb and exit when job submissions are complete.';
11             command_long_description 'Watch the sqlitedb for one or more submission ids. This is only really useful for testing. In a real world application it is probably best to just have the scheduler email you on completion, unless you are submitting more jobs than you want emails.';
12              
13             has 'total_processes' => (
14             traits => ['Number'],
15             is => 'rw',
16             isa => 'Num',
17             default => 0,
18             handles => {
19             set_total_processes => 'set',
20             add_total_processes => 'add',
21             },
22             );
23              
24             option 'exit_on_fail' => (
25             traits => ['Bool'],
26             is => 'rw',
27             isa => 'Bool',
28             default => 0,
29             documentation => 'Fail if any jobs have an exit code besides 0 - whether all tasks have completed or not',
30             );
31              
32             option 'sleep_interval' => (
33             is => 'rw',
34             isa => 'Int',
35             default => 10,
36             documentation => 'Sleep interval in seconds to query sqlite db. For software testing you should leave as is. For longer running analyses you probably want to increase this.',
37             );
38              
39             option 'verbose' => (
40             traits => ['Bool'],
41             is => 'rw',
42             isa => 'Bool',
43             default => 0,
44             documentation => 'Enable verbose logging',
45             );
46              
47             sub execute {
48 0     0     my $self = shift;
49              
50 0 0         if($self->submission_id){
51 0           $self->app_log->info("Watching Submission Id : " . $self->submission_id);
52             }
53             else{
54 0           $self->app_log->info("No submission id specified. We will watch the whole database");
55             }
56              
57 0           my $results = $self->query_submissions;
58 0           $self->total_tasks($results);
59              
60 0           while (1){
61              
62 0 0         if($self->verbose){
63 0           $self->app_log->debug("Watching again...");
64             }
65              
66 0           my $results = $self->query_submissions;
67              
68 0           my $jobs = $results->search_related('jobs');
69 0           my $tasks = $jobs->search_related('tasks');
70              
71 0           $self->query_task($tasks);
72              
73 0           sleep ($self->sleep_interval);
74             }
75             }
76              
77             sub query_task {
78 0     0     my $self = shift;
79 0           my $task_rs = shift;
80              
81 0 0         if($self->verbose){
82 0           $self->app_log->debug("Tasks in DB are ".$task_rs->count);
83             }
84              
85             #If exit on fail we don't care if we have completed the number of processes - just fail
86 0 0         if ($self->exit_on_fail){
87 0           $self->check_exit_code($task_rs);
88             }
89              
90 0 0         if ($task_rs->count != $self->total_processes){
    0          
    0          
91             #We have
92 0           return;
93             }
94             elsif($task_rs->count == $self->total_processes){
95 0           $self->app_log->info("We have completed ".$self->total_processes." tasks. Exiting successfully");
96 0           exit 0;
97             }
98             elsif($task_rs->count >= $self->total_processes){
99 0           $self->app_log->info("More tasks were completed than were in the databases ".$self->total_processes." tasks.");
100 0           $self->app_log->info("Were jobs restarted manually?");
101 0           $self->app_log->info("Exiting successfully");
102 0           exit 0;
103             }
104             else{
105 0           $self->app_log->debug("Not sure how we got here...");
106             }
107              
108             }
109              
110             sub check_exit_code {
111 0     0     my $self = shift;
112 0           my $task_rs = shift;
113              
114 0           my $exit_codes = $task_rs->get_column('exit_code');
115              
116 0           while ( my $res = $task_rs->next ) {
117 0 0         if ($res->exit_code != 0){
118 0           $self->app_log->error("A task has failed! ".$res->task_pi);
119 0           exit 1;
120             }
121             }
122             }
123              
124             sub total_tasks {
125 0     0     my $self = shift;
126 0           my $results = shift;
127              
128 0           while ( my $res = $results->next ) {
129 0           $self->add_total_processes( $res->total_processes );
130             }
131             }
132              
133             sub query_submissions {
134 0     0     my $self = shift;
135              
136 0           my $results;
137              
138 0 0         if($self->project){
    0          
139 0           $results = $self->schema->resultset('Submission')
140             ->search( { 'project' => $self->project } );
141             }
142             elsif ($self->submission_id){
143 0           $results = $self->schema->resultset('Submission')
144             ->search( { 'submission_pi' => $self->submission_id } );
145             }
146             else{
147 0           $results = $self->schema->resultset('Submission')
148             ->search();
149             }
150              
151 0           return $results;
152              
153              
154             }
155              
156             #TODO To keep or not to keep?
157              
158             sub query_job {
159 0     0     my $self = shift;
160 0           my $job_rs = shift;
161              
162             #$job_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
163             #while ( my $res = $job_rs->next ) {
164             #print Dumper($res);
165             #}
166             }
167              
168             # sub query_related {
169             # my $self = shift;
170             #
171             # #$ENV{DBIC_TRACE} = 1;
172             #
173             # print "In query related\n";
174             # $self->schema->storage->debug(1);
175             #
176             # my $results = $self->schema->resultset('Submission')
177             # ->search( {}, { 'prefetch' => { jobs => 'tasks' } } );
178             #
179             # $results->result_class('DBIx::Class::ResultClass::HashRefInflator');
180             #
181             # while ( my $res = $results->next ) {
182             # print "Here is a result!\n";
183             # print Dumper($res);
184             # }
185             #
186             # }
187              
188             1;