Last active
May 24, 2018 13:26
-
-
Save djfdyuruiry/96eb8edd967058654082d5c2ce9ee0e7 to your computer and use it in GitHub Desktop.
Pop off and log SQS messages from a given queue, both to std out and a log file (with an err log file)
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
#!/bin/bash | |
queueName="sns-queue" | |
endpointUrl="http://localhost:4576" | |
queueUrl="${endpointUrl}/queue/${queueName}" | |
queueLogFile="sqs_${queueName}.log" | |
queueErrorLogFile="sqs_${queueName}_err.log" | |
jsonLoadFromStdIn="json.load(sys.stdin)" | |
doJsonOperation() { | |
json="$1" | |
operation="$2" | |
operationWithJsonLoad=${operation/__JSON__/$jsonLoadFromStdIn} | |
echo "${json}" | python -c "import sys, json; print ${operationWithJsonLoad}" | |
} | |
logPoppedSqsMessage() { | |
messageJson="$1" | |
echo "$(date +"%D %T") - Popped message from SQS queue '${queueName}':" | tee -a ${queueLogFile} | |
echo "${messageJson}" | tee -a ${queueLogFile} | |
} | |
popMessageFromSqsQueue() { | |
sqsResponse="$1" | |
messageJson=$(doJsonOperation "${sqsResponse}" "json.dumps(json.loads(__JSON__['Messages'][0]['Body']), indent=4, sort_keys=True)") | |
receiptHandle=$(doJsonOperation "${sqsResponse}" "__JSON__['Messages'][0]['ReceiptHandle']") | |
aws sqs delete-message --queue-url ${queueUrl} --receipt-handle ${receiptHandle} --endpoint-url ${endpointUrl} 2>> ${queueErrorLogFile} | |
logPoppedSqsMessage "${messageJson}" | |
} | |
watchSqs() { | |
while true; do | |
sleep 0.25 | |
sqsResponse=$(aws sqs receive-message --queue-url ${queueUrl} --endpoint-url ${endpointUrl} 2>> ${queueErrorLogFile}) | |
if [ -z "${sqsResponse}" ]; then | |
continue | |
fi | |
hasMessages=$(doJsonOperation "${sqsResponse}" "len(__JSON__['Messages']) > 0") | |
if [ ${hasMessages} == "True" ]; then | |
popMessageFromSqsQueue "${sqsResponse}" | |
fi | |
done | |
} | |
watchSqs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment