-
Notifications
You must be signed in to change notification settings - Fork 1
/
awsq.sh
executable file
·305 lines (254 loc) · 7.45 KB
/
awsq.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
scriptDir=""
os=`uname`
# OS specific part
case $os in
Darwin)
scriptDir=$(cd "$(dirname "$0")"; pwd)
;;
Linux)
scriptDir="$(dirname "$(readlink -f "$0")")"
;;
*)
echo "Unknown OS."
exit 1
;;
esac
q=${scriptDir}/$(basename $0)
type jq >/dev/null 2>&1 || { echo >&2 "Please install \"jq\". Try \"brew install jq\" or \"apt-get install jq\"." ; exit 1; }
type aws >/dev/null 2>&1 || { echo >&2 "Please install \"aws cli\". Try \"pip install awscli\"." ; exit 1; }
queue=''
shutdown=false
completeOnError=false
region='us-east-1'
emptyQueueTimeout=10
cmd=""
# Additional parameters
aargs=()
while [[ $# > 0 ]]
do
key="$1"
shift
case $key in
--queue)
queue="${1}"
shift
;;
--region)
region='${1}'
shift
;;
--shutdown-after-work)
shutdown=true
;;
--complete-on-error)
shutdown=true
;;
--emptyTimeout)
emptyQueueTimeout=${1}
shift
;;
*)
if [ -z "$cmd" ]
then
cmd="$key"
else
aargs+=($key)
fi
;;
esac
done
# In 30-second ticks
emptyQueueTimeout=$((emptyQueueTimeout * 2))
case $cmd in
worker|push|worker-screen|spawn-workers)
if [ -z "${queue}" ]
then
echo "Queue name not specified."
exit 1
fi
;;
esac
if [ -z "${cmd}" ]
then
echo "Action not specified."
exit 1
fi
queueURL=""
function ensure_queue() {
resp=$(aws sqs create-queue --queue-name ${queue} --region ${region})
if [[ "$?" != "0" ]]
then
echo "Error creating queue."
exit 1
fi
queueURL=$(echo ${resp} | jq -r '.QueueUrl')
}
function push {
msg="${1}"
msgArray=( $msg )
if ( [[ "${msgArray[0]}" != "s3://"* ]] && [[ "${msgArray[0]}" != "http://"* ]] && [[ "${msgArray[0]}" != "https://"* ]] )
then
echo "Error in task: ${msg}"
echo "First field of the message must be correct s3 address of task zip file. Found: ${msgArray[0]}"
echo "Like this: ./q.sh push --queue my-queue-name s3://mybucket/path/to/task.zip ./go.sh agr1 arg2 ..."
exit 1
fi
resp=$(aws sqs send-message --queue-url ${queueURL} --region ${region} --message-body "Q $msg")
if [[ "$?" != "0" ]]
then
echo "Error pushing task."
exit 1
fi
msgId=$(echo ${resp} | jq -r '.MessageId')
echo "Task successfully submitted. Id=${msgId}"
}
qworkerScreenPrefix="qworker_"
case $cmd in
spawn-workers)
workingDirectoryPrefix=${aargs[0]}
count=${aargs[1]}
for i in $(seq 1 ${count})
do
${q} worker-screen --queue ${queue} "${workingDirectoryPrefix}${i}"
done
if [[ $shutdown == true ]]; then
${q} wait-shutdown-screen
fi
;;
wait-shutdown-screen)
screen -d -m -S toShutdown ${q} wait-shutdown
;;
wait-shutdown)
while true
do
if screen -list | grep ${qworkerScreenPrefix} > /dev/null
then
echo "running"
else
break;
fi
sleep 100
done
sudo shutdown -h now
;;
worker-screen)
workingDirectory=${aargs[0]}
if [ -z "${workingDirectory}" ]
then
echo "Please specify working directory."
exit 1
fi
workingDirectoryName=$(basename ${workingDirectory})
screenName="${qworkerScreenPrefix}${workingDirectoryName}"
screen -d -m -S ${screenName} ${q} worker --queue ${queue} ${workingDirectory}
;;
# Arguments: ./q.sh working_folder
worker)
ensure_queue
# To stop underlying process with current if Ctrl-C is pressed
trap "trap - SIGTERM && kill -- -$$ && echo ''" SIGINT SIGTERM EXIT
workingDirectory=${aargs[0]}
if [ -z "${workingDirectory}" ]
then
echo "Please specify working directory."
exit 1
fi
# Timeout counter
attempt=0
while true;
do
content="$(aws sqs receive-message --region ${region} --queue-url ${queueURL} | tr '\n' ' ')"
if [ -z "$content" ]
then
attempt=$((attempt + 1))
if [ $attempt -gt $emptyQueueTimeout ]
then
exit 0
fi
echo "Nothing in queue. (#${attempt})"
sleep 30
continue
fi
attempt=0
body="$(echo ${content} | jq -r '.Messages[0].Body')"
body=$(echo ${body} | sed 's/^Q //')
echo ${body}
# Converting input message to array
bodyArray=( $body )
receiptHandle="$(echo ${content} | jq -r '.Messages[0].ReceiptHandle')"
execLine="$q worker-task ${workingDirectory} ${body}"
${execLine[@]} &
pid="$!"
while true
do
if kill -s 0 $pid > /dev/null 2>&1
then
aws sqs change-message-visibility --region ${region} --queue-url ${queueURL} --receipt-handle "${receiptHandle}" --visibility-timeout 120
echo "Prolonging visibility-timeout."
sleep 60
else
if wait $pid
then
echo "Process complete successfully. Deleting item from queue."
aws sqs delete-message --region ${region} --queue-url ${queueURL} --receipt-handle "${receiptHandle}"
else
echo "Process complete with error."
if [[ ${completeOnError} == true ]]
then
aws sqs delete-message --region ${region} --queue-url ${queueURL} --receipt-handle "${receiptHandle}"
fi
fi
break
fi
done
done
;;
# Don't invoke this action. Internally used in worker action.
# Arguments: working_folder s3://mybucket/mytask.zip cmd [arg0 [arg1 ...]]
worker-task)
workingDirectory=${aargs[0]}
taskFile=${aargs[1]}
cmd=${aargs[2]}
args="${aargs[@]:3}"
rm -rf ${workingDirectory}
mkdir -p ${workingDirectory}
fileName=$(basename ${taskFile})
cd ${workingDirectory}
if [[ "${taskFile}" == "s3://"* ]]; then
aws s3 cp ${taskFile} ${fileName}
elif [[ "${taskFile}" == "http://"* ]] || [[ "${taskFile}" == "https://"* ]]; then
curl ${taskFile} > ${fileName}
fi
if [[ ${fileName} == *".zip" ]]; then
unzip ${fileName}
rm ${fileName}
fi
if [[ "${cmd}" == "./"* ]];
then
chmod +x ${cmd}
fi
$cmd ${args[@]}
;;
push)
if [ -z "${aargs[0]}" ]
then
echo "Message not specified."
exit 1
fi
ensure_queue
if [ "${aargs[0]}" == "-" ]
then
while read line;
do
push "$line"
done
else
push "$(echo "${aargs[@]}")"
fi
;;
*)
echo "Unknown command ${cmd}."
exit 1
;;
esac