summaryrefslogtreecommitdiffabout
path: root/lib/App/Glacier/Command/Jobs.pm
blob: 736d7132894dc12bc7d01de2dde486bbd7a1c19e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package App::Glacier::Command::Jobs;
use strict;
use warnings;
use App::Glacier::Core;
use parent qw(App::Glacier::Command);
use Carp;
use Data::Dumper;
use App::Glacier::Timestamp;

=head1 NAME

glacier jobs - list Glacier jobs

=head1 SYNOPSIS

B<glacier jobs>
[B<-cl>]
[B<--cached>]    
[B<--long>]    
[B<--time-style=>B<default>|B<full-iso>|B<long-iso>|B<iso>|B<locale>|B<+I<FORMAT>>]
[I<VAULT>...]
    
=head1 DESCRIPTION

Verifies and lists pending and completed glacier jobs.  By default, all jobs
are listed.  If one or more I<VAULT> arguments are given, only jobs for the
listed vaults are displayed.    

Verification consists in querying the Glacier for the status of the current
job and updating cached data accordingly.  Failed and expired jobst are
removed.
    
=head1 OPTIONS

=over 4

=item B<-c>, B<--cached>

Displays cached results without querying Glacier.

=item B<-l>, B<--long>

List jobs in long format.  Long format includes Glaciar job identifier.

=item B<--time-style=>I<STYLE>

List timestamps in style STYLE.  The STYLE should be one of the
following:

=over 8

=item B<+I<FORMAT>>

List timestamps using I<FORMAT>, which is interpreted as the I<format>
argument to B<strftime>(3) function.
    
=item B<default>

Default format.  For timestamps not older than 6 month: month, day, hour
and minute, e.g.: "May 23 15:00".  For older timestamps: month, day, and
year, e.g.: "May 23  2017".
    
=item B<full-iso>

List timestamps in full using ISO 8601 date, time, and time zone format with
nanosecond precision, e.g., "2017-05-23 15:53:10.633308971 +0000".  This style
is equivalent to "B<+%Y-%m-%d %H:%M:%S.%N %z>".

=item B<long-iso>

List ISO 8601 date and time in minutes, e.g., "2017-05-23 15:53".  Equivalent
to "B<+%Y-%m-%d %H:%M>".    
    
=item B<iso>

B<GNU ls>-compatible "iso": ISO 8601 dates for non-recent timestamps (e.g.,
"2017-05-23"), and ISO 8601 month, day, hour, and minute for recent
timestamps (e.g., "03-30 23:45").  Timestamp is considered "recent", if it
is not older than 6 months ago.    

=item B<locale>

List timestamps in a locale-dependent form.  This is equivalent to B<+%c>.

=back

=back

=head1 SEE ALSO

B<glacier>(1),    
B<strftime>(3).
    
=cut    

sub new {
    my ($class, $argref, %opts) = @_;
    $class->SUPER::new(
	$argref,
        optmap => {
	    'time-style=s' => sub { $_[0]->set_time_style_option($_[2]) },
	    'long|l+' => 'long',
	    'cached|c' => 'cached',
	},
	%opts);
}

sub run {
     my $self = shift;
     $self->list($self->command_line);
}

sub list {
    my ($self, @vault_names) = @_;

    my $db = $self->jobdb();
    $db->foreach(sub {
	my ($key, $descr) = @_;
	my $vault = $descr->{VaultARN};
	$vault =~ s{.*:vaults/}{};

	return if (@vault_names && ! grep { $_ eq $vault } @vault_names);

	unless ($self->{_options}{cached}) {
	    if ($descr->{StatusCode} eq 'Failed') {
		$self->debug(1, "deleting failed $key $vault " .
			     ($descr->{JobDescription} || $descr->{Action}) .
			     $descr->{JobId});
		$db->delete($key) unless $self->dry_run;
		return;
	    }
	    
	    my $res = $self->glacier->Describe_job($vault, $descr->{JobId});
	    if ($self->glacier->lasterr) {
		if ($self->glacier->lasterr('code') == 404) {
		    $self->debug(1, "deleting expired $key $vault " .
			     ($descr->{JobDescription} || $descr->{Action}) .
			     $descr->{JobId});
		    $db->delete($key) unless $self->dry_run;
		    return;
		} else {
		    $self->error("can't describe job $descr->{JobId}: ",
				 $self->glacier->last_error_message);
		}
	    } elsif (ref($res) ne 'HASH') {
		croak "describe_job returned wrong datatype (".ref($res).") for \"$descr->{JobId}\"";
	    } else {
		$res = timestamp_deserialize($res);
		$self->debug(2, $res->{StatusCode});
		$db->store($key, $res) unless $self->dry_run;
		$descr = $res;
	    }		
	}
	
	my $started = $self->format_date_time($descr, 'CreationDate');
	    
	print "$started - ";
	if ($descr->{Completed} && $descr->{StatusCode} eq 'Succeeded') {
	    print $self->format_date_time($descr, 'CompletionDate');
	} else {
	    my $len = length($started);
	    printf("%$len.${len}s", $descr->{StatusCode});
	}
	printf(" %-10.10s ", $vault);	
	print $descr->{JobDescription} || $descr->{Action};
	if ($self->{_options}{long}) {
	    print ' ', $descr->{JobId};
	}
	print "\n";
		 });
}

1;

Return to:

Send suggestions and report system problems to the System administrator.