-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqspoll.sh
executable file
·94 lines (89 loc) · 2.66 KB
/
sqspoll.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
set -e
for x in "$@"; do
case $x in
--help|-h|help)
echo "Usage:"
echo "$0 <options>"
echo " -h this help message"
echo " -v verbose output"
echo " --timeout= sets the timeout to wait for new messages"
echo " --queue= sets the queue url"
echo " --run= the command to run (the body of the sqs message will be sent to stdin of this command)"
echo " --count= exit after x number of messages"
echo " --loop= exit after x number of timeouts"
echo " --region= specify the aws region for the queue url"
exit
;;
-v)
VERBOSE=1
;;
--timeout=*)
TIMEOUT=$(echo $x | cut -d= -f2)
;;
--region=*)
AWS_REGION=$(echo $x | cut -d= -f2)
;;
--queue=*)
QUEUE_URL=$(echo $x | cut -d= -f2)
;;
--run=*)
RUN_CMD=$(echo $x | cut -d= -f2)
;;
--count=*)
COUNT=$(echo $x | cut -d= -f2)
;;
--loop=*)
LOOP=$(echo $x | cut -d= -f2)
;;
esac
done
: ${AWS_REGION:?"You must provide the AWS region with --region"}
: ${QUEUE_URL:?"You must provide a queue url with --queue"}
: ${RUN_CMD:?"You must provide a run command with --run"}
: ${VERBOSE:="0"}
: ${TIMEOUT:="10"}
: ${COUNT:=""}
: ${LOOP:=""}
function log () {
[[ $VERBOSE == 1 ]] && logerr $@
return 0
}
function logerr () {
(>&2 echo $@)
return 0
}
set +e
log "Queue: $QUEUE_URL"
log "Timeout = $TIMEOUT"
while (true); do
logerr -n .
MESSAGES=$(aws --region "$AWS_REGION" sqs receive-message --queue-url "$QUEUE_URL" --wait-time-seconds "$TIMEOUT" --max-number-of-messages 1)
if [[ "$MESSAGES" != "" ]]; then
MESSAGE=$(echo $MESSAGES | jq '.Messages[]' -r)
RECEIPT=$(echo $MESSAGE | jq '.ReceiptHandle' -r)
BODY=$(echo $MESSAGE | jq '.Body' -r)
log
log "Receipt: $RECEIPT"
log "Got Message:"
log "$BODY"
(echo "$BODY" | $RUN_CMD) > /dev/null 2>&1
SUCCESS=$?
if [ "$SUCCESS" == "0" ]; then
aws --region "$AWS_REGION" sqs delete-message --queue-url "$QUEUE_URL" --receipt-handle "$RECEIPT"
logerr -n √
else
logerr -n X
fi
if [[ $COUNT != "" ]]; then
let COUNT--
log "Count: $COUNT"
(( $COUNT < 1 )) && exit
fi
fi
if [[ $LOOP != "" ]]; then
let LOOP--
log "Loop: $LOOP"
(( $LOOP < 1 )) && exit
fi
done