-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
247 lines (219 loc) · 5.59 KB
/
commands.go
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
package beanstalkg
import (
"fmt"
"strings"
"time"
)
func assertExpected(expected, got string) error {
if expected != got {
return UnexpectedResponse
}
return nil
}
func handleReserve(c *Connection, cmd string) (*Job, error) {
resp, err := c.GetResp(cmd)
if err != nil {
return nil, err
}
var id int
var bodyLen int
if strings.HasPrefix(resp, "RESERVED") {
_, err := fmt.Sscanf(resp, "RESERVED %d %d\r\n", &id, &bodyLen)
if err != nil {
return nil, err
}
} else {
return nil, stringToError(resp)
}
body, err := c.readBody(bodyLen)
return &Job{ID: id, Body: body, conn: c}, err
}
func handlePeek(c *Connection, cmd string) (*Job, error) {
resp, err := c.GetResp(cmd)
if err != nil {
return nil, err
}
var id int
var bodyLen int
if strings.HasPrefix(resp, "FOUND") {
_, err := fmt.Sscanf(resp, "FOUND %d %d\r\n", &id, &bodyLen)
if err != nil {
return nil, err
}
} else {
return nil, stringToError(resp)
}
body, err := c.readBody(bodyLen)
return &Job{ID: id, Body: body, conn: c}, err
}
func handlePut(c *Connection, cmd string) (int, error) {
resp, err := c.GetResp(cmd)
if err != nil {
return 0, err
}
switch {
case strings.HasPrefix(resp, "INSERTED"):
var id int
_, err = fmt.Sscanf(resp, "INSERTED %d\r\n", &id)
return id, err
case strings.HasPrefix(resp, "BURIED"):
var id int
_, err := fmt.Sscanf(resp, "BURIED %d\r\n", &id)
if err != nil {
return 0, UnexpectedResponse
}
return id, Buried
default:
return 0, stringToError(resp)
}
}
func (c *Connection) ReserveWithTimeout(timeout time.Duration) (*Job, error) {
cmd := fmt.Sprintf("reserve-with-timeout %d\r\n", int(timeout.Seconds()))
return handleReserve(c, cmd)
}
func (c *Connection) Reserve() (*Job, error) {
return handleReserve(c, "reserve\r\n")
}
func (c *Connection) Ignore(tubename string) (int, error) {
cmd := fmt.Sprintf("ignore %s\r\n", tubename)
resp, err := c.GetResp(cmd)
if err != nil {
return -1, err
}
var tubeNum int
_, err = fmt.Sscanf(resp, "WATCHING %d\r\n", &tubeNum)
if err != nil {
return -1, stringToError(resp)
}
return tubeNum, nil
}
func (c *Connection) Watch(tubename string) (int, error) {
cmd := fmt.Sprintf("watch %s\r\n", tubename)
resp, err := c.GetResp(cmd)
if err != nil {
return -1, err
}
var tubeNum int
_, err = fmt.Sscanf(resp, "WATCHING %d\r\n", &tubeNum)
if err != nil {
return -1, stringToError(tubename)
}
return tubeNum, nil
}
func (c *Connection) Use(tubename string) error {
if len(tubename) > 200 {
return InvalidTubeName
}
cmd := fmt.Sprintf("use %s", tubename)
res, _ := c.GetResp(cmd)
fmt.Println(res)
return assertExpected(fmt.Sprintf("USING %s\r\n", tubename), res)
}
func (c *Connection) PutBytes(data []byte, priority int, delay, timeToRun time.Duration) (int, error) {
cmd := fmt.Sprintf("put %d %d %d %d\r\n%s\r\n", priority, uint64(delay.Seconds()), uint64(timeToRun.Seconds()), len(data), string(data))
return handlePut(c, cmd)
}
func (c *Connection) PutString(data string, priority int, delay, timeToRun time.Duration) (int, error) {
cmd := fmt.Sprintf("put %d %d %d %d\r\n%s\r\n", priority, uint64(delay.Seconds()), uint64(timeToRun.Seconds()), len(data), data)
return handlePut(c, cmd)
}
func (c *Connection) Release(id int, priority int, delay time.Duration) error {
cmd := fmt.Sprintf("release %d %d %d\r\n", id, priority, int(delay.Seconds()))
resp, err := c.GetResp(cmd)
if err != nil {
return err
}
return assertExpected("RELEASED\r\n", resp)
}
func (c *Connection) Bury(id, priority int) error {
cmd := fmt.Sprintf("bury %d %d\r\n", id, priority)
resp, err := c.GetResp(cmd)
if err != nil {
return err
}
return assertExpected("BURIED\r\n", resp)
}
func (c *Connection) KickJob(id int) (error) {
cmd := fmt.Sprintf("kick-job %d\r\n", id)
resp, err := c.GetResp(cmd)
if err != nil {
return err
}
return assertExpected("KICKED\r\n", resp)
}
func (c *Connection) Kick(maxJobs int) (int, error) {
cmd := fmt.Sprintf("kick %d\r\n", maxJobs)
resp, err := c.GetResp(cmd)
if err != nil {
return 0, err
}
var id int
if strings.HasPrefix(resp, "KICKED") {
_, err := fmt.Sscanf(resp, "KICKED %d\r\n", &id)
if err != nil {
return 0, UnexpectedResponse
}
return id, nil
}
return 0, stringToError(resp)
}
func (c *Connection) Touch(id int) error {
cmd := fmt.Sprintf("touch %d\r\n", id)
resp, err := c.GetResp(cmd)
if err != nil {
return err
}
return assertExpected("TOUCHED\r\n", resp)
}
func (c *Connection) Delete(id int) error {
cmd := fmt.Sprintf("delete %d\r\n", id)
resp, err := c.GetResp(cmd)
if err != nil {
return err
}
return assertExpected("DELETED\r\n", resp)
}
func (c *Connection) PeekJob(id int) (*Job, error) {
resp, err := handlePeek(c, fmt.Sprintf("peek %d\r\n", id))
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Connection) PeekReady() (*Job, error) {
resp, err := handlePeek(c, "peek-ready\r\n")
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Connection) PeekDelayed() (*Job, error) {
resp, err := handlePeek(c, "peek-delayed\r\n")
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Connection) PeekBuried() (*Job, error) {
resp, err := handlePeek(c, "peek-buried\r\n")
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Connection) PauseTube(name string, delay time.Duration) error {
cmd := fmt.Sprintf("pause-tube %s %d\r\n", name, int(delay.Seconds()))
resp, err := c.GetResp(cmd)
if err != nil {
return err
}
return assertExpected("PAUSED\r\n", resp)
}
func (c *Connection) Quit() error {
defer c.connection.Close()
_, err := c.GetResp("quit\r\n")
if err != nil {
return err
}
return nil
}