-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQS polling, fetching, and uploading to S3 are done in batch (#3)
- Loading branch information
Showing
7 changed files
with
105 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,47 @@ | ||
import fetch from 'node-fetch' | ||
import { writeOutput } from '../utils/writeOutput.js' | ||
import { fetchWithTimeout } from '../utils/consumer.js' | ||
|
||
const FETCH_TIMEOUT = 10000; | ||
|
||
/** | ||
* This script is the handler that performs any manipulation of the message | ||
* before it is uploaded to Amazon S3. It must return the string data that will | ||
* be written to the S3 bucket. | ||
* This script is an example of a handler function that sends an HTTP request to the url | ||
* and uploads the result to Amazon S3. You can change it to anything you want ;) | ||
* | ||
* @param {object} message The SQS message body as a JSON object | ||
* @param {object} messages The SQS messages batch as an array with JSON objects | ||
* @return {void} | ||
*/ | ||
export async function handler(message) { | ||
// Make the request | ||
const response = await fetch(message.url) | ||
|
||
// Get the response body as JSON | ||
const body = await response.json() | ||
export async function handler(messages) { | ||
try { | ||
// running multiple asynchronous tasks | ||
const responses = await Promise.allSettled( | ||
// mapping messages from producer to return promises array | ||
messages.map(async message => { | ||
const parsedMessage = JSON.parse(message.Body); | ||
const url = parsedMessage.url; | ||
const urlWithProtocol = url.startsWith("http") ? url : `http://${url}`; | ||
|
||
console.log("Handling url: ", urlWithProtocol); | ||
return await fetchWithTimeout(urlWithProtocol, FETCH_TIMEOUT); | ||
}) | ||
) | ||
|
||
// handling aggregated results | ||
console.log("Responses are back!"); | ||
const okResponses = responses | ||
.filter((res) => { | ||
if (res.status === "fulfilled") { | ||
console.log('Response OK from ', res.value.url); | ||
return res; | ||
} | ||
else { console.error(res.reason) } | ||
}) | ||
.map(res => res.value); | ||
|
||
// (TODO) Add your processing logic here... | ||
// upload to s3 bucket | ||
await writeOutput(okResponses); | ||
|
||
// Write the output to S3 | ||
await writeOutput(message, body) | ||
} | ||
} catch (error) { | ||
console.error('An error occurred:', error) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3' | ||
import uniqid from 'uniqid'; | ||
|
||
/** | ||
* Writes output to Amazon S3 | ||
* | ||
* @param {import('@aws-sdk/types').Message} message The SQS message | ||
* @param {object} output The output to write | ||
* @param {object[]} outputs The output to write | ||
* @return {void} | ||
*/ | ||
export async function writeOutput(message, output) { | ||
export async function writeOutput(outputs) { | ||
// Build the S3 client | ||
const client = new S3Client({ | ||
region: process.env.AWS_REGION | ||
}) | ||
|
||
// Write to S3 | ||
const s3Response = await client.send( | ||
new PutObjectCommand({ | ||
Body: JSON.stringify(output), | ||
Bucket: process.env.S3_BUCKET_ARN.split(':::')[1], | ||
Key: `${message.MessageId}.json` | ||
// Write to S3 | ||
const s3Responses = await Promise.allSettled( | ||
outputs.map(async output => { | ||
return await client.send( | ||
new PutObjectCommand({ | ||
Body: JSON.stringify(output.body), | ||
Bucket: process.env.S3_BUCKET_ARN.split(':::')[1], | ||
// Key: `${urlToFileString(output.url)}.json` | ||
Key: `${uniqid()}.json` | ||
}) | ||
) | ||
}) | ||
) | ||
|
||
console.log(`S3 Response: ${JSON.stringify(s3Response)}`) | ||
s3Responses.map((res) => { | ||
if (res.status === "fulfilled") { | ||
console.log(`S3 Response: `, res.value); | ||
} | ||
else { console.error(res.reason) } | ||
}); | ||
} | ||
|
||
function urlToFileString(url) { | ||
return new URL(url).host.replace(/\./g, '_'); | ||
} |