Last active
August 29, 2015 14:13
-
-
Save jhorman/6245edbf7008bc9edd86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
QUEUE= | |
echo "starting amazon sqs worker for queue $QUEUE" | |
while true | |
do | |
MESSAGE=`aws sqs receive-message --wait-time-seconds=1 --visibility-timeout=600 --queue-url=$QUEUE` | |
if [ $? -ne 0 ]; then | |
echo "aws sqs operation failed, trying again in 5s" | |
sleep 5 | |
continue | |
fi | |
NUM_MESSAGES=`echo $MESSAGE | jq '.Messages | length'` | |
if [ "$NUM_MESSAGES" -eq 0 ]; then | |
sleep 10 | |
continue | |
fi | |
HANDLE=`echo $MESSAGE | jq -r '.Messages[0].ReceiptHandle'` | |
TOPIC=`echo $MESSAGE | jq -r '.Messages[0].Body | fromjson | .TopicArn'` | |
echo "message received on topic $TOPIC" | |
# run a process | |
if [ $? -ne 0 ]; then | |
echo "error processing message" | |
else | |
aws sqs delete-message --queue-url=$QUEUE --receipt-handle=$HANDLE | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires aws cli client, and http://stedolan.github.io/jq/