forked from Samsung360VR/aws-lambda-redshift-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reprocessBatch.js
173 lines (159 loc) · 4.02 KB
/
reprocessBatch.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
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
/*
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');
var async = require('async');
require('./constants');
var common = require('./common');
if (process.argv.length < 4) {
console.log("You must provide an AWS Region Code, Batch ID, and configured Input Location");
process.exit(ERROR);
}
var setRegion = process.argv[2];
var thisBatchId = process.argv[3];
var prefix = process.argv[4];
// connect to dynamo db and s3
var dynamoDB = new aws.DynamoDB({
apiVersion : '2012-08-10',
region : setRegion
});
var s3 = new aws.S3({
apiVersion : '2006-03-01',
region : setRegion
});
var processFile = function(batchEntry, callback) {
// delete the processed file entry
var fileItem = {
Key : {
loadFile : {
S : batchEntry
}
},
TableName : filesTable
};
dynamoDB.deleteItem(fileItem, function(err, data) {
if (err) {
console.log(filesTable + " Delete Error");
console.log(err);
callback(err);
} else {
// issue a same source/target copy command to S3, which will cause
// Lambda to get a new event
var bucketName = batchEntry.split("/")[0];
var fileKey = batchEntry.replace(bucketName + "\/", "");
var copySpec = {
Metadata : {
CopyReason : "AWS Lambda Redshift Loader Reprocess Batch " + thisBatchId
},
MetadataDirective : "REPLACE",
Bucket : bucketName,
Key : fileKey,
CopySource : batchEntry
};
s3.copyObject(copySpec, function(err, data) {
if (err) {
console.log(err);
callback(err);
} else {
console.log("Submitted reprocess request for " + batchEntry);
// done - call the callback
callback();
}
});
}
});
};
var updateBatchStatus = function(thisBatchId, err, results) {
if (err) {
console.log(JSON.stringify(err));
process.exit(ERROR);
} else {
var updateBatchStatus = {
Key : {
batchId : {
S : thisBatchId,
},
s3Prefix : {
S : prefix
}
},
TableName : batchTable,
AttributeUpdates : {
status : {
Action : 'PUT',
Value : {
S : 'reprocessed'
}
},
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) {
console.log(JSON.stringify(err));
process.exit(ERROR);
} else {
console.log("Batch " + thisBatchId + " Submitted for Reprocessing");
process.exit(OK);
}
})
}
};
// fetch the batch
var getBatch = {
Key : {
batchId : {
S : thisBatchId,
},
s3Prefix : {
S : prefix
}
},
TableName : batchTable,
ConsistentRead : true
};
dynamoDB.getItem(getBatch, function(err, data) {
if (err) {
console.log(err);
process.exit(ERROR);
} else {
if (data && data.Item) {
if (data.Item.status.S === open) {
console.log("Cannot reprocess an Open Batch");
process.exit(error);
} else {
// load the global batch entries so that we can process it in
// callbacks
var batchEntries = data.Item.entries.SS;
if (!data.Item.entries.SS) {
console.log("Batch is Empty!");
} else {
async.map(data.Item.entries.SS, processFile, updateBatchStatus.bind(undefined, thisBatchId));
}
}
} else {
console.log("Unable to retrieve batch " + thisBatchId + " for prefix " + prefix);
process.exit(ERROR);
}
}
});