| 1 | #ABSTRACT: NBI::Queue, to filter jobs in the queue | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # NBI::Queue - Queries the live SLURM queue and returns a filtered job list. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION: | ||||||||||||||||||||||||||||
| 6 | # Wraps a call to `squeue` and parses its output into a list of | ||||||||||||||||||||||||||||
| 7 | # NBI::QueuedJob objects. Key responsibilities: | ||||||||||||||||||||||||||||
| 8 | # - new() : accepts -username, -jobid, -queue, -state, -name filters; | ||||||||||||||||||||||||||||
| 9 | # immediately runs squeue and populates $self->{jobs} | ||||||||||||||||||||||||||||
| 10 | # - len() : returns the number of matching jobs | ||||||||||||||||||||||||||||
| 11 | # - ids() : returns an array (or arrayref) of job IDs | ||||||||||||||||||||||||||||
| 12 | # - remove() : removes a job by ID from the in-memory list | ||||||||||||||||||||||||||||
| 13 | # Internal helpers: | ||||||||||||||||||||||||||||
| 14 | # - _squeue() : builds and runs the squeue command, splits | ||||||||||||||||||||||||||||
| 15 | # fields on a custom separator, applies name | ||||||||||||||||||||||||||||
| 16 | # filtering, and instantiates NBI::QueuedJob objects | ||||||||||||||||||||||||||||
| 17 | # - _make_format_string() : substitutes human-readable field names for | ||||||||||||||||||||||||||||
| 18 | # squeue %-format codes using %NBI::Slurm::FORMAT_STRINGS | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # RELATIONSHIPS: | ||||||||||||||||||||||||||||
| 21 | # - Uses %NBI::Slurm::FORMAT_STRINGS to translate field names to squeue codes. | ||||||||||||||||||||||||||||
| 22 | # - Creates NBI::QueuedJob instances for each row returned by squeue. | ||||||||||||||||||||||||||||
| 23 | # - $NBI::Queue::VERSION is set from $NBI::Slurm::VERSION. | ||||||||||||||||||||||||||||
| 24 | # - Used by the lsjobs and waitjobs bin scripts. | ||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | package NBI::Queue; | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use NBI::Slurm; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use NBI::QueuedJob; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use Data::Dumper qw(Dumper); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 2 | $NBI::Queue::VERSION = $NBI::Slurm::VERSION; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Export QueuedJob | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 2 | our @EXPORT = qw(NBI::QueuedJob new); | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 42 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # -username | ||||||||||||||||||||||||||||
| 46 | # -jobid | ||||||||||||||||||||||||||||
| 47 | 0 | my $username; | |||||||||||||||||||||||||||
| 48 | my $jobid; | ||||||||||||||||||||||||||||
| 49 | 0 | my $queue; | |||||||||||||||||||||||||||
| 50 | 0 | my $state_short; | |||||||||||||||||||||||||||
| 51 | 0 | my $partitions_csv; | |||||||||||||||||||||||||||
| 52 | 0 | my $jobname; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (defined $_[0] and substr($_[0], 0, 1) eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_[0] and substr($_[0], 0, 1) eq "-"
MC/DC0%0/2
defined $_[0] and substr($_[0], 0, 1) eq "-"
defined $_[0]
substr($_[0], 0, 1) eq "-"
Truth table0%0/3
A and B
A= defined $_[0]B= substr($_[0], 0, 1) eq "-"
| |||||||||||||||||||||||||||||
| 54 | 0 | my %data = @_; | |||||||||||||||||||||||||||
| 55 | 0 | for my $i (keys %data) { | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($i =~ /^-user/) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | next unless defined $data{$i}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $username = $data{$i}; | |||||||||||||||||||||||||||
| 59 | } elsif ($i =~ /^-jobid/) { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | next unless defined $data{$i}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($data{$i} =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $jobid = $data{$i}; | |||||||||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||||||||
| 64 | 0 | confess "ERROR NBI::Queue: -jobid expects an integer\n"; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } elsif ($i =~ /^-queue/) { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | next unless defined $data{$i}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $queue = $data{$i}; | |||||||||||||||||||||||||||
| 69 | } elsif ($i =~ /^-state/) { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | next unless defined $data{$i}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | my @valid_states = qw(PD R CG CF CA CD F TO NF SE ST RV S SO PR NF RV S SO PR); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (grep {$_ eq uc($data{$i})} @valid_states) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $state_short = uc($data{$i}); | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 0 | confess "ERROR NBI::Queue: -state expects one of the following values: @valid_states\n"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } elsif ($i =~ /^-name/) { | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $jobname = $data{$i} if defined $data{$i}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||
| 80 | 0 | confess "ERROR NBI::Queue: Unknown option/parameter $i\n"; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | my $jobs = _squeue($username, $jobid, $state_short, $partitions_csv, $jobname); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | $self->{username} = $username // undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$username // undef
MC/DC0%0/1
$username // undef
$username
Truth table0%0/2
A
A= $username
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $self->{jobid} = $jobid // undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$jobid // undef
MC/DC0%0/1
$jobid // undef
$jobid
Truth table0%0/2
A
A= $jobid
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $self->{queue} = $queue // undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$queue // undef
MC/DC0%0/1
$queue // undef
$queue
Truth table0%0/2
A
A= $queue
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $self->{state_short} = $state_short // undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$state_short // undef
MC/DC0%0/1
$state_short // undef
$state_short
Truth table0%0/2
A
A= $state_short
| |||||||||||||||||||||||||||||
| 89 | 0 | $self->{jobs} = $jobs; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | $self->{jobname} = $jobname // undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$jobname // undef
MC/DC0%0/1
$jobname // undef
$jobname
Truth table0%0/2
A
A= $jobname
| |||||||||||||||||||||||||||||
| 91 | 0 | return $self; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 0 | my $jobid = shift; | |||||||||||||||||||||||||||
| 97 | 0 | my @jobs = grep {$_->jobid != $jobid} @{$self->{jobs}}; | |||||||||||||||||||||||||||
| 98 | 0 | $self->{jobs} = \@jobs; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _squeue { | ||||||||||||||||||||||||||
|
Subroutine
_squeue: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($username, $jobid, $state_short, $partitions_csv, $jobname) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | my $field_sep = ':/:'; | |||||||||||||||||||||||||||
| 103 | 0 | my @field_names = qw(jobid user jobname cpus memory queue status start_time end_time total_time time_left command workdir account reason); | |||||||||||||||||||||||||||
| 104 | 0 | my $format = join $field_sep, @field_names; | |||||||||||||||||||||||||||
| 105 | 0 | $format = _make_format_string($format); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Prepare command | ||||||||||||||||||||||||||||
| 108 | 0 | my $cmd = "squeue --format '$format' --noheader "; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | $cmd .= " -u $username " if defined $username; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $cmd .= " -j $jobid " if defined $jobid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $cmd .= " -t $state_short " if defined $state_short; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $cmd .= " -p $partitions_csv " if defined $partitions_csv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Prepend '-' to @field_names | ||||||||||||||||||||||||||||
| 115 | 0 | @field_names = map { "-$_" } @field_names; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my @output = `$cmd 2>/dev/null`; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($? != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | Carp::croak "ERROR NBI::Queue: squeue failed. Are you in a SLURM cluster?\n"; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | my @header; | |||||||||||||||||||||||||||
| 122 | 0 | my $c = 0; | |||||||||||||||||||||||||||
| 123 | 0 | my @jobs; | |||||||||||||||||||||||||||
| 124 | 0 | for my $line (@output) { | |||||||||||||||||||||||||||
| 125 | 0 | $c++; | |||||||||||||||||||||||||||
| 126 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my @fields = split /$field_sep/, $line; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Make a hash of @fields_names and @fields | ||||||||||||||||||||||||||||
| 131 | 0 | my %job; | |||||||||||||||||||||||||||
| 132 | 0 | @job{@field_names} = @fields; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | ## FILTER FURTHER | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if (defined $jobname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | next unless $job{-"jobname"} =~ /$jobname/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | my $submitted_job = NBI::QueuedJob->new(%job); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | push @jobs, $submitted_job; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | return \@jobs; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub len { | ||||||||||||||||||||||||||
|
Subroutine
len: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 0 | return scalar @{$self->{jobs}}; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub ids { | ||||||||||||||||||||||||||
|
Subroutine
ids: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 155 | 0 | my @ids = map {$_->jobid} @{$self->{jobs}}; | |||||||||||||||||||||||||||
| 156 | # If scalar | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | return @ids; | |||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 0 | return \@ids; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub _make_format_string { | ||||||||||||||||||||||||||
|
Subroutine
_make_format_string: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 164 | 0 | my $string = shift; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | for my $key (keys %NBI::Slurm::FORMAT_STRINGS) { | |||||||||||||||||||||||||||
| 167 | 0 | my $val = $NBI::Slurm::FORMAT_STRINGS{$key}; | |||||||||||||||||||||||||||
| 168 | 0 | $string =~ s/$key/$val/; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | return $string; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 2 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||