File Coverage

blib/lib/HPC/Runner/Command/submit_jobs/Logger/JSON.pm
Criterion Covered Total %
statement 27 90 30.0
branch 0 6 0.0
condition 0 4 0.0
subroutine 9 12 75.0
pod 3 3 100.0
total 39 115 33.9


line stmt bran cond sub pod time code
1             package HPC::Runner::Command::submit_jobs::Logger::JSON;
2              
3 1     1   1094 use Moose::Role;
  1         3  
  1         6  
4 1     1   5145 use namespace::autoclean;
  1         3  
  1         11  
5              
6             with 'HPC::Runner::Command::execute_job::Logger::Lock';
7              
8 1     1   118 use JSON;
  1         2  
  1         9  
9 1     1   121 use File::Spec;
  1         4  
  1         46  
10 1     1   9 use Data::UUID;
  1         4  
  1         81  
11 1     1   7 use File::Path qw(make_path remove_tree);
  1         2  
  1         42  
12 1     1   5 use File::Slurp;
  1         2  
  1         54  
13 1     1   6 use DateTime;
  1         2  
  1         20  
14 1     1   5 use Capture::Tiny ':all';
  1         1  
  1         721  
15              
16             =head3 create_json_submission
17              
18             Create the data for the json submission
19              
20             =cut
21              
22             sub create_json_submission {
23 0     0 1   my $self = shift;
24              
25 0           $self->logger('app_log');
26 0           my $hpc_meta = $self->gen_hpc_meta;
27              
28 0           my $json_text = encode_json $hpc_meta;
29              
30             # We are rolling back this functionality for a future release
31             # my $basename = $self->data_tar->basename('.tar.gz');
32             # my $submission_file = File::Spec->catdir( $basename, 'submission.json' );
33             #
34             # $self->check_lock;
35             # $self->write_lock;
36             #
37             # $self->archive->add_data( $submission_file, $json_text );
38             # capture {
39             # $self->archive->write( $self->data_tar, 1 );
40             # };
41             # $self->lock_file->remove;
42              
43 0           return $hpc_meta;
44             }
45              
46             =head3 update_json_submission
47              
48             Take the initial submission and update it to contain the hpcmeta
49              
50             We only rerun this here to get the submission status
51              
52             =cut
53              
54             sub update_json_submission {
55 0     0 1   my $self = shift;
56              
57 0           my $hpc_meta = $self->gen_hpc_meta;
58 0           my $json_text = encode_json $hpc_meta;
59              
60             # We are rolling back this functionality for a future release
61             #
62             # my $basename = $self->data_tar->basename('.tar.gz');
63             # my $submission_file = File::Spec->catdir( $basename, 'submission.json' );
64             #
65             # ##TODO Change this to around?
66             # $self->check_lock;
67             # $self->write_lock;
68             #
69             # $self->archive->add_data( $submission_file, $json_text );
70             # capture {
71             # $self->archive->write( $self->data_tar, 1 );
72             # };
73             #
74             # $self->lock_file->remove;
75 0           my $file_name = File::Spec->catdir( $self->logdir, 'submission.json' );
76 0           $self->_make_the_dirs( $self->logdir );
77 0           write_file($file_name, $json_text);
78              
79 0           return $hpc_meta;
80              
81             }
82              
83             =head3 gen_hpc_meta
84              
85             Generate the HPC meta from the submission
86              
87             #TODO Check for batches
88              
89             =cut
90              
91             sub gen_hpc_meta {
92 0     0 1   my $self = shift;
93              
94 0           my $hpc_meta = {};
95 0           $hpc_meta->{uuid} = $self->submission_uuid;
96 0 0         $hpc_meta->{project} = $self->project if $self->has_project;
97 0           my $dt = DateTime->now( time_zone => 'local' );
98 0           $hpc_meta->{submission_time} = "$dt";
99 0           $hpc_meta->{jobs} = [];
100 0           $hpc_meta->{submissions} = {};
101 0           $hpc_meta->{schedule} = $self->schedule;
102              
103 0           foreach my $job ( $self->all_schedules ) {
104 0           my $job_obj = {};
105              
106             #Dependencies
107 0           my $ref = $self->graph_job_deps->{$job};
108 0           my $depstring = join( ", ", @{$ref} );
  0            
109 0           my $count_cmd = $self->jobs->{$job}->cmd_counter;
110 0           my $mem = $self->jobs->{$job}->mem;
111 0           my $cpus = $self->jobs->{$job}->cpus_per_task;
112 0           my $walltime = $self->jobs->{$job}->walltime;
113 0           my $cmd_start = $self->jobs->{$job}->{cmd_start};
114 0   0       my $submission_stat = $self->jobs->{$job}->submission_failure || 0;
115              
116 0           $job_obj->{job} = $job;
117 0           $job_obj->{deps} = $depstring;
118 0           $job_obj->{total_tasks} = $count_cmd;
119 0           $job_obj->{walltime} = $walltime;
120 0           $job_obj->{cpus_per_task} = $cpus;
121 0           $job_obj->{mem} = $mem;
122 0           $job_obj->{cmd_start} = $cmd_start;
123 0           $job_obj->{cmd_end} = $cmd_start + $count_cmd;
124 0           $job_obj->{schedule} = [];
125              
126             #I think this should be scheduler_ids
127 0           for ( my $x = 0 ; $x < $self->jobs->{$job}->{num_job_arrays} ; $x++ ) {
128              
129 0           my $obj = {};
130              
131             #index start, index end
132 0 0         next unless $self->jobs->{$job}->batch_indexes->[$x];
133              
134             my $batch_start =
135 0           $self->jobs->{$job}->batch_indexes->[$x]->{'batch_index_start'};
136             my $batch_end =
137 0           $self->jobs->{$job}->batch_indexes->[$x]->{'batch_index_end'};
138 0           my $len = ( $batch_end - $batch_start ) + 1;
139              
140 0           my $logname = $self->jobs->{$job}->lognames->[$x];
141 0 0         if ($logname) {
142             ##TASK IDS ARE 0 INDEXED
143 0           $hpc_meta->{submissions}->{$logname}->{jobname} = $job;
144             ##The entire job
145             $hpc_meta->{submissions}->{$logname}->{job_task_index_start} =
146 0           $cmd_start + 0;
147             $hpc_meta->{submissions}->{$logname}->{job_task_index_end} =
148 0           $cmd_start + $count_cmd - 1;
149              
150             ##This particular batch
151             ##These are 1 indexed to match the command line parameters 1
152             ##TODO clean up 0/1 indexed!!
153             $hpc_meta->{submissions}->{$logname}->{batch_index_start} =
154 0           $batch_start;
155             $hpc_meta->{submissions}->{$logname}->{batch_index_end} =
156 0           $batch_end;
157             }
158              
159 0   0       my $scheduler_id = $self->jobs->{$job}->scheduler_ids->[$x] || '0';
160 0           $obj->{task_indices} = "$batch_start-$batch_end";
161 0           $obj->{total_tasks} = $len;
162 0           $obj->{scheduler_id} = $scheduler_id;
163              
164 0           push( @{ $job_obj->{schedule} }, $obj );
  0            
165             }
166              
167 0           push( @{ $hpc_meta->{jobs} }, $job_obj );
  0            
168             }
169              
170 0           return $hpc_meta;
171             }
172              
173             1;