-
Notifications
You must be signed in to change notification settings - Fork 2
/
disque.go
227 lines (176 loc) · 4.57 KB
/
disque.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
package disque
import (
"time"
"github.com/gomodule/redigo/redis"
)
type AddOptions struct {
Async bool `redis:"-"`
Delay time.Duration `redis:"-"`
MaxLen uint `redis:"MAXLEN,omitempty"`
Replicate uint16 `redis:"REPLICATE,omitempty"`
Retry time.Duration `redis:"-"`
TTL time.Duration `redis:"-"`
}
type GetOptions struct {
Count uint `redis:"COUNT,omitempty"`
NoHang bool `redis:"-"`
Timeout time.Duration `redis:"-"`
WithCounters bool `redis:"-"`
}
type Job struct {
Queue, ID, Body string
Nacks, AdditionalDeliveries int64
}
type Pool struct{ redis.Pool }
func New(addr string, idleTimeout time.Duration) *Pool {
return &Pool{
redis.Pool{
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr) },
IdleTimeout: idleTimeout,
MaxIdle: 3,
TestOnBorrow: func(c redis.Conn, _ time.Time) error {
_, err := c.Do("PING")
return err
},
},
}
}
func (p *Pool) Ack(jobs ...Job) error {
ids := make([]interface{}, len(jobs))
for i, job := range jobs {
ids[i] = job.ID
}
conn := p.Pool.Get()
defer conn.Close()
return conn.Send("ACKJOB", ids...)
}
func (p *Pool) Add(q, job string, t time.Duration, o *AddOptions) (string, error) {
// Convert TIMEOUT to milliseconds.
args := redis.Args{q, job, t.Nanoseconds() / 1e6}.AddFlat(o)
if o != nil {
if o.Async {
args = append(args, "ASYNC")
}
if o.Delay >= time.Second {
// Convert DELAY to seconds.
args = append(args, "DELAY", int64(o.Delay/time.Second))
}
if o.Retry >= time.Second {
// Convert Retry to seconds.
args = append(args, "RETRY", int64(o.Retry/time.Second))
}
if o.TTL >= time.Second {
// Convert TTL to seconds.
args = append(args, "TTL", int64(o.TTL/time.Second))
}
}
conn := p.Pool.Get()
defer conn.Close()
return redis.String(conn.Do("ADDJOB", args...))
}
func (p *Pool) FastAck(jobs ...Job) error {
ids := make([]interface{}, len(jobs))
for i, job := range jobs {
ids[i] = job.ID
}
conn := p.Pool.Get()
defer conn.Close()
return conn.Send("FASTACK", ids...)
}
func (p *Pool) Get(o *GetOptions, q ...string) ([]Job, error) {
args := redis.Args{}.AddFlat(o)
if o != nil {
if o.NoHang {
args = append(args, "NOHANG")
}
if o.Timeout >= time.Millisecond {
args = append(args, "TIMEOUT", int64(o.Timeout/time.Millisecond))
}
if o.WithCounters {
args = append(args, "WITHCOUNTERS")
}
}
args = args.Add("FROM").AddFlat(q)
conn := p.Pool.Get()
defer conn.Close()
var jobs []Job
reply, err := conn.Do("GETJOB", args...)
if rows, ok := reply.([]interface{}); ok {
for _, row := range rows {
data := row.([]interface{})
job := Job{
Queue: string(data[0].([]byte)),
ID: string(data[1].([]byte)),
Body: string(data[2].([]byte)),
}
if o != nil && o.WithCounters {
job.Nacks = data[4].(int64)
job.AdditionalDeliveries = data[6].(int64)
}
jobs = append(jobs, job)
}
}
return jobs, err
}
func (p *Pool) Len(queue string) (int, error) {
conn := p.Pool.Get()
defer conn.Close()
return redis.Int(conn.Do("QLEN", queue))
}
func (p *Pool) Nack(jobs ...Job) error {
ids := make([]interface{}, len(jobs))
for i, job := range jobs {
ids[i] = job.ID
}
conn := p.Pool.Get()
defer conn.Close()
return conn.Send("NACK", ids...)
}
func (p *Pool) Ping() (string, error) {
conn := p.Pool.Get()
defer conn.Close()
return redis.String(conn.Do("PING"))
}
func (p *Pool) Stat(queue string) (map[string]interface{}, error) {
conn := p.Pool.Get()
defer conn.Close()
reply, err := conn.Do("QSTAT", queue)
if err != nil {
return nil, err
}
values, ok := reply.([]interface{})
if !ok {
return nil, nil
}
stat := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key := string(values[i].([]byte))
switch value := values[i+1]; value.(type) {
case []byte:
stat[key] = string(value.([]byte))
case []interface{}:
values := value.([]interface{})
newValues := make([]string, len(values))
for i, v := range values {
newValues[i] = string(v.([]byte))
}
stat[key] = newValues
case int64:
stat[key] = value.(int64)
}
}
return stat, err
}
func (p *Pool) Working(job Job) (time.Duration, error) {
conn := p.Pool.Get()
defer conn.Close()
seconds, err := redis.Int(conn.Do("WORKING", job.ID))
return time.Second * time.Duration(seconds), err
}
// Private method used in testing.
// https://github.com/antirez/disque/issues/14
func (p *Pool) flush() {
conn := p.Pool.Get()
defer conn.Close()
conn.Send("DEBUG", "FLUSHALL")
}