-
Notifications
You must be signed in to change notification settings - Fork 163
/
unlockBatch.js
116 lines (104 loc) · 2.9 KB
/
unlockBatch.js
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
/*
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
var aws = require('aws-sdk');
require('./constants');
var common = require('./common');
var usage = function() {
console.log("You must provide an AWS Region Code, Batch ID, and configured Input Location to use Unlock.");
process.exit(ERROR);
}
if (process.argv.length < 4) {
usage();
}
var setRegion = process.argv[2];
var thisBatchId = process.argv[3];
var prefix = process.argv[4];
if (!thisBatchId || !prefix) {
usage();
}
var dynamoDB = new aws.DynamoDB({
apiVersion : '2012-08-10',
region : setRegion
});
var getConfig = {
Key : {
s3Prefix : {
S : prefix
}
},
TableName : configTable,
ConsistentRead : true
};
dynamoDB.getItem(getConfig, function(err, data) {
if (err) {
console.log(err);
process.exit(ERROR);
} else {
if (!data) {
console.log("Unable to find Configuration with S3 Prefix " + prefix + " in Region " + setRegion);
} else {
// only allow unlocking if the batch is allocated as current
if (data.Item.currentBatch.S !== thisBatchId) {
console.log("Batch " + thisBatchId + " is not currently allocated as the open batch for Load Configuration on "
+ prefix + ". Use reprocessBatch.js to rerun the load of this Batch.");
process.exit(ERROR);
} else {
var updateBatchStatus = {
Key : {
batchId : {
S : thisBatchId,
},
s3Prefix : {
S : prefix
}
},
TableName : batchTable,
AttributeUpdates : {
status : {
Action : 'PUT',
Value : {
S : 'open'
}
},
lastUpdate : {
Action : 'PUT',
Value : {
N : '' + common.now()
}
}
},
// the batch to be unlocked must be in locked or error state - we
// can't reopen
// 'complete' batches
Expected : {
status : {
AttributeValueList : [ {
S : 'locked'
}, {
S : 'error'
} ],
ComparisonOperator : 'IN'
}
}
};
dynamoDB.updateItem(updateBatchStatus, function(err, data) {
if (err) {
if (err.code === conditionCheckFailed) {
console.log("Batch " + thisBatchId + " cannot be unlocked as it is not in 'locked' or 'error' status");
} else {
console.log(err);
process.exit(ERROR);
}
} else {
console.log("Batch " + thisBatchId + " Unlocked and ready for reprocessing");
}
process.exit(OK);
});
}
}
}
});