This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beanstalk-client.sh
executable file
·276 lines (223 loc) · 7.01 KB
/
beanstalk-client.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
#!/bin/bash
# XXX: We should keep the connection open, for example with coproc or a file descriptor
# XXX: add the possibilty
# XXX: Release and bury doesn't work because the connection is not keeped open
SELF="${BASH_SOURCE[0]##*/}"
NAME="${SELF%.sh}"
OPTS="t:H:P:psvxeh"
USAGE="Usage: $SELF [$OPTS]"
HELP="
$USAGE
Options:
-t tube
-H Host
-P Port
-s simulate
-p set -o pipefail
-v set -v
-x set -x
-e set -ve
-h Help
Methods
put
Put save data to beanstalk
Syntax: put <pri> <delay> <ttr> <data>
Examples: $SELF -H localhost -P 11300 -t new put 100 0 100 fuu=uuf
Return: INSERTED JOBID
Note: depends on -t TUBE
peek-ready
Get the last job of a specific tube
Syntax: peek-ready
Examples: $SELF -H localhost -P 11300 -t new peek-ready
Return: FOUND JOBID BYTES
DATA
Note: depends on -t TUBE
delete
Delete a job
Syntax: delete JOBID
Examples: $SELF -H localhost -P 11300 -t new delete JOBID
Return: DELETED
Note: Depends on -t TUBE
stats or list-tubes
List tubes or give stats of beanstalk
Syntax: [stats|list-tubes]
Examples: $SELF -H localhost -P 11300 -t new [stats|list-tubes]
Return: the data you asked
Note: n/a
watch
Watch the tube and return the job data, it always removes the jobs after ouputing data
Syntax: watch
Examples: $SELF -H localhost -P 11300 -t new watch
Return: The data of the job
Note: depends on -t TUBE
reserve
Reserve a job, if no job is provided, it will keep looking for a new job
Syntax: reserve
Examples: $SELF -H localhost -P 11300 -t new reserve
Return: RESERVED JOBID BYTES
DATA
Note: depends on -t TUBE
bury
Bury a job
Syntax: bury
Examples: $SELF H localhost -P 11300 -t new bury jobid priority
Return: BURIED
Note: depends on -t TUBE, it doesn't work for the moment, because we don't reserve a job correctly
release
Release a job
Syntax: bury
Examples: $SELF H localhost -P 11300 -t new release jobid priority delay
Return: RELEASED
Note: depends on -t TUBE, it doesn't work for the moment, because we don't reserve a job correctly
"
function _quit ()
{
local retCode="$1" msg="${@:2}"
echo -e "$msg"
exit "$retCode"
}
function my_nc ()
{
nc -w 1 -C "$_host" "$_port"
}
function my_sed ()
{
sed 's/^[ \t]*//'
}
function my_grep ()
{
egrep -v '(UNKNOWN_COMMAND|USING)'
}
function value-checker ()
{
for value in "$@"
do
[[ -z "${!value}" ]] && _quit 2 "Value ($value) is not set! $HELP"
done
}
function put ()
{
local priority="$1" delay="$2" ttr="$3" data="$4"
# check if all values are set
value-checker priority delay ttr data _tube
# get custom byte
bytes="${#data}"
# use sed to just have a propper tabulation :)
echo -e "\
use $_tube\r\n\
put $priority $delay $ttr $bytes\r\n\
$data\r\n
" | my_sed | my_nc | my_grep
}
function peek-ready ()
{
# check if values are set
value-checker _tube
echo -e "\
use $_tube\r\n\
peek-ready\r\n
" | my_sed | my_nc | my_grep
}
function delete ()
{
local jobid="$1"
# check if values are set
value-checker _tube jobid
echo -e "\
use $_tube\r\n\
delete $jobid\r\n
" | my_sed | my_nc | my_grep
}
function bury ()
{
local jobid="$1" priority="$2"
# check if values are set
value-checker _tube jobid priority
echo -e "\
use $_tube\r\n\
bury $jobid $priority\r\n
" | my_sed | my_nc | my_grep
}
function reserve ()
{
# check if values are set
value-checker _tube
echo -e "\
use $_tube\r\n\
reserve\r\n
" | my_sed | my_nc | my_grep
}
function release ()
{
local jobid="$1" priority="$2" delay="$3"
# check if values are set
value-checker _tube jobid priority delay
echo -e "\
use $_tube\r\n\
release $jobid $priority $delay\r\n
" | my_sed | my_nc | my_grep
}
function watch ()
{
# check if values are set
value-checker _tube
# keep connection
while true
do
while read -ra lines
do
# check what the line contains
if [[ "${lines[0]}" =~ "FOUND"* ]]
then
jobid="${lines[1]}"
elif ! [[ "${lines[0]}" =~ "NOT_FOUND"* ]]
then
# save data
data+=" ${lines[@]} "
fi
done < <(peek-ready)
if [[ ! -z "$jobid" ]]
then
echo -e " $jobid = $data"
delete "$jobid" | grep -v DELETE
unset jobid data
fi
# sleep a secone my little while
sleep 1
done
}
function beanstalk-basic-runner ()
{
local beanstalk_command="$@"
echo "$beanstalk_command" | my_sed | my_nc | my_grep
}
while getopts "${OPTS}" arg; do
case "${arg}" in
t) _tube="${OPTARG}" ;;
H) _host="${OPTARG}" ;;
P) _port="${OPTARG}" ;;
s) _run="echo" ;;
p) set -o pipefail ;;
v) set -v ;;
x) set -x ;;
e) set -ve ;;
h) _quit 0 "$HELP" ;;
?) _quit 1 "Invalid Argument: $USAGE" ;;
*) _quit 1 "$USAGE" ;;
esac
done
shift $((OPTIND - 1))
value-checker _host _port
_action="$1"
case "$_action" in
put) put "$2" "$3" "$4" "$5" "${@:6}" ;;
peek-ready) peek-ready ;;
list-tubes) beanstalk-basic-runner list-tubes ;;
stats) beanstalk-basic-runner stats ;;
delete) delete "$2" ;;
bury) bury "$2" "$3" ;;
reserve) reserve ;;
release) release "$2" "$3" "$4" ;;
watch) watch ;;
*) _quit 2 "Action not Found! $HELP" ;;
esac