Skip to content

Instantly share code, notes, and snippets.

@jhorman
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhorman/6245edbf7008bc9edd86 to your computer and use it in GitHub Desktop.
Save jhorman/6245edbf7008bc9edd86 to your computer and use it in GitHub Desktop.
#!/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
@jhorman
Copy link
Author

jhorman commented Jan 8, 2015

Requires aws cli client, and http://stedolan.github.io/jq/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment