forked from xuri/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
351 lines (336 loc) · 8.42 KB
/
lib.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2016 - 2018 The aurora Authors. All rights reserved. Use of this
// source code is governed by a MIT license that can be found in the LICENSE
// file.
//
// The aurora is a web-based Beanstalk queue server console written in Go
// and works on macOS, Linux and Windows machines. Main idea behind using Go
// for backend development is to utilize ability of the compiler to produce
// zero-dependency binaries for multiple platforms. aurora was created as an
// attempt to build very simple and portable application to work with local or
// remote Beanstalk server.
package main
import (
"net/url"
"strconv"
"strings"
"time"
"github.com/xuri/aurora/beanstalk"
)
// addJob puts a job into tube by given config.
func addJob(server string, tube string, data string, priority string, delay string, TTR string) {
var err error
var tubePriority, tubeDelay, tubeTTR int
var bstkConn *beanstalk.Conn
tubePriority, err = strconv.Atoi(priority)
if err != nil {
tubePriority = DefaultPriority
}
tubeDelay, err = strconv.Atoi(delay)
if err != nil {
tubeDelay = DefaultDelay
}
tubeTTR, err = strconv.Atoi(TTR)
if err != nil {
tubeTTR = DefaultTTR
}
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
_, _ = bstkTube.Put([]byte(data), uint32(tubePriority), time.Duration(tubeDelay)*time.Second, time.Duration(tubeTTR)*time.Second)
bstkConn.Close()
}
// deleteJob delete a job in tube by given config.
func deleteJob(server string, tube string, jobID string) {
var err error
var id int
var bstkConn *beanstalk.Conn
id, err = strconv.Atoi(jobID)
if err != nil {
return
}
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
_ = bstkConn.Delete(uint64(id))
bstkConn.Close()
}
// deleteAll delete all jobs in tube by given server and tube.
func deleteAll(server string, tube string) {
var err error
var bstkConn *beanstalk.Conn
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
for {
readyJob, _, err := bstkTube.PeekReady()
if err != nil {
break
}
_ = bstkConn.Delete(readyJob)
}
for {
buriedJob, _, err := bstkTube.PeekBuried()
if err != nil {
break
}
_ = bstkConn.Delete(buriedJob)
}
for {
delayedJob, _, err := bstkTube.PeekDelayed()
if err != nil {
break
}
_ = bstkConn.Delete(delayedJob)
}
bstkConn.Close()
}
// kick takes up to bound jobs from the holding area and moves them into the
// ready queue, then returns the number of jobs moved. Jobs will be taken in the
// order in which they were last buried.
func kick(server string, tube string, count string) {
var err error
var bound int
var bstkConn *beanstalk.Conn
bound, err = strconv.Atoi(count)
if err != nil {
bound = 0
}
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
_, _ = bstkTube.Kick(bound)
bstkConn.Close()
}
// kickJob kick single job in tube by given server, tube name and job ID.
func kickJob(server string, tube string, id string) {
var err error
var bound int
var bstkConn *beanstalk.Conn
bound, err = strconv.Atoi(id)
if err != nil {
return
}
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
_ = bstkConn.KickJob(uint64(bound))
bstkConn.Close()
}
// pause pauses new reservations in tube for time duration.
func pause(server string, tube string, count string) {
var err error
var bstkConn *beanstalk.Conn
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
switch count {
case "-1": // Pause tube
if selfConf.TubePauseSeconds == -1 {
_ = bstkTube.Pause(DefaultTubePauseSeconds * time.Second)
} else {
_ = bstkTube.Pause(time.Duration(selfConf.TubePauseSeconds) * time.Second)
}
case "0":
_ = bstkTube.Pause(0 * time.Second) // Unpause tube
}
bstkConn.Close()
}
// moveJobsTo switch two case when move a job.
func moveJobsTo(server string, tube string, destTube string, state string, destState string) {
switch state {
case "ready": // ready to buried or ready
moveReadyJobsTo(server, tube, destTube, destState)
case "buried": // move job across the tube
moveBuriedJobsTo(server, tube, destTube, destState)
}
}
// moveReadyJobsTo process job moved origin stats in ready.
func moveReadyJobsTo(server string, tube string, destTube string, destState string) {
var err error
var bstkConn *beanstalk.Conn
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
switch destState {
case "buried":
tubeSet := beanstalk.NewTubeSet(bstkConn, tube)
for {
id, _, err := tubeSet.Reserve(time.Second)
if err != nil {
break
}
err = bstkConn.Bury(id, DefaultPriority)
if err != nil {
break
}
}
default:
if tube == destTube {
bstkConn.Close()
return
}
bstkDestTube := &beanstalk.Tube{
Conn: bstkConn,
Name: destTube,
}
for {
readyJob, readyBody, err := bstkTube.PeekReady()
if err != nil {
break
}
_, err = bstkDestTube.Put(readyBody, DefaultPriority, DefaultDelay, DefaultTTR)
if err != nil {
break
}
err = bstkConn.Delete(readyJob)
if err != nil {
break
}
}
}
bstkConn.Close()
}
// moveBuriedJobsTo process job moved origin stats in buried.
func moveBuriedJobsTo(server string, tube string, destTube string, destState string) {
var err error
var bstkConn *beanstalk.Conn
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return
}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
bstkDestTube := &beanstalk.Tube{
Conn: bstkConn,
Name: destTube,
}
for {
buriedJob, buriedBody, err := bstkTube.PeekBuried()
if err != nil {
break
}
_, err = bstkDestTube.Put(buriedBody, DefaultPriority, DefaultDelay, DefaultTTR)
if err != nil {
break
}
err = bstkConn.Delete(buriedJob)
if err != nil {
break
}
}
bstkConn.Close()
}
// clearTubes delete all jobs in all tubes by given server.
func clearTubes(server string, data url.Values) {
for tube := range data { // range over map
deleteAll(server, tube)
}
}
// searchTube search job by given search string in ready, delayed and buried
// stats.
func searchTube(server string, tube string, limit string, searchStr string) string {
var err error
var bstkConn *beanstalk.Conn
var searchLimit int
var table = currentTubeJobsSummaryTable(server, tube)
if table == `` {
return `Tube "` + tube + `" not found or it is empty <br><br><a href="./server?server=` + server + `"> << back </a>`
}
searchLimit, err = strconv.Atoi(limit)
if err != nil {
return table
}
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return table
}
result := []SearchResult{}
bstkTube := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
tubeStat, err := bstkTube.Stats()
if err != nil {
bstkConn.Close()
return table
}
// Get ready stat job total
statsFilter := []string{"ready", "delayed", "buried"}
jobsFilter := []string{"current-jobs-ready", "current-jobs-delayed", "current-jobs-buried"}
peek := []func() (id uint64, body []byte, err error){bstkTube.PeekReady, bstkTube.PeekDelayed, bstkTube.PeekBuried}
for k, v := range statsFilter {
s, err := strconv.Atoi(tubeStat[jobsFilter[k]])
if err != nil {
continue
}
var b uint64
if s > 0 {
b, _, err = peek[k]()
if err != nil {
continue
}
result = searchTubeInStats(tube, searchLimit, searchStr, bstkConn, result, b, s, v)
}
}
bstkConn.Close()
return table + currentTubeSearchResults(server, tube, limit, searchStr, result)
}
// searchTubeInStats search job in tube by given stats.
func searchTubeInStats(tube string, limit int, searchStr string, bstkConn *beanstalk.Conn, result []SearchResult, id uint64, cnt int, stat string) []SearchResult {
if cnt > limit {
cnt = limit
}
resultCnt := 0
for {
if resultCnt == cnt {
break
}
jobStats, err := bstkConn.StatsJob(id)
if err != nil {
break
}
if jobStats["tube"] != tube {
id++
continue
}
readyBody, err := bstkConn.Peek(id)
if err != nil {
continue
}
body := string(readyBody)
if !strings.Contains(body, searchStr) {
id++
resultCnt++
continue
}
job := SearchResult{
ID: id,
State: stat,
Data: string(readyBody),
}
result = append(result, job)
id++
resultCnt++
}
return result
}