-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLifecycleUpdateExpirationTask.js
More file actions
187 lines (173 loc) · 6.66 KB
/
LifecycleUpdateExpirationTask.js
File metadata and controls
187 lines (173 loc) · 6.66 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const async = require('async');
const { errors } = require('arsenal');
const ObjectMD = require('arsenal').models.ObjectMD;
const BackbeatTask = require('../../../lib/tasks/BackbeatTask');
const ActionQueueEntry = require('../../../lib/models/ActionQueueEntry');
class LifecycleUpdateExpirationTask extends BackbeatTask {
/**
* Process a lifecycle object entry
*
* @constructor
* @param {LifecycleObjectProcessor} proc - object processor instance
*/
constructor(proc) {
const procState = proc.getStateVars();
super();
Object.assign(this, procState);
}
_getMetadata(entry, log, done) {
const {
accountId,
bucket,
key,
version,
} = entry.getAttribute('target');
const backbeatClient = this.getBackbeatMetadataProxy(accountId);
if (!backbeatClient) {
log.error('failed to get backbeat client', { accountId });
done(errors.InternalError.customizeDescription(
'Unable to obtain client',
));
return;
}
backbeatClient.getMetadata({
bucket,
objectKey: key,
versionId: version,
}, log, (err, blob) => {
if (err) {
log.error('error getting metadata blob from S3', Object.assign({
method: 'LifecycleUpdateExpirationTask._getMetadata',
error: err.message,
}, entry.getLogInfo()));
done(err);
return;
}
const res = ObjectMD.createFromBlob(blob.Body);
if (res.error) {
log.error('error parsing metadata blob', Object.assign({
error: res.error,
method: 'LifecycleUpdateExpirationTask._getMetadata',
}, entry.getLogInfo()));
done(errors.InternalError.customizeDescription(
'error parsing metadata blob'
));
} else {
done(null, res.result);
}
});
return;
}
_putMetadata(entry, objMD, log, done) {
const {
accountId,
bucket,
key,
version,
} = entry.getAttribute('target');
const backbeatClient = this.getBackbeatMetadataProxy(accountId);
if (!backbeatClient) {
log.error('failed to get backbeat client', { accountId });
done(errors.InternalError.customizeDescription(
'Unable to obtain client',
));
return;
}
backbeatClient.putMetadata({
bucket,
objectKey: key,
versionId: version,
mdBlob: objMD.getSerialized(),
}, log, err => {
if (err) {
log.error(
'an error occurred when updating metadata for transition',
Object.assign(
{
method: 'LifecycleUpdateExpirationTask._putMetadata',
error: err.message,
},
entry.getLogInfo(),
)
);
done(err);
return;
} else {
log.end().info('metadata updated for transition', entry.getLogInfo());
done();
return;
}
});
}
_garbageCollectLocation(entry, locations, log, done) {
const { bucket, key, version, eTag } = entry.getAttribute('target');
const gcEntry = ActionQueueEntry.create('deleteData')
.addContext({
origin: 'lifecycle',
ruleType: 'restore',
reqId: log.getSerializedUids(),
bucketName: bucket,
objectKey: key,
versionId: version,
eTag,
})
.setAttribute('source', entry.getAttribute('source'))
.setAttribute('serviceName', 'lifecycle-transition')
.setAttribute('target.locations', locations);
this.gcProducer.publishActionEntry(gcEntry);
return process.nextTick(done);
}
/**
* Execute the action specified in action entry to update expirations on an object
*
* @param {ActionQueueEntry} entry - action entry to execute
* @param {Function} done - callback funtion
* @return {undefined}
*/
processActionEntry(entry, done) {
const log = this.logger.newRequestLogger();
entry.addLoggedAttributes({
bucketName: 'target.bucket',
objectKey: 'target.key',
versionId: 'target.version',
});
async.waterfall([
next => {
const coldLocation = entry.getAttribute('target.location');
if (!coldLocation) {
// this should never happen as sorbet always sets the location attribute
log.error('missing target location', {
entry: entry.getLogInfo(),
method: 'LifecycleUpdateExpirationTask.processActionEntry',
});
return next(errors.MissingParameter.customizeDescription('missing target location'));
}
return next(null, coldLocation);
},
(coldLocation, next) => this._getMetadata(entry, log, (err, objMD) => next(err, coldLocation, objMD)),
(coldLocation, objMD, next) => {
const archive = objMD.getArchive();
// Confirm the object has indeed expired: it can happen that the
// expiration date is updated while the expiry was "in-flight" (e.g.
// queued for expiry but not yet expired)
if (new Date(archive.restoreWillExpireAt) > new Date()) {
return process.nextTick(done);
}
// Reset archive flags to no longer show it as restored
objMD.setArchive({
archiveInfo: archive.archiveInfo,
});
objMD.setAmzRestore();
objMD.setDataStoreName(coldLocation);
objMD.setAmzStorageClass(coldLocation);
objMD.setTransitionInProgress(false);
objMD.setOriginOp('s3:ObjectRestore:Delete');
return this._putMetadata(entry, objMD, log, err => next(err, objMD));
},
(objMD, next) => this._garbageCollectLocation(
entry, objMD.getLocation(), log, next,
),
], done);
}
}
module.exports = LifecycleUpdateExpirationTask;