blib/lib/HPC/Runner/Command/execute_job/Plugin/Logger/Sqlite.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 73 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package HPC::Runner::Command::execute_job::Plugin::Logger::Sqlite;
2  
3 use Moose::Role;
4 use Data::Dumper;
5 use DateTime;
6 use JSON;
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 my $res = $self->schema->resultset('Job')->create(
79 {
80 submission_fk => $self->submission_id,
81 start_time => $start_time,
82 exit_time => $start_time,
83 job_scheduler_id => $self->job_scheduler_id,
84 jobs_meta => $self->metastr,
85 job_name => $job_meta->{jobname}
86 }
87 );
88  
89 my $id = $res->job_pi;
90 $self->job_id($id);
91  
92 $self->$orig(@_);
93  
94 my $dt2 = DateTime->now( time_zone => 'local' );
95 $ymd = $dt2->ymd();
96 $hms = $dt2->hms();
97 my $end_time = "$ymd $hms";
98 my $duration = $dt2 - $dt1;
99 my $format = DateTime::Format::Duration->new( pattern =>
100 '%e days, %H hours, %M minutes, %S seconds' );
101  
102 $duration = $format->format_duration($duration);
103  
104 $res->update( { exit_time => $end_time, duration => $duration } );
105 };
106  
107 around 'start_command_log' => sub {
108 my $orig = shift;
109 my $self = shift;
110 my $cmdpid = shift;
111  
112 my $res = $self->schema->resultset('Task')->create(
113 {
114 job_fk => $self->job_id,
115 pid => $cmdpid,
116 start_time => $self->table_data->{start_time},
117 }
118 );
119  
120 $self->$orig($cmdpid);
121  
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;