-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.ts
58 lines (50 loc) · 1.77 KB
/
trigger.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
import aws from 'aws-sdk'
import fetch from 'isomorphic-fetch'
import { Site } from './types'
export const handler = async (
event: AWSLambda.ScheduledEvent,
context: AWSLambda.Context,
) => {
if (!process.env.SCRAPER_FUNCTION_NAME) {
return context.fail(
'Environment variable SCRAPER_FUNCTION_NAME must be set with the name of the AWS Lambda function to call that scrapes the sites.',
)
}
const response = await fetch(`${process.env.FRONT_PAGES_BASE_URL}/sites`)
const sites: { sites: Site[] } = await response.json()
// See https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
const lambda = new aws.Lambda({ region: process.env.AWS_REGION })
await Promise.all(
sites.sites.map(site => {
console.log(
`Invoking '${process.env.SCRAPER_FUNCTION_NAME}' for site '${site.name}'`,
)
// This invocation follows the example from
// https://stackoverflow.com/a/31745774/974981
return new Promise((resolve, reject) => {
lambda.invoke(
{
FunctionName: process.env.SCRAPER_FUNCTION_NAME as string,
Payload: JSON.stringify(site),
InvocationType: 'Event',
},
function(error, data) {
if (error) {
console.error(
`Invoking '${process.env.SCRAPER_FUNCTION_NAME}' for site '${site.name}' failed. Error: ${error}`,
)
reject()
}
console.log(
`Invoking '${process.env.SCRAPER_FUNCTION_NAME}' for site '${site.name}' succeeded.`,
)
if (data!.Payload) {
console.log(`Payload for site '${site.name}': ${data.Payload}`)
}
resolve()
},
)
})
}),
)
}