bin/waitjobs

filter: Statementstmt 41.0% 57 / 139 Branchbran 4.0% 2 / 50 Conditioncond 3.0% 1 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 71.4% 15 / 21 Podpod n/a totaltotal 28.7% 75 / 261 CC 24 SCAR 45.0
CC
24
Cov
27%
submit_self
240.0
derive_jobname
42.0
update_queue
20.0
CRAP 90.1
« bin/lsjobs ↑ Summary blib/lib/NBI/Opts.pm »
1 #!/usr/bin/env perl
2 #ABSTRACT: Wait for selected jobs to exit the queue
3 #PODNAME: waitjobs
4  
5 1 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use FindBin qw($RealBin);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶if (-e "$RealBin/../dist.ini") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$main::RealBin/../dist.ini") 1 0 1
15 1 ▶ say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 1 1
16 1 ▶ use lib "$RealBin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 }
18 1 ▶use NBI::Slurm;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use NBI::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use NBI::Opts;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use NBI::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 $Data::Dumper::Sortkeys = 1;
23  
24 1 my $opt_user = $ENV{USER};
25 1 my $opt_name = '.+';
26 1 my $opt_status = '';
27 1 my $opt_verbose_bool = 0;
28 1 my $opt_debug_bool = 0;
29 1 my $refresh_time = 20;
30 1 my $opt_submit = 0; # resubmit this waiter to SLURM instead of waiting in the foreground
31 1 my $opt_jobname; # override the derived "wait-<name>" job name
32 1 my $opt_jobtime = '1d'; # walltime for the submitted waiter job
33 # Check if this script is running as a job, in case save the job id
34 1 ▶my $self_job_id = $ENV{SLURM_JOB_ID} // undef;
Condition50%1/2
$ENV{"SLURM_JOB_ID"} // undef
l!l
01
MC/DC0%0/1
$ENV{"SLURM_JOB_ID"} // undef
$ENV{"SLURM_JOB_ID"}
Truth table50%1/2
A
A= $ENV{"SLURM_JOB_ID"}
Aresult
11
00
35 GetOptions(
36 'u|user=s' => \$opt_user,
37 'n|name=s' => \$opt_name,
38 's|status=s' => \$opt_status,
39 'r|refresh=i' => \$refresh_time,
40 'S|submit' => \$opt_submit,
41 'jobname=s' => \$opt_jobname,
42 'T|jobtime=s' => \$opt_jobtime,
43 'verbose' => \$opt_verbose_bool,
44 1 ▶ 'version' => sub { say "waitjobs v", $NBI::Slurm::VERSION; exit },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 'debug' => \$opt_debug_bool,
46 0 ▶ 'help' => sub { usage() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 1 );
48  
49 # Allow the job name to be passed as a positional argument: waitjobs [options] [job_name]
50 0 ▶$opt_name = $ARGV[0] if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
51  
52 # --submit: instead of waiting in the foreground, submit this very command as a
53 # small SLURM job (1 core, 4GB) that does the waiting, then exit printing its job id.
54 0 ▶if ($opt_submit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_submit) 0 0 0
55 0 submit_self();
56 0 exit 0;
57 }
58  
59 # Build the queue-selection arguments once and reuse them on every refresh.
60 # Only pass -state when a status filter was requested: NBI::Queue validates the
61 # value against the list of SLURM short codes and would die on an empty string.
62 0 my @queue_args = (
63 -username => $opt_user,
64 -name => $opt_name,
65 );
66 0 ▶push @queue_args, (-state => $opt_status) if length $opt_status;
Branch: 1 branch
BranchTrueFalseTotal
if length $opt_status 0 0 0
67  
68 0 my $queue = NBI::Queue->new(@queue_args);
69 # If $self_job_id is anq integer, remove it from the queue
70 0 ▶if (defined $self_job_id and $self_job_id =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self_job_id and $self_job_id =~ /^\d+$/u) 0 0 0
Condition0%0/3
defined $self_job_id and $self_job_id =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self_job_id and $self_job_id =~ /^\d+$/u
defined $self_job_id $self_job_id =~ /^\d+$/u
Truth table0%0/3
A and B
A= defined $self_job_idB= $self_job_id =~ /^\d+$/u
ABresult
0X0
100
111
71 0 print STDERR timelog("waitjobs"), "Ignoring current job id $self_job_id\n";
72 0 $queue->remove($self_job_id);
73 }
74  
75 0 my @starting_ids = @{$queue->ids()};
76 0 ▶print STDERR timelog("waitjobs"), "There are ",
Branch: 1 branch
BranchTrueFalseTotal
length $opt_status ? : 0 0 0
77 GREEN, BOLD, scalar @starting_ids, RESET, " jobs for ",
78 GREEN, BOLD, $opt_user, RESET, " with name matching ",
79 GREEN, BOLD, $opt_name, RESET,
80 (length $opt_status ? (" and status ", GREEN, BOLD, $opt_status, RESET) : ()),
81 "\n";
82 0 ▶if ($opt_debug_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_debug_bool) 0 0 0
83 0 say STDERR timelog("waitjobs"), "[debug] Initial job id(s): ", join(",", @starting_ids);
84 }
85  
86 0 ▶while (scalar @starting_ids > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @starting_ids > 0) 0 0 0
87 # Create the new queue with the same parameters as the initial queue
88 0 my $current_jobs = NBI::Queue->new(@queue_args);
89 0 my @current_ids = @{$current_jobs->ids()};
90  
91 # Remove from @starting_ids all the jobs that are NOT in @current_ids
92 0 my @new_starting_ids = ();
93 0 for my $starting_id (@starting_ids) {
94 0 ▶ if (grep {$_ eq $starting_id} @current_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $starting_id;} @current_ids) 0 0 0
95 0 push(@new_starting_ids, $starting_id);
96 }
97 }
98 0 @starting_ids = @new_starting_ids;
99 0 ▶ if ($opt_debug_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_debug_bool) 0 0 0
100 0 say STDERR timelog("waitjobs"), "[debug] Tracking ", scalar @starting_ids,
101 " job id(s): ", join(",", @starting_ids);
102 }
103 0 ▶ if ($opt_verbose_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_verbose_bool) 0 0 0
104 0 say STDERR timelog("waitjobs"), "Waiting for ", scalar @starting_ids, " jobs to finish (refresh in $refresh_time seconds)";
105 }
106 0 sleep $refresh_time;
107 }
108  
109  
110 0 say STDERR timelog("waitjobs"), "No jobs left: quitting\n";
111  
112 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
113 0 say STDERR<<END;
114  
115 Usage: waitjobs [options] [job_name]
116 ------------------------------------
117 Wait for selected jobs to exit the queue
118  
119 Options:
120 -u, --user Username to wait for (default: current user)
121 -n, --name Job name pattern, as a regex (default '.+', matches any name).
122 Can also be supplied as the positional [job_name] argument.
123 -s, --status Only wait for jobs in this SLURM state, given as a short
124 code (e.g. R, PD, CG). Default: any state.
125 -r, --refresh Seconds to wait between queue checks (default: 20)
126 -S, --submit Submit this waiter as a SLURM job (1 core, 4GB) and exit,
127 printing the job id. The job does the waiting for you.
128 --jobname STR Name for the submitted waiter job (default: wait-<name>)
129 -T, --jobtime Walltime for the submitted waiter job (default: 1d)
130 --verbose Print a status line at every refresh
131 --debug Print the job ids being tracked at every refresh
132 --version Print the version and exit
133 --help Print this help and exit
134  
135 END
136 0 exit;
137 }
138  
139 # Derive a tidy job name from the name pattern, e.g. "concat" -> "wait-concat".
140 0 ▶sub derive_jobname {
Subroutine derive_jobname: not called CC=6 SCAR=37.4
141 0 my ($pattern) = @_;
142 0 ▶ return "wait-all" if !defined $pattern || $pattern eq '' || $pattern eq '.+' || $pattern eq '.*';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pattern or $pattern eq "" or $pattern eq ".+" or $pattern eq ".*" 0 0 0
Condition0%0/9
not defined $pattern or $pattern eq "" or $pattern eq ".+" or $pattern eq ".*"
l!l&&r!l&&!r
000
not defined $pattern or $pattern eq "" or $pattern eq ".+"
l!l&&r!l&&!r
000
not defined $pattern or $pattern eq ""
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $pattern or $pattern eq "" or $pattern eq ".+" or $pattern eq ".*"
not defined $pattern $pattern eq "" $pattern eq ".+" $pattern eq ".*"
Truth table0%0/5
not(A) or B or C or D
A= not defined $patternB= $pattern eq ""C= $pattern eq ".+"D= $pattern eq ".*"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
143 0 my $s = $pattern;
144 0 $s =~ s/[^A-Za-z0-9._-]+/-/g; # turn regex metacharacters / spaces into dashes
145 0 $s =~ s/^[-.]+//;
146 0 $s =~ s/[-.]+$//;
147 0 ▶ return "wait-all" if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 0 0
148 0 return "wait-$s";
149 }
150  
151 # Single-quote a string for safe inclusion in a shell command line.
152 0 ▶sub shell_quote {
Subroutine shell_quote: not called CC=2 SCAR=17.9
153 0 my ($s) = @_;
154 0 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 0 0
155 0 $s =~ s/'/'\\''/g;
156 0 return "'$s'";
157 }
158  
159 # If the queue name contains a '*', expand it to short/medium/long based on walltime.
160 0 ▶sub update_queue {
Subroutine update_queue: not called CC=4 SCAR=30.0
161 0 my ($queue, $time_hours) = @_;
162 0 ▶ return $queue if $queue !~ /\*/;
Branch: 1 branch
BranchTrueFalseTotal
unless $queue =~ /\*/u 0 0 0
163 0 ▶ if ($time_hours <= 2) { $queue =~ s/\*/short/g; }
Branch: 2 branches
BranchTrueFalseTotal
if ($time_hours <= 2) { } 0 0 0
elsif ($time_hours <= 48) { } 0 0 0
164 0 elsif ($time_hours <= 48) { $queue =~ s/\*/medium/g; }
165 0 else { $queue =~ s/\*/long/g; }
166 0 return $queue;
167 }
168  
169 # Build an equivalent waitjobs command (minus --submit) and submit it as a SLURM job.
170 0 ▶sub submit_self {
Subroutine submit_self: not called CC=15 SCAR=54.8
171 0 ▶ my $home = $ENV{HOME} // (getcwd());
Condition0%0/3
$ENV{"HOME"} // getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"HOME"} // getcwd()
$ENV{"HOME"} getcwd()
Truth table0%0/3
A // B
A= $ENV{"HOME"}B= getcwd()
ABresult
1X1
011
000
172 0 my $config = NBI::Slurm::load_config("$home/.nbislurm.config");
173  
174 0 ▶ my $queue = $config->{queue} // 'qib-short';
Condition0%0/2
$config->{"queue"} // "qib-short"
l!l
00
MC/DC0%0/1
$config->{"queue"} // "qib-short"
$config->{"queue"}
Truth table0%0/2
A
A= $config->{"queue"}
Aresult
11
00
175 0 ▶ my $tmpdir = $config->{tmpdir} // '/tmp';
Condition0%0/2
$config->{"tmpdir"} // "/tmp"
l!l
00
MC/DC0%0/1
$config->{"tmpdir"} // "/tmp"
$config->{"tmpdir"}
Truth table0%0/2
A
A= $config->{"tmpdir"}
Aresult
11
00
176 0 my $time_hours = NBI::Opts::_time_to_hour($opt_jobtime);
177 0 $queue = update_queue($queue, $time_hours);
178  
179 0 ▶ my $jobname = defined $opt_jobname ? $opt_jobname : derive_jobname($opt_name);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt_jobname ? : 0 0 0
180  
181 # Reconstruct the foreground command from the parsed options (NOT @ARGV), so the
182 # submitted job never carries --submit and cannot resubmit itself in a loop.
183 0 ▶ my $self_exe = abs_path($0) // File::Spec->catfile($RealBin, basename($0));
Condition0%0/3
abs_path($0) // "File::Spec"->catfile($main::RealBin, basename($0))
l!l&&r!l&&!r
000
MC/DC0%0/2
abs_path($0) // "File::Spec"->catfile($main::RealBin, basename($0))
abs_path($0) "File::Spec"->catfile($main::RealBin, basename($0))
Truth table0%0/3
A // B
A= abs_path($0)B= "File::Spec"->catfile($main::RealBin, basename($0))
ABresult
1X1
011
000
184 0 my @cmd = (shell_quote($self_exe));
185 0 push @cmd, '-u', shell_quote($opt_user);
186 0 push @cmd, '-n', shell_quote($opt_name);
187 0 ▶ push @cmd, '-s', shell_quote($opt_status) if length $opt_status;
Branch: 1 branch
BranchTrueFalseTotal
if length $opt_status 0 0 0
188 0 push @cmd, '-r', $refresh_time;
189 0 ▶ push @cmd, '--verbose' if $opt_verbose_bool;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_verbose_bool 0 0 0
190 0 ▶ push @cmd, '--debug' if $opt_debug_bool;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_debug_bool 0 0 0
191 0 my $wait_command = join(' ', @cmd);
192  
193 0 my $cwd = getcwd();
194 0 my $opts = NBI::Opts->new(
195 -queue => $queue,
196 -threads => 1,
197 -memory => '4GB',
198 -time => $opt_jobtime,
199 -tmpdir => $tmpdir,
200 );
201 0 my $job = NBI::Job->new(
202 -name => $jobname,
203 -command => "cd " . shell_quote($cwd),
204 -opts => $opts,
205 );
206 0 $job->append_command($wait_command);
207  
208 0 ▶ if ($opt_verbose_bool || $opt_debug_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_verbose_bool or $opt_debug_bool) 0 0 0
Condition0%0/3
$opt_verbose_bool or $opt_debug_bool
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt_verbose_bool or $opt_debug_bool
$opt_verbose_bool $opt_debug_bool
Truth table0%0/3
A or B
A= $opt_verbose_boolB= $opt_debug_bool
ABresult
1X1
011
000
209 0 say STDERR timelog("waitjobs"), "Submitting waiter job '$jobname' to queue '$queue' (walltime $opt_jobtime)";
210 0 ▶ say STDERR timelog("waitjobs"), "[debug] Command: $wait_command" if $opt_debug_bool;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_debug_bool 0 0 0
211 }
212  
213 0 my $jobid = $job->run();
214 0 ▶ if (defined $jobid && $jobid =~ /^\d+$/ && $jobid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jobid and $jobid =~ /^\d+$/u and $jobid > 0) { } 0 0 0
Condition0%0/6
defined $jobid and $jobid =~ /^\d+$/u and $jobid > 0
!ll&&!rl&&r
000
defined $jobid and $jobid =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/3
defined $jobid and $jobid =~ /^\d+$/u and $jobid > 0
defined $jobid $jobid =~ /^\d+$/u $jobid > 0
Truth table0%0/4
A and B and C
A= defined $jobidB= $jobid =~ /^\d+$/uC= $jobid > 0
ABCresult
0XX0
10X0
1100
1111
215 0 say STDERR timelog("waitjobs"), GREEN, BOLD, "Submitted waiter job $jobid ($jobname)", RESET;
216 0 say $jobid; # to STDOUT so it can be captured: JOBID=$(waitjobs -n concat -S)
217 } else {
218 0 say STDERR timelog("waitjobs"), RED, BOLD, "ERROR:", RESET,
219 " could not submit waiter job (sbatch not found, or you are outside a SLURM cluster?)";
220 0 exit 1;
221 }
222 }
223  
224 __END__
« bin/lsjobs ↑ Summary blib/lib/NBI/Opts.pm »