File Coverage

blib/lib/NBI/QueuedJob.pm
Criterion Covered Total %
statement 50 62 80.6
branch 23 46 50.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 6 6 100.0
total 90 129 69.7


line stmt bran cond sub pod time code
1             #ABSTRACT: NBI::QueuedJob, to describe a job from the SLURM queue (squeue)
2 2     2   3314 use strict;
  2         5  
  2         60  
3 2     2   10 use warnings;
  2         4  
  2         74  
4             package NBI::QueuedJob;
5 2     2   11 use Carp qw(confess croak);
  2         3  
  2         2328  
6             require Exporter;
7             our @ISA = qw(Exporter);
8              
9              
10             $NBI::QueuedJob::VERSION = $NBI::Slurm::VERSION;
11             our $VALID_ATTR_STR="ACCOUNT|GRES|MIN_CPUS|MIN_TMP_DISK|END_TIME|FEATURES|GROUP|OVER_SUBSCRIBE|JOBID|NAME|COMMENT|TIME_LIMIT|MIN_MEMORY|REQ_NODES|COMMAND|PRIORITY|QOS|REASON|ST|USER|RESERVATION|WCKEY|EXC_NODES|NICE|S:C:T|JOBID|EXEC_HOST|CPUS|NODES|DEPENDENCY|ARRAY_JOB_ID|GROUP|SOCKETS_PER_NODE|CORES_PER_SOCKET|THREADS_PER_CORE|ARRAY_TASK_ID|TIME_LEFT|TIME|NODELIST|CONTIGUOUS|PARTITION|PRIORITY|NODELIST(REASON)|START_TIME|STATE|USER|SUBMIT_TIME|LICENSES|CORE_SPEC|SCHEDNODES|WORK_DIR";
12             our $VALID_STATUS_STR="PENDING,RUNNING,SUSPENDED,COMPLETED,CANCELLED,FAILED,TIMEOUT,NODE_FAIL,PREEMPTED,BOOT_FAIL,DEADLINE,OUT_OF_MEMORY,COMPLETING,CONFIGURING,RESIZING,REVOKED,SPECIAL_EXIT";
13             our @VALID_ATTR=split(/\|/,$VALID_ATTR_STR);
14             our @VALID_STATUS=split(/,/,$VALID_STATUS_STR);
15              
16             # Append to @VALID_STATUS also @FORMAT_STRINGS
17             @VALID_ATTR = (@VALID_ATTR, keys %NBI::Slurm::FORMAT_STRINGS);
18             # make uc all VALID_ATTR
19             @VALID_ATTR = map {uc($_)} @VALID_ATTR;
20             sub new {
21 2     2 1 1043 my $class = shift @_;
22 2         9 my $username;
23             my $queue;
24 2         0 my $name;
25 2         0 my $jobid;
26 2         0 my $status;
27 2         4 my $attrs = {};
28             # Descriptive instantiation with parameters -param => value
29 2 50       7 if (substr($_[0], 0, 1) eq '-') {
30 2         8 my %data = @_;
31             # Try parsing
32 2         6 for my $i (keys %data) {
33 5 100       31 if ($i =~ /^-user/) {
    50          
    50          
    50          
    50          
    50          
34 2         5 $username = $data{$i};
35             } elsif ($i =~ /^-jobid/) {
36             # Check it's an integer
37 0 0       0 if ($data{$i} =~ /^\d+$/) {
38 0         0 $jobid = $data{$i};
39             } else {
40 0         0 confess "ERROR NBI: -threads expects an integer\n";
41             }
42             } elsif ($i =~ /^-queue/) {
43 0         0 $queue = $data{$i};
44             } elsif ($i =~ /^-status/) {
45             # Check it's a valid status
46 0 0       0 if (grep {$_ eq uc($data{$i})} @VALID_STATUS) {
  0         0  
47 0         0 $status = uc($data{$i});
48             } else {
49 0         0 confess "ERROR NBI: -status expects one of the following values: $VALID_STATUS_STR\n";
50             }
51            
52             } elsif ($i =~ /^-name/) {
53 0         0 $name = $data{$i};
54             } elsif ($i =~ /^-(\w+)/) {
55 3 100       6 if (grep {$_ eq uc($1)} @VALID_ATTR) {
  153         249  
56 2 50       6 if (defined $data{$i}) {
57 2         5 $attrs->{uc($1)} = $data{$i};
58             } else {
59 0         0 croak "ERROR NBI: -$1 expects a value\n";
60             }
61             } else {
62 1         375 confess "ERROR NBI: Unknown parameter -$1\nValid parameters are: ",
63             join("\n -",@VALID_ATTR), "\n";
64             }
65             } else {
66 0         0 confess "ERROR NBI: Unknown option/parameter $i\n";
67             }
68             }
69             }
70            
71 1         3 my $self = bless {}, $class;
72            
73             # Set attributes
74 1 50       4 $self->username = defined $username ? $username : undef;
75 1 50       7 $self->queue = defined $queue ? $queue : undef;
76 1 50       4 $self->name = defined $name ? $name : undef;
77 1 50       5 $self->jobid = defined $jobid ? $jobid : undef;
78 1 50       5 $self->status = defined $status ? $status : undef;
79 1         2 $self->{attrs} = $attrs;
80 1         12 return $self;
81             }
82              
83             sub username : lvalue {
84             # Update threads
85 1     1 1 7 my ($self, $new_val) = @_;
86 1 50       3 $self->{username} = $new_val if (defined $new_val);
87 1         6 return $self->{username};
88             }
89              
90             sub queue : lvalue {
91             # Update queue
92 1     1 1 2 my ($self, $new_val) = @_;
93 1 50       7 $self->{queue} = $new_val if (defined $new_val);
94 1         2 return $self->{queue};
95             }
96              
97             sub name : lvalue {
98             # Update name
99 1     1 1 1 my ($self, $new_val) = @_;
100 1 50       3 $self->{name} = $new_val if (defined $new_val);
101 1         18 return $self->{name};
102             }
103              
104             sub jobid : lvalue {
105             # Update jobid
106 1     1 1 1 my ($self, $new_val) = @_;
107 1 50 33     3 confess "ERROR NBI::Queue: jobid must be an integer\n" if (defined $new_val && $new_val !~ /^\d+$/);
108 1 50       5 $self->{jobid} = $new_val if (defined $new_val);
109 1         2 return $self->{jobid};
110             }
111              
112             sub status : lvalue {
113             # Update status
114 1     1 1 2 my ($self, $new_val) = @_;
115 1 50 33     3 confess "ERROR NBI::Queue: status must be one of the following values: $VALID_STATUS_STR\n" if (defined $new_val && !grep {$_ eq uc($new_val)} @VALID_STATUS);
  0         0  
116 1 50       2 $self->{status} = $new_val if (defined $new_val);
117 1         2 return $self->{status};
118             }
119             1;
120              
121             __END__