-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.go
executable file
·265 lines (220 loc) · 5.21 KB
/
queue.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
package qless
import (
"time"
"github.com/gomodule/redigo/redis"
)
type Queue interface {
Name() string
Info() QueueInfo
Jobs(state string, start, count int) ([]string, error)
CancelAll()
Pause()
Resume()
Put(class string, data interface{}, opt ...putOptionFn) (string, error)
PutOrReplace(class string, jid string, data interface{}, opt ...putOptionFn) (int64, error)
PopOne() (j Job, err error)
Pop(count int) ([]Job, error)
Recur(class string, data interface{}, interval int, opt ...putOptionFn) (string, error)
Len() (int64, error)
Stats(time.Time) (*QueueStatistics, error)
}
type queue struct {
name string
info QueueInfo
c *Client
}
func (q *queue) Name() string {
return q.name
}
func (q *queue) Info() QueueInfo {
q.c.queueInfo(q.name, &q.info)
return q.info
}
func (q *queue) Jobs(state string, start, count int) ([]string, error) {
reply, err := redis.Values(q.c.Do("jobs", timestamp(), state, q.name))
if err != nil {
return nil, err
}
ret := []string{}
for _, val := range reply {
s, _ := redis.String(val, err)
ret = append(ret, s)
}
return ret, err
}
// Cancel all jobs in this queue
func (q *queue) CancelAll() {
for _, state := range jobStates {
var jids []string
for {
jids, _ = q.Jobs(state, 0, 100)
for _, jid := range jids {
j, err := q.c.GetRecurringJob(jid)
if j != nil && err == nil {
j.Cancel()
}
}
if len(jids) < 100 {
break
}
}
}
}
func (q *queue) Pause() {
q.c.Do("pause", timestamp(), q.name)
}
func (q *queue) Resume() {
q.c.Do("unpause", timestamp(), q.name)
}
type putData struct {
jid string
delay int
args []interface{}
}
func newPutData() putData {
return putData{}
}
func (p *putData) setOptions(opt []putOptionFn) (err error) {
for _, fn := range opt {
fn(p)
}
if p.jid == "" {
p.jid, err = generateUUID()
}
return
}
type putOptionFn func(d *putData)
func putOptionNoOp(*putData) {}
func WithJID(v string) putOptionFn {
return func(p *putData) {
p.jid = v
}
}
func WithDelay(v int) putOptionFn {
return func(p *putData) {
p.delay = v
}
}
func WithPriority(v int) putOptionFn {
return func(p *putData) {
p.args = append(p.args, "priority", v)
}
}
func WithRetries(v int) putOptionFn {
return func(p *putData) {
p.args = append(p.args, "retries", v)
}
}
func WithTags(v ...string) putOptionFn {
if v == nil {
return putOptionNoOp
}
return func(p *putData) {
p.args = append(p.args, "tags", marshal(v))
}
}
func WithDepends(v ...string) putOptionFn {
if v == nil {
return putOptionNoOp
}
return func(p *putData) {
p.args = append(p.args, "depends", marshal(v))
}
}
func WithResources(v ...string) putOptionFn {
if v == nil {
return putOptionNoOp
}
return func(p *putData) {
p.args = append(p.args, "resources", marshal(v))
}
}
func WithInterval(v float32) putOptionFn {
return func(p *putData) {
p.args = append(p.args, "interval", v)
}
}
// Put enqueues a job to the named queue
func (q *queue) Put(class string, data interface{}, opt ...putOptionFn) (string, error) {
pd := newPutData()
if err := pd.setOptions(opt); err != nil {
return "", err
}
args := []interface{}{"put", timestamp(), "", q.name, pd.jid, class, marshal(data), pd.delay}
args = append(args, pd.args...)
return redis.String(q.c.Do(args...))
}
// Put enqueues a job to the named queue
func (q *queue) PutOrReplace(class string, jid string, data interface{}, opt ...putOptionFn) (int64, error) {
pd := newPutData()
if err := pd.setOptions(opt); err != nil {
return -1, err
}
args := []interface{}{"put", timestamp(), "", q.name, jid, class, marshal(data), pd.delay}
args = append(args, pd.args...)
args = append(args, "replace", 0)
r, err := q.c.Do(args...)
if err != nil {
return -1, err
}
if r, ok := r.(int64); ok {
return r, nil
}
return -1, nil
}
func (q *queue) Recur(class string, data interface{}, interval int, opt ...putOptionFn) (string, error) {
pd := newPutData()
if err := pd.setOptions(opt); err != nil {
return "", err
}
args := []interface{}{"recur", timestamp(), "on", q.name, pd.jid, class, marshal(data), "interval", interval, pd.delay}
args = append(args, pd.args...)
return redis.String(q.c.Do(args...))
}
// Pops a job off the queue.
func (q *queue) PopOne() (j Job, err error) {
var jobs []Job
if jobs, err = q.Pop(1); err == nil && len(jobs) == 1 {
j = jobs[0]
}
return
}
// Put a recurring job in this queue
func (q *queue) Pop(count int) ([]Job, error) {
if count == 0 {
count = 1
}
data, err := redis.Bytes(q.c.Do("pop", timestamp(), q.name, workerNameStr, count))
if err != nil {
return nil, err
}
if len(data) == 2 {
return nil, nil
}
var jobsData jobDataArray
err = unmarshal(data, &jobsData)
if err != nil {
return nil, err
}
jobs := make([]Job, len(jobsData))
for i, v := range jobsData {
jobs[i] = &job{d: &v, c: q.c}
}
return jobs, nil
}
func (q *queue) Len() (int64, error) {
reply, err := redis.Int64(q.c.Do("length", timestamp(), q.name))
if err != nil {
return -1, err
}
return reply, nil
}
func (q *queue) Stats(d time.Time) (*QueueStatistics, error) {
data, err := redis.Bytes(q.c.Do("stats", timestamp(), q.name, d.Unix()))
if err != nil {
return nil, err
}
var qs QueueStatistics
err = unmarshal(data, &qs)
return &qs, err
}