-
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.
- Loading branch information
Showing
16 changed files
with
206 additions
and
10,090 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
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 |
---|---|---|
|
@@ -46,4 +46,4 @@ output.txt | |
output/ | ||
|
||
*.csv | ||
script/download | ||
script/download |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/bin/bash | ||
|
||
LOG_DIR="logs" | ||
|
||
mkdir -p "$LOG_DIR" | ||
|
||
|
||
LOG_GROUP="/aws/lambda/producer" | ||
|
||
# Get the latest log stream for the specified log group | ||
LOG_STREAM=$(aws logs describe-log-streams \ | ||
--log-group-name "$LOG_GROUP" \ | ||
--max-items 1 \ | ||
--order-by LastEventTime \ | ||
--descending \ | ||
--query "logStreams[].logStreamName" \ | ||
--output text | head -n 1) | ||
|
||
if [ -n "$LOG_STREAM" ]; then | ||
LOG_FILE="$LOG_DIR/producer.log" | ||
|
||
# Download the log events from the specified log stream | ||
aws logs get-log-events \ | ||
--log-group-name "$LOG_GROUP" \ | ||
--log-stream-name "$LOG_STREAM" \ | ||
--query "events[].message" \ | ||
--output text > "$LOG_FILE" | ||
|
||
echo "Downloaded producer log for $LOG_STREAM to $LOG_FILE" | ||
else | ||
echo "No log streams found for $LOG_GROUP" | ||
fi | ||
|
||
# Get a list of EC2 instance IDs | ||
INSTANCE_IDS=$(aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId" --filters Name=instance-state-name,Values=running --output text) | ||
|
||
# Loop through the instance IDs and download logs for each | ||
for INSTANCE_ID in $INSTANCE_IDS; do | ||
LOG_FILE="$LOG_DIR/$INSTANCE_ID.log" | ||
|
||
# Capture the system logs for the instance | ||
output=$(aws ec2 get-console-output --instance-id "$INSTANCE_ID" --query "Output" --output text) | ||
|
||
# Check if the output is not None before redirecting it to the log file | ||
if [ "$output" != "None" ]; then | ||
echo "$output" > "$LOG_FILE" | ||
echo "Downloaded consumer log for $INSTANCE_ID to $LOG_FILE" | ||
else | ||
echo "Empty consumer log for $INSTANCE_ID" | ||
fi | ||
done |
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,37 +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) { | ||
|
||
export async function handler(messages) { | ||
try { | ||
// Make the request | ||
await timeout(200 * Math.random()) | ||
const response = await fetch(`https://${message.url}`) | ||
// 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); | ||
|
||
// upload to s3 bucket | ||
await writeOutput(okResponses); | ||
|
||
// Write the output to S3 | ||
if (response.ok) { | ||
await writeOutput(urlToFileString(message.url), message.url) | ||
} | ||
} catch (error) { | ||
console.error('An error occurred:', error) | ||
} | ||
} | ||
|
||
function urlToFileString(url) { | ||
let urlWithoutProtocol = url.replace('https://', '').replace('http://', '') | ||
let fileCompatibleString = urlWithoutProtocol | ||
.replace(/\//g, '-') | ||
.replace(/\./g, '_') | ||
return fileCompatibleString | ||
} | ||
|
||
function timeout(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)) | ||
} | ||
} |
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
Oops, something went wrong.