summaryrefslogtreecommitdiffabout
path: root/lib/App/Glacier/Job.pm
blob: 9478b20a6c33036b7056397e0007f95108eb1f06 (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
package App::Glacier::Job;
use strict;
use warnings;
require Exporter;
use parent qw(Exporter);
use Carp;
use App::Glacier::Core;
use App::Glacier::Timestamp;

# new(CMD, VAULT, KEY, INIT)

sub new {
    croak "bad number of arguments" unless $#_ >= 4;
    my ($class, $cmd, $vault, $key, %opts) = @_;
    my $invalidate = delete $opts{invalidate};
    my $ttl = delete $opts{ttl};
    
    if (keys(%opts)) {
	croak "unrecognized parameters: ".join(', ', keys(%opts));
    }
    
    return bless { _cmd => $cmd,
		   _vault => $vault,
		   _key => $key,
		   _job => undef,
		   _ttl => $ttl,
		   _invalidate => $invalidate }, $class;
}

sub fromdb {
    my ($class, $cmd, $vault, $key, $job) = @_;
    return bless { _cmd => $cmd,
		   _vault => $vault,
		   _key => $key,
		   _job => $job }, $class;
}

sub command { shift->{_cmd} }
sub glacier { shift->command->glacier }
sub vault { shift->{_vault} };

sub _get_db {
    my ($self) = @_;
    return $self->command->jobdb();
}

sub _get_job {
    my ($self) = @_;
    my $db = $self->_get_db;

    if ($self->{_job}) {
	if ($self->{_invalidate}) {
	    $db->delete($self->{_key});
	    $self->{_job} = undef;
	}
    }
    
    unless ($self->{_job}) {
	my $job = $db->retrieve($self->{_key}) unless $self->{_invalidate};
	if (!$job) {
	    $self->debug(2, "initiating job $self->{_key}");
	    $job = { JobId => $self->init, Completed => 0 };
	    $db->store($self->{_key}, $job);
	}

	if (!$job->{Completed}
	    || ($self->{_ttl} 
		&& (time - $job->{CompletionDate}->epoch) > $self->{_ttl})) {
	    $self->debug(2, "checking status of job $self->{_key}");
	    my $res = $self->glacier->Describe_job($self->{_vault},
						   $job->{JobId});
	    if ($self->glacier->lasterr) {
		if ($self->glacier->lasterr('code') == 404) {
		    $self->debug(2, "job $self->{_key} expired");
		    $db->delete($self->{_key});
		    return $self->_get_job;
		} else {
		    $self->command->abend(EX_UNAVAILABLE,
					 "can't describe job $job->{JobId}: ",
					 $self->glacier->last_error_message);
		}
	    } elsif (ref($res) ne 'HASH') {
		croak "describe_job returned wrong datatype (".ref($res).") for \"$job->{JobId}\"";
	    } else {
		$res = timestamp_deserialize($res);
		$self->debug(2, $res->{StatusCode});
		$db->store($self->{_key}, $res);
		$job = $res;
	    }		
	}
	$self->{_job} = $job;
    }
    return $self->{_job};
}

sub debug { my $self = shift->command->debug(@_) }

sub id {
    my $self = shift;
    my $job = $self->_get_job;
    return $job->{JobId};
}

sub get {
    my ($self, $key) = @_;
    my $job = $self->_get_job;
    return undef unless exists $job->{$key};
    return $job->{$key};
}

sub as_string { shift->get('JobDescription') }

use overload
    '""' => \&as_string;

sub is_finished {
    my $self = shift;
    return defined($self->get('StatusCode'));
}

sub is_completed {
    my $self = shift;
    return ($self->get('StatusCode') || '') eq 'Succeeded';
}

sub status {
    my $self = shift;
    my $status = $self->get('StatusCode');
    return undef unless defined $status;
    return wantarray ? ($status, $self->get('StatusMessage')) : $status;
}

sub delete {
    my $self = shift;
    if (my $cache = $self->cache_file) {
	if (-f $cache) {
	    unlink($cache);
	}
    }
    my $db = $self->_get_db;
    $db->delete($self->{_key});
}

sub cache_file {
    my $self = shift;
    my $aid = $self->get('ArchiveId') or return;
    my $vault = $self->get('VaultARN') or return;
    $vault =~ s{.*:vaults/}{};
    return $self->command->archive_cache_filename($vault, $aid);
}

1;

    

Return to:

Send suggestions and report system problems to the System administrator.