File Coverage

blib/lib/HPC/Runner/Command/execute_job/Plugin/Logger/Sqlite.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package HPC::Runner::Command::execute_job::Plugin::Logger::Sqlite;
2              
3 2     2   1693718 use Moose::Role;
  2         10  
  2         26  
4 2     2   11700 use Data::Dumper;
  2         6  
  2         142  
5 2     2   14 use DateTime;
  2         10  
  2         52  
6 2     2   12 use JSON;
  2         4  
  2         30  
7              
8             with 'HPC::Runner::Command::Plugin::Logger::Sqlite';
9              
10             =head1 HPC::Runner::Command::execute_job::Plugin::Logger::Sqlite;
11              
12             =cut
13              
14             =head2 Attributes
15              
16             =cut
17              
18             =head3 job_id
19              
20             This is the ID for the hpcrunner.pl execute_job
21              
22             =cut
23              
24             has 'job_id' => (
25             is => 'rw',
26             isa => 'Str|Int',
27             lazy => 1,
28             default => '',
29             predicate => 'has_job_id',
30             clearer => 'clear_job_id'
31             );
32              
33             =head2 Subroutines
34              
35             =cut
36              
37             =head3 after log_table
38              
39             Log the table data to the sqlite DB
40              
41             Each start, stop time for the whole batch gets logged
42              
43             Each start, stop time for each task gets logged
44              
45             =cut
46              
47             #$sql =<<EOF;
48             #CREATE TABLE IF NOT EXISTS jobs (
49             #jobs_pi INTEGER PRIMARY KEY NOT NULL,
50             #submission_id integer NOT NULL,
51             #start_time varchar(20) NOT NULL,
52             #exit_time varchar(20) NOT NULL,
53             #jobs_meta text,
54              
55             ##TODO Add Moose object with starttime, endtime, duration
56              
57             around 'run_mce' => sub {
58             my $orig = shift;
59             my $self = shift;
60              
61             $self->deploy_schema;
62              
63             my $dt1 = DateTime->now( time_zone => 'local' );
64             my $ymd = $dt1->ymd();
65             my $hms = $dt1->hms();
66             my $start_time = "$ymd $hms";
67              
68             my $job_meta = {};
69              
70             if ( $self->metastr ) {
71             $job_meta = decode_json( $self->metastr );
72             }
73              
74             if ( !exists $job_meta->{jobname} ) {
75             $job_meta->{jobname} = 'undefined';
76             }
77              
78             ##TODO update for running in single node mode
79             my $res = $self->schema->resultset('Job')->create(
80             {
81             submission_fk => $self->submission_id,
82             start_time => $start_time,
83             exit_time => $start_time,
84             job_scheduler_id => $self->job_scheduler_id,
85             jobs_meta => $self->metastr,
86             job_name => $job_meta->{jobname}
87             }
88             );
89              
90             my $id = $res->job_pi;
91             $self->job_id($id);
92              
93             $self->$orig(@_);
94              
95             my $dt2 = DateTime->now( time_zone => 'local' );
96             $ymd = $dt2->ymd();
97             $hms = $dt2->hms();
98             my $end_time = "$ymd $hms";
99             my $duration = $dt2 - $dt1;
100             my $format = DateTime::Format::Duration->new( pattern =>
101             '%e days, %H hours, %M minutes, %S seconds' );
102              
103             $duration = $format->format_duration($duration);
104              
105             $res->update( { exit_time => $end_time, duration => $duration } );
106             };
107              
108             around 'start_command_log' => sub {
109             my $orig = shift;
110             my $self = shift;
111             my $cmdpid = shift;
112              
113             my $res = $self->schema->resultset('Task')->create(
114             {
115             job_fk => $self->job_id,
116             pid => $cmdpid,
117             start_time => $self->table_data->{start_time},
118             }
119             );
120              
121             $self->$orig($cmdpid);
122             };
123              
124             around 'log_table' => sub {
125             my $orig = shift;
126             my $self = shift;
127              
128             $self->$orig(@_);
129              
130             my $tags = "";
131             if ( exists $self->table_data->{task_tags} ) {
132             my $task_tags = $self->table_data->{task_tags};
133             if ($task_tags) {
134             $tags = $task_tags;
135             }
136             }
137              
138             my $started_task = $self->schema->resultset('Task')->find(
139             {
140             job_fk => $self->job_id,
141             pid => $self->table_data->{cmdpid},
142             start_time => $self->table_data->{start_time},
143             }
144             );
145              
146             $started_task->update(
147             {
148             exit_time => $self->table_data->{exit_time},
149             duration => $self->table_data->{duration},
150             exit_code => $self->table_data->{exitcode},
151             task_tags => $tags,
152             }
153             );
154             };
155              
156             1;