blib/lib/NBI/Slurm.pm

filter: Statementstmt 47.7% 53 / 111 Branchbran 26.1% 11 / 42 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 7 / 7 totaltotal 45.3% 83 / 183 CC 25 SCAR 29.1
CC
25
Cov
42%
load_config
52.2
days_since_update
12.0
execute_command
9.4
CRAP 18.4
« blib/lib/NBI/Test.pl ↑ Summary lib/NBI/Launcher/Kraken2.pm »
1 #ABSTRACT: NBI Slurm module
2 #
3 # NBI::Slurm - Main entry-point module for the NBI::Slurm package.
4 #
5 # DESCRIPTION:
6 # Provides utility functions and constants used across the package:
7 # - %FORMAT_STRINGS : squeue format codes (jobid, user, memory, etc.)
8 # - load_config() : reads ~/.nbislurm.config key=value settings
9 # - has_squeue() : checks whether the squeue binary is available
10 # - queues() : lists available SLURM partitions via sinfo
11 # - valid_queue() : validates a queue name against the cluster
12 # - execute_command(): runs a shell command and captures stdout/stderr/exit
13 # - timelog() : returns a formatted timestamp string for logging
14 # - days_since_update(): returns days since a file was last modified
15 #
16 # RELATIONSHIPS:
17 # - Loads and re-exports NBI::Job and NBI::Opts so callers only need
18 # "use NBI::Slurm".
19 # - NBI::Queue and NBI::QueuedJob both import %FORMAT_STRINGS and
20 # $NBI::Slurm::VERSION from this module.
21 # - NBI::EcoScheduler uses $NBI::Slurm::VERSION and is called by
22 # bin/runjob when eco scheduling is active.
23 #
24 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 package NBI::Slurm;
28 23 ▶use NBI::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 23 ▶use NBI::Opts;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 23 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 23 our @ISA = qw(Exporter);
32 23 our @EXPORT = qw(Job Opts load_config has_squeue timelog execute_command %FORMAT_STRINGS);
33  
34 23 $NBI::Slurm::VERSION = '0.22.0';
35  
36  
37  
38  
39 23 our %FORMAT_STRINGS = (
40 'account' => '%a',
41 'jobid' => '%A',
42 'jobname' => '%j',
43 'cpus' => '%C',
44 'end_time' => '%E',
45 'start_time' => '%S',
46 'total_time' => '%l',
47 'time_left' => '%L',
48 'memory' => '%m',
49 'command' => '%o',
50 'queue' => '%P',
51 'reason' => '%r',
52 'status' => '%T', # short: %t
53 'workdir' => '%Z',
54 'user' => '%u',
55 );
56  
57 0 ▶sub execute_command {
Subroutine execute_command: not called CC=3 SCAR=22.4
Pod documented
58 0 my ($command) = @_;
59
60 # Use File::Temp functionality available in core Perl
61 23 ▶ use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
62
63 # Create temporary files for capturing output
64 0 my ($stdout_fh, $stdout_file) = tempfile(UNLINK => 1);
65 0 my ($stderr_fh, $stderr_file) = tempfile(UNLINK => 1);
66 0 close($stdout_fh); # Close handles so system() can write to files
67 0 close($stderr_fh);
68
69 # Execute command with output redirection
70 0 my $full_command = "$command >$stdout_file 2>$stderr_file";
71 0 system($full_command);
72
73 # Capture exit code (system() returns exit code << 8)
74 0 my $exit_code = $? >> 8;
75
76 # Read stdout
77 0 my $stdout = '';
78 0 ▶ if (open(my $stdout_read_fh, '<', $stdout_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $stdout_read_fh, "<", $stdout_file) 0 0 0
79 0 $stdout = do { local $/; <$stdout_read_fh> };
80 0 close($stdout_read_fh);
81 }
82
83 # Read stderr
84 0 my $stderr = '';
85 0 ▶ if (open(my $stderr_read_fh, '<', $stderr_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $stderr_read_fh, "<", $stderr_file) 0 0 0
86 0 $stderr = do { local $/; <$stderr_read_fh> };
87 0 close($stderr_read_fh);
88 }
89
90 # Files are automatically cleaned up due to UNLINK => 1
91
92 return {
93 0 stdout => $stdout,
94 stderr => $stderr,
95 exit_code => $exit_code
96 };
97 }
98  
99 5 ▶sub load_config {
Subroutine load_config: called CC=10 SCAR=39.5
Pod documented
100 5 my $filename = shift;
101 5 ▶ if (! $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 5 5
102 0 $filename = "$ENV{HOME}/.nbislurm.config";
103 }
104 5 my $config = {};
105 5 ▶ if (! -e "$filename") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$filename") 5 0 5
106 5 ▶ say STDERR "# Config file not found: $filename" if ($ENV{"DEBUG"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 5 5
107 5 return $config;
108 }
109 0 ▶ open(my $fh, "<", $filename) or die "Cannot open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Cannot open ${filename}: $!" 0 0 0
110 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
111 0 chomp;
112 0 ▶ next if (/^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
113 0 ▶ next if (/^#/);
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 0 0
114 0 ▶ next if (/^;/);
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 0 0 0
115 0 my ($key, $value) = split(/=/, $_);
116 # discard keys with spaces
117 0 ▶ next if ($key =~ /\s/);
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\s/ 0 0 0
118 0 $config->{$key} = $value;
119 }
120 0 close $fh;
121 0 return $config;
122 }
123  
124  
125 2 ▶sub has_squeue {
Subroutine has_squeue: called CC=2 SCAR=7.4
Pod documented
126 2 my $cmd = "squeue --version";
127 2 my $output = `$cmd 2>&1`;
128 2 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 2 2
129 0 return 1;
130 } else {
131 2 return 0;
132 }
133 }
134  
135 2 ▶sub queues {
Subroutine queues: called CC=6 SCAR=20.4
Pod documented
136 2 my $can_fail = shift;
137 # Retrieve queues from SLURM
138 2 my $has_sinfo = undef;
139 2 eval {
140 2 $has_sinfo = `sinfo --version > /dev/null 2>&1`;
141 };
142
143 2 ▶ chomp($has_sinfo) if defined $has_sinfo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $has_sinfo 2 0 2
144 2 ▶ if (not defined $has_sinfo and ! $can_fail) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $has_sinfo or $can_fail) 0 2 2
Condition33%1/3
defined $has_sinfo or $can_fail
l!l&&r!l&&!r
200
MC/DC0%0/2
defined $has_sinfo or $can_fail
defined $has_sinfo $can_fail
Truth table33%1/3
A or B
A= defined $has_sinfoB= $can_fail
ABresult
1X1
011
000
145 0 Carp::croak "ERROR NBI::Slurm: sinfo failed. Are you in a SLURM cluster?\n";
146 }
147 2 my $cmd = "timeout 5s sinfo --format '%P' --noheader";
148 2 my @output = `$cmd 2>/dev/null`;
149 2 ▶ if ($? != 0 and ! $can_fail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0 and not $can_fail) 0 2 2
Condition33%1/3
$? != 0 and not $can_fail
!ll&&!rl&&r
020
MC/DC0%0/2
$? != 0 and not $can_fail
$? != 0 not $can_fail
Truth table33%1/3
A and not(B)
A= $? != 0B= not $can_fail
ABresult
0X0
100
111
150 0 Carp::croak "ERROR NBI::Slurm: sinfo did not find queues. Are you in a SLURM cluster?\n";
151 }
152
153 2 chomp @output;
154 2 return @output;
155 }
156  
157 2 ▶sub valid_queue {
Subroutine valid_queue: called CC=5 SCAR=17.1
Pod documented
158 2 ▶ if ($ENV{'SKIP_SLURM_CHECK'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SKIP_SLURM_CHECK"}) 1 1 2
159 1 return 1;
160 }
161 1 my $queue = shift;
162 1 my @queues = queues('CAN_FAIL');
163 1 my @input_queues = split(/,/, $queue);
164  
165 1 ▶ if (scalar(@input_queues) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @input_queues == 0) 0 1 1
166 # Let's assume it exists... TODO CHECK
167 0 return 1;
168 }
169 1 foreach my $input_queue (@input_queues) {
170 0 ▶ if (! grep { $_ eq $input_queue } @queues) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $input_queue;} @queues) 1 0 1
171 1 return 0;
172 }
173 }
174 0 return 1;
175 }
176  
177  
178 0 ▶sub days_since_update {
Subroutine days_since_update: not called CC=3 SCAR=24.8
Pod documented
179 0 my $file_path = shift;
180  
181 # Check if the required modules can be loaded
182 0 eval {
183 0 require File::Spec;
184 0 require Time::Piece;
185 0 require Time::Seconds;
186 };
187 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
188 0 return -1; # Failed to load required module(s)
189 }
190  
191 # Check if the file exists
192 0 ▶ unless (-e $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file_path) 0 0 0
193 0 return -1; # File not found
194 }
195  
196 # Get the file's last modification time
197 0 my $last_modified = (stat($file_path))[9];
198  
199 # Calculate the number of days since the last modification
200 0 my $current_time = time();
201 0 my $days_since_update = int(($current_time - $last_modified) / (24 * 60 * 60));
202  
203 0 return $days_since_update;
204 }
205  
206 0 ▶sub timelog {
Subroutine timelog: not called CC=2 SCAR=17.9
Pod documented
207 0 my $tag = shift;
208 0 ▶ $tag = "nbi-slurm" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 0 0
209 0 my ($sec, $min, $hour, $day, $month, $year) = localtime(time);
210 0 $year += 1900; # Adjust year (localtime returns years since 1900)
211 0 $month += 1; # Adjust month (localtime returns 0-11)
212
213 # Format with leading zeros
214 0 return sprintf("[%s:%04d-%02d-%02d %02d:%02d:%02d]\t",
215 $tag, $year, $month, $day, $hour, $min, $sec);
216 }
217  
218 23 1;
219  
220 __END__
« blib/lib/NBI/Test.pl ↑ Summary lib/NBI/Launcher/Kraken2.pm »