-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (48 loc) · 1.33 KB
/
index.ts
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
import AWS = require("aws-sdk");
import _ = require("lodash");
import uuid = require("uuid/v1");
export function bootstrap(event, context, callback) {
console.log("Start publishing data request tasks.");
const s3 = new AWS.S3();
const sqs = new AWS.SQS();
const params = {
Bucket: process.env.S3_BUCKET,
Key: process.env.S3_DATA_SOURCE
};
s3
.getObject(params)
.promise()
.then((data: any) => {
const list = JSON.parse(data.Body);
const messages = [];
for (const source of list) {
console.log(`Publishing request task for data source ${source.name}.`);
source.messageType = "FetchSource";
messages.push(source);
}
const chunks = _.chunk(messages, 10);
const tasks = [];
for (const chunk of chunks) {
const entries = [];
for (const source of chunk) {
entries.push({
Id: uuid(),
MessageBody: JSON.stringify(source)
});
}
const task = sqs
.sendMessageBatch({
QueueUrl: process.env.SQS_QUEUE_URL,
Entries: entries
})
.promise();
tasks.push(task);
}
return Promise.all(tasks);
})
.then(() => {
console.log("Published tasks!");
callback();
})
.catch(err => callback(err));
}