-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
228 lines (196 loc) · 3.8 KB
/
queue.go
File metadata and controls
228 lines (196 loc) · 3.8 KB
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
package tinyq
import (
"context"
"fmt"
"sync"
"github.com/twiny/dice"
)
// State
type State string
const (
running State = "running"
paused State = "paused"
)
type Command string
const (
Start Command = "start"
Pause Command = "pause"
Retry Command = "retry"
Stop Command = "stop"
)
// Store
type Store interface {
Enqueue(msg Message) error
Dequeue() (Message, error)
Retry() error
Notify(uuid string, merr error) error
List(typ MessageStatus, offset, limit uint64) (Messages, error)
IsEmpty() bool
Remove(typ MessageStatus) error
Statistic() (Stats, error)
Close()
}
// Queue
type Queue struct {
l *sync.Mutex
wg *sync.WaitGroup
state State
store Store
stream chan Message
start chan struct{}
pause chan struct{}
ctx context.Context
cancel context.CancelFunc
}
// NewQueue
func NewQueue(store Store, autostart bool) *Queue {
ctx, cancel := context.WithCancel(context.Background())
q := &Queue{
l: &sync.Mutex{},
wg: &sync.WaitGroup{},
state: running,
store: store,
stream: make(chan Message, 1), // todo
start: make(chan struct{}, 1),
pause: make(chan struct{}, 1),
ctx: ctx,
cancel: cancel,
}
if !autostart {
q.pause <- struct{}{}
q.state = paused
}
q.routine()
return q
}
// routine
func (q *Queue) routine() {
q.wg.Add(1)
go func() {
defer func() {
q.wg.Done()
close(q.stream)
}()
//
for {
select {
case <-q.pause:
select {
case <-q.start:
case <-q.ctx.Done():
return
}
case <-q.ctx.Done():
return
default:
if q.ctx.Err() != nil {
return
}
if q.state == paused {
q.pause <- struct{}{}
q.state = paused
continue
}
if q.store.IsEmpty() {
q.pause <- struct{}{}
q.state = paused
continue
}
msg, err := q.store.Dequeue()
if err != nil {
q.pause <- struct{}{}
q.state = paused
continue
}
q.stream <- msg // this blocks
}
}
}()
}
// Exec
func (q *Queue) Exec(cmd Command) error {
q.l.Lock()
defer q.l.Unlock()
switch cmd {
case Start:
if q.state == running {
return fmt.Errorf("queue is already running")
}
if q.store.IsEmpty() {
return fmt.Errorf("queue is empty")
}
q.state = running
q.start <- struct{}{}
return nil
case Pause:
if q.state == paused {
return fmt.Errorf("queue is already paused")
}
q.pause <- struct{}{}
q.state = paused
return nil
case Retry:
return q.store.Retry()
case Stop:
if q.state == running {
q.pause <- struct{}{}
q.state = paused
// this should handle when queue is closed
// and queue is running. gracefule shutdown.
msg := <-q.stream
_ = q.Notify(msg.UUID, fmt.Errorf("queue is stopped")) // TODO: handle err
}
q.cancel()
return nil
default:
return fmt.Errorf("unknown command")
}
}
// Enqueue
func (q *Queue) Enqueue(v interface{}) error {
key := dice.RandString(25)
msg, err := NewMessage(key, v)
if err != nil {
return err
}
return q.store.Enqueue(msg)
}
// Dequeue
func (q *Queue) Dequeue() <-chan Message {
return q.stream
}
// Notify
func (q *Queue) Notify(uuid string, merr error) error {
return q.store.Notify(uuid, merr)
}
// List
func (q *Queue) List(typ MessageStatus, offset, limit uint64) (Messages, error) {
return q.store.List(typ, offset, limit)
}
// Remove
func (q *Queue) Remove(typ MessageStatus) error {
return q.store.Remove(typ)
}
// Stats
type Stats struct {
IsRunning bool `json:"is_running"`
Pending uint64 `json:"pending"`
Failed uint64 `json:"failed"`
}
// Statistic
func (q *Queue) Statistic() (Stats, error) {
stats, err := q.store.Statistic()
if err != nil {
return stats, err
}
stats.IsRunning = q.state == running
return stats, nil
}
// Close
func (q *Queue) Close() {
_ = q.Exec(Stop)
close(q.pause)
close(q.start)
// q.wg.Wait()
q.store.Close()
}