-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtubes.go
115 lines (98 loc) · 2.37 KB
/
tubes.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
// Copyright 2014 David Persson. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
"time"
"github.com/kr/beanstalk"
)
type Tubes struct {
Names []string
Conns []beanstalk.Tube
All bool // Flag indicating if all available tubes are represented.
}
func (ts *Tubes) Reset() {
ts.Conns = ts.Conns[:0]
ts.Names = ts.Names[:0]
}
// Selects tubes.
func (ts *Tubes) Use(tns []string) {
ts.Reset()
ts.All = false
for _, tn := range tns {
ts.Conns = append(ts.Conns, beanstalk.Tube{conn, tn})
ts.Names = append(ts.Names, tn)
}
return
}
func (ts *Tubes) UseAll() {
ts.Reset()
ts.All = true
tns, _ := conn.ListTubes()
for _, tn := range tns {
ts.Conns = append(ts.Conns, beanstalk.Tube{conn, tn})
ts.Names = append(ts.Names, tn)
}
return
}
// Prints most important statistics for each tube.
func listTubes() {
lf := "%20s %10s %30s %30s\n"
fmt.Printf(lf, "", "paused", "ready/delayed/buried", "waiting/watching/using")
fmt.Println(strings.Repeat("-", 93))
for _, t := range cTubes.Conns {
var pf, wf, jf string
stats, _ := t.Stats()
if stats["pause"] == "0" {
pf = "-"
} else {
pf = fmt.Sprintf("%ss", stats["pause-time-left"])
}
jf = fmt.Sprintf(
"%d (%d) / %d / %d",
castStatsValue(stats["current-jobs-ready"]),
castStatsValue(stats["current-jobs-urgent"]),
castStatsValue(stats["current-jobs-delayed"]),
castStatsValue(stats["current-jobs-buried"]),
)
wf = fmt.Sprintf(
"%d / %d / %d",
castStatsValue(stats["current-waiting"]),
castStatsValue(stats["current-watching"]),
castStatsValue(stats["current-using"]),
)
fmt.Printf(lf, t.Name, pf, jf, wf)
}
fmt.Println()
}
func kickTubes(bound int) {
for _, t := range cTubes.Conns {
t.Kick(bound)
fmt.Printf("Kicked jobs in tube %s.\n", t.Name)
}
}
func pauseTubes(delay time.Duration) {
for _, t := range cTubes.Conns {
t.Pause(delay)
fmt.Printf("Paused tube %s for %v.\n", t.Name, delay)
}
}
func clearTubes(state string) {
cnt := 0
for _, t := range cTubes.Conns {
for {
if id, _, err := peekState(t, state); err == nil {
if err := conn.Delete(id); err != nil {
panic(fmt.Sprintf("Failed deleting job %v.\n", id))
}
cnt++
} else {
break
}
}
fmt.Printf("Tube %s cleared, %d %s jobs deleted.\n", t.Name, cnt, state)
}
}