Skip to content

Instantly share code, notes, and snippets.

@jhorman jhorman/sqs-worker.sh
Last active Aug 29, 2015

Embed
What would you like to do?
#!/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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.