File Coverage

bin/waitjobs
Criterion Covered Total %
statement 39 64 60.9
branch 2 10 20.0
condition 1 5 20.0
subroutine 10 12 83.3
pod n/a
total 52 91 57.1


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             #ABSTRACT: Wait for selected jobs to exit the queue
3             #PODNAME: waitjobs
4              
5 1     1   5181 use v5.12;
  1         4  
6 1     1   4 use warnings;
  1         2  
  1         48  
7 1     1   772 use Getopt::Long;
  1         17729  
  1         7  
8 1     1   649 use FindBin qw($RealBin);
  1         1463  
  1         127  
9 1     1   663 use Data::Dumper;
  1         10460  
  1         87  
10 1     1   682 use Term::ANSIColor qw(:constants);
  1         13802  
  1         1427  
11 1 50       128744 if (-e "$RealBin/../dist.ini") {
  0         0  
12 1 50       7 say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
13 1     1   551 use lib "$RealBin/../lib";
  1         827  
  1         7  
14             }
15 1     1   703 use NBI::Slurm;
  1         5  
  1         217  
16 1     1   651 use NBI::Queue;
  1         4  
  1         106525  
17 1         4 $Data::Dumper::Sortkeys = 1;
18              
19 1         3 my $opt_user = $ENV{USER};
20 1         4 my $opt_name = '.+';
21 1         3 my $opt_status = '';
22 1         3 my $opt_verbose_bool = 0;
23 1         3 my $opt_debug_bool = 0;
24 1         2 my $refresh_time = 20;
25             # Check if this script is running as a job, in case save the job id
26 1   50     8 my $self_job_id = $ENV{SLURM_JOB_ID} // undef;
27             GetOptions(
28             'u|user=s' => \$opt_user,
29             'n|name=s' => \$opt_name,
30             'r|refresh=i' => \$refresh_time,
31             'verbose' => \$opt_verbose_bool,
32 1     1   1441 'version' => sub { say "waitjobs v", $NBI::Slurm::VERSION; exit },
  1         120  
33             'debug' => \$opt_debug_bool,
34 0     0   0 'help' => sub { usage() },
35 1         16 );
36              
37 0         0 my $queue = NBI::Queue->new(
38             -username => $opt_user,
39             -name => $opt_name,
40             );
41             # If $self_job_id is anq integer, remove it from the queue
42 0 0 0     0 if (defined $self_job_id and $self_job_id =~ /^\d+$/) {
43 0         0 print STDERR timelog("waitjobs"), "Ignoring current job id $self_job_id\n";
44 0         0 $queue->remove($self_job_id);
45             }
46              
47 0         0 my @starting_ids = @{$queue->ids()};
  0         0  
48 0         0 print STDERR timelog("waitjobs"), "There are ",
49             GREEN, BOLD, scalar @starting_ids, RESET " jobs for ",
50             GREEN, BOLD, $opt_user, RESET, " with name matching ",
51             GREEN, BOLD, $opt_name, RESET, "\n";
52              
53 0         0 while (scalar @starting_ids > 0) {
54             # Create the new queue with the same parameters as the initial queue
55 0         0 my $current_jobs = NBI::Queue->new(
56             -username => $opt_user,
57             -name => $opt_name,
58             );
59 0         0 my @current_ids = @{$current_jobs->ids()};
  0         0  
60              
61             # Remove from @starting_ids all the jobs that are NOT in @current_ids
62 0         0 my @new_starting_ids = ();
63 0         0 for my $starting_id (@starting_ids) {
64 0 0       0 if (grep {$_ eq $starting_id} @current_ids) {
  0         0  
65 0         0 push(@new_starting_ids, $starting_id);
66             }
67             }
68 0         0 @starting_ids = @new_starting_ids;
69 0 0       0 if ($opt_verbose_bool) {
70 0         0 say STDERR timelog("waitjobs"), "Waiting for ", scalar @starting_ids, " jobs to finish (refresh in $refresh_time seconds)";
71             }
72 0         0 sleep $refresh_time;
73             }
74              
75              
76 0         0 say STDERR timelog("waitjobs"), "No jobs left: quitting\n";
77              
78             sub usage {
79 0     0     say STDERR<
80              
81             Usage: waitjobs [options] [job_name]
82             ------------------------------------
83             Wait for selected jobs to exit the queue
84              
85             Options:
86             -u, --user default current user
87             -n, --name default '.+' (matches any name)
88             -r, --refresh check every 20 seconds
89            
90             END
91 0           exit;
92             }
93              
94             __END__