-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.go
234 lines (206 loc) · 5.05 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
package q
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/liuzl/ds"
"github.com/liuzl/store"
"github.com/syndtr/goleveldb/leveldb/util"
"zliu.org/goutil"
)
type Queue struct {
Path string `json:"path"`
queue *ds.PriorityQueue
runningStore *store.LevelStore
exit chan bool
retryLimit int
}
type valueCnt struct {
Value []byte
Priority uint8
Cnt int
}
func NewQueueWithRetryLimit(path string, limit int) (*Queue, error) {
q, err := NewQueue(path)
if err != nil {
return nil, err
}
q.retryLimit = limit
return q, nil
}
func NewQueue(path string) (*Queue, error) {
q := &Queue{Path: path, exit: make(chan bool)}
var err error
queueDir := filepath.Join(path, "queue")
if q.queue, err = ds.OpenPriorityQueue(queueDir); err != nil {
return nil, err
}
storeDir := filepath.Join(path, "running")
if q.runningStore, err = store.NewLevelStore(storeDir); err != nil {
return nil, err
}
go q.retry()
return q, nil
}
func (q *Queue) Length() uint64 {
if q.queue == nil {
return 0
}
return q.queue.Length()
}
func (q *Queue) Status() map[string]interface{} {
if q.queue == nil {
return map[string]interface{}{"error": "queue is nil"}
}
var runningCount int64
if q.runningStore != nil {
q.runningStore.ForEach(nil, func(_, _ []byte) (bool, error) {
runningCount++
return true, nil
})
}
return map[string]interface{}{
"queue_length": q.queue.Length(),
"running_count": runningCount,
}
}
func (q *Queue) EnqueueWithPriority(data string, priority uint8) error {
if q.queue != nil {
_, err := q.queue.EnqueueObject(&valueCnt{Value: []byte(data), Priority: priority}, priority)
return err
}
return fmt.Errorf("queue is nil")
}
func (q *Queue) Enqueue(data string) error {
return q.EnqueueWithPriority(data, 128)
}
func (q *Queue) Peek() (string, error) {
if q.queue == nil || q.queue.Length() == 0 {
return "", fmt.Errorf("Queue is empty")
}
item, err := q.queue.Peek()
if err != nil {
return "", err
}
var v valueCnt
if err = store.BytesToObject(item.Value, &v); err != nil {
return "", err
}
return string(v.Value), nil
}
func (q *Queue) Dequeue(timeout int64) (string, string, error) {
if q.queue == nil || q.queue.Length() == 0 {
return "", "", fmt.Errorf("Queue is empty")
}
item, err := q.queue.Dequeue()
if err != nil {
return "", "", err
}
key := ""
var v valueCnt
if err = store.BytesToObject(item.Value, &v); err != nil {
return "", "", err
}
if timeout > 0 && (q.retryLimit <= 0 || v.Cnt < q.retryLimit) {
now := time.Now().Unix()
key = goutil.TimeStr(now+timeout) + ":" + goutil.ContentMD5(item.Value)
v.Cnt++
if err = q.addToRunning(key, &v); err != nil {
return "", "", err
}
}
return key, string(v.Value), nil
}
func (q *Queue) Confirm(key string) error {
if q.runningStore == nil {
return fmt.Errorf("runningStore is nil")
}
exists, err := q.runningStore.Has(key)
if err != nil {
return err
}
if exists {
return q.runningStore.Delete(key)
}
return fmt.Errorf("key not found: %s", key)
}
func (q *Queue) Close() {
if q.exit != nil {
q.exit <- true
}
if q.queue != nil {
q.queue.Close()
}
if q.runningStore != nil {
q.runningStore.Close()
}
}
func (q *Queue) Drop() {
q.Close()
os.RemoveAll(q.Path)
}
func (q *Queue) addToRunning(key string, item *valueCnt) error {
if len(item.Value) == 0 {
return fmt.Errorf("empty value")
}
if q.runningStore == nil {
return fmt.Errorf("runningStore is nil")
}
v, err := store.ObjectToBytes(item)
if err != nil {
return err
}
return q.runningStore.Put(key, v)
}
func (q *Queue) retry() {
for {
select {
case <-q.exit:
return
default:
now := goutil.TimeStr(time.Now().Unix())
q.runningStore.ForEach(&util.Range{Limit: []byte(now)},
func(key, value []byte) (bool, error) {
var v valueCnt
if err := store.BytesToObject(value, &v); err != nil {
return false, err
}
v.Priority = 0 // retry with highest priority
if _, err := q.queue.EnqueueObject(v, v.Priority); err != nil {
return false, err
}
q.runningStore.Delete(string(key))
return true, nil
})
goutil.Sleep(5*time.Second, q.exit)
}
}
}
// DequeueWithPreviousRetryCount removes and returns the next item in the queue
// along with the number of times it has been previously retried.
// If a timeout is specified, the item is added to the running queue.
func (q *Queue) DequeueWithPreviousRetryCount(timeout int64) (string, string, int, error) {
if q.queue == nil || q.queue.Length() == 0 {
return "", "", 0, fmt.Errorf("Queue is empty")
}
item, err := q.queue.Dequeue()
if err != nil {
return "", "", 0, err
}
key := ""
var v valueCnt
if err = store.BytesToObject(item.Value, &v); err != nil {
return "", "", 0, err
}
previousRetryCount := v.Cnt
if timeout > 0 && (q.retryLimit <= 0 || previousRetryCount < q.retryLimit) {
now := time.Now().Unix()
key = goutil.TimeStr(now+timeout) + ":" + goutil.ContentMD5(item.Value)
v.Cnt++
if err = q.addToRunning(key, &v); err != nil {
return "", "", 0, err
}
}
return key, string(v.Value), previousRetryCount, nil
}