|
6 | 6 | http://aws.amazon.com/asl/ |
7 | 7 |
|
8 | 8 | 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. |
9 | | -*/ |
| 9 | + */ |
10 | 10 |
|
11 | 11 | var aws = require('aws-sdk'); |
12 | 12 | require('./constants'); |
13 | 13 | var common = require('./common'); |
14 | 14 |
|
15 | | -if (process.argv.length < 4) { |
16 | | - console.log("You must provide an AWS Region Code, Batch ID, and configured Input Location to use Unlock"); |
| 15 | +var usage = function() { |
| 16 | + console.log("You must provide an AWS Region Code, Batch ID, and configured Input Location to use Unlock."); |
17 | 17 | process.exit(ERROR); |
18 | 18 | } |
| 19 | + |
| 20 | +if (process.argv.length < 4) { |
| 21 | + usage(); |
| 22 | +} |
| 23 | + |
19 | 24 | var setRegion = process.argv[2]; |
20 | 25 | var thisBatchId = process.argv[3]; |
21 | 26 | var prefix = process.argv[4]; |
22 | 27 |
|
| 28 | +if (!thisBatchId || !prefix) { |
| 29 | + usage(); |
| 30 | +} |
23 | 31 | var dynamoDB = new aws.DynamoDB({ |
24 | 32 | apiVersion : '2012-08-10', |
25 | 33 | region : setRegion |
26 | 34 | }); |
27 | 35 |
|
28 | | -var updateBatchStatus = { |
| 36 | +var getConfig = { |
29 | 37 | Key : { |
30 | | - batchId : { |
31 | | - S : thisBatchId, |
32 | | - }, |
33 | 38 | s3Prefix : { |
34 | 39 | S : prefix |
35 | 40 | } |
36 | 41 | }, |
37 | | - TableName : batchTable, |
38 | | - AttributeUpdates : { |
39 | | - status : { |
40 | | - Action : 'PUT', |
41 | | - Value : { |
42 | | - S : 'open' |
43 | | - } |
44 | | - }, |
45 | | - lastUpdate : { |
46 | | - Action : 'PUT', |
47 | | - Value : { |
48 | | - N : '' + common.now() |
49 | | - } |
50 | | - } |
51 | | - }, |
52 | | - // the batch to be unlocked must be in locked or error state - we can't reopen |
53 | | - // 'complete' batches |
54 | | - Expected : { |
55 | | - status : { |
56 | | - AttributeValueList : [ { |
57 | | - S : 'locked' |
58 | | - }, { |
59 | | - S : 'error' |
60 | | - } ], |
61 | | - ComparisonOperator : 'IN' |
62 | | - } |
63 | | - } |
| 42 | + TableName : configTable, |
| 43 | + ConsistentRead : true |
64 | 44 | }; |
65 | 45 |
|
66 | | -dynamoDB.updateItem(updateBatchStatus, function(err, data) { |
| 46 | +dynamoDB.getItem(getConfig, function(err, data) { |
67 | 47 | if (err) { |
68 | | - if (err.code === conditionCheckFailed) { |
69 | | - console.log("Batch " + thisBatchId + " cannot be unlocked"); |
| 48 | + console.log(err); |
| 49 | + process.exit(ERROR); |
| 50 | + } else { |
| 51 | + if (!data) { |
| 52 | + console.log("Unable to find Configuration with S3 Prefix " + prefix + " in Region " + setRegion); |
70 | 53 | } else { |
71 | | - console.log(err); |
72 | | - process.exit(ERROR); |
| 54 | + // only allow unlocking if the batch is allocated as current |
| 55 | + if (data.Item.currentBatch.S !== thisBatchId) { |
| 56 | + console.log("Batch " + thisBatchId + " is not currently allocated as the open batch for Load Configuration on " |
| 57 | + + prefix + ". Use reprocessBatch.js to rerun the load of this Batch."); |
| 58 | + process.exit(ERROR); |
| 59 | + } else { |
| 60 | + var updateBatchStatus = { |
| 61 | + Key : { |
| 62 | + batchId : { |
| 63 | + S : thisBatchId, |
| 64 | + }, |
| 65 | + s3Prefix : { |
| 66 | + S : prefix |
| 67 | + } |
| 68 | + }, |
| 69 | + TableName : batchTable, |
| 70 | + AttributeUpdates : { |
| 71 | + status : { |
| 72 | + Action : 'PUT', |
| 73 | + Value : { |
| 74 | + S : 'open' |
| 75 | + } |
| 76 | + }, |
| 77 | + lastUpdate : { |
| 78 | + Action : 'PUT', |
| 79 | + Value : { |
| 80 | + N : '' + common.now() |
| 81 | + } |
| 82 | + } |
| 83 | + }, |
| 84 | + // the batch to be unlocked must be in locked or error state - we |
| 85 | + // can't reopen |
| 86 | + // 'complete' batches |
| 87 | + Expected : { |
| 88 | + status : { |
| 89 | + AttributeValueList : [ { |
| 90 | + S : 'locked' |
| 91 | + }, { |
| 92 | + S : 'error' |
| 93 | + } ], |
| 94 | + ComparisonOperator : 'IN' |
| 95 | + } |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + dynamoDB.updateItem(updateBatchStatus, function(err, data) { |
| 100 | + if (err) { |
| 101 | + if (err.code === conditionCheckFailed) { |
| 102 | + console.log("Batch " + thisBatchId + " cannot be unlocked as it is not in 'locked' or 'error' status"); |
| 103 | + } else { |
| 104 | + console.log(err); |
| 105 | + process.exit(ERROR); |
| 106 | + } |
| 107 | + } else { |
| 108 | + console.log("Batch " + thisBatchId + " Unlocked and ready for reprocessing"); |
| 109 | + } |
| 110 | + |
| 111 | + process.exit(OK); |
| 112 | + }); |
| 113 | + } |
73 | 114 | } |
74 | | - } else { |
75 | | - console.log("Batch " + thisBatchId + " Unlocked and ready for reprocessing"); |
76 | 115 | } |
77 | | - |
78 | | - process.exit(OK); |
79 | 116 | }); |
0 commit comments