-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathfetcher.go
185 lines (158 loc) · 3.38 KB
/
fetcher.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
package workers
import (
"context"
"fmt"
"log"
"os"
"sync"
"time"
"github.com/digitalocean/go-workers2/storage"
)
//Fetcher is an interface for managing work messages
type Fetcher interface {
Queue() string
InProgressQueue() string
Fetch()
Acknowledge(*Msg)
SetActive(bool)
IsActive() bool
Ready() chan bool
Messages() chan *Msg
Close()
Closed() bool
}
type simpleFetcher struct {
store storage.Store
processID string
queue string
lock sync.Mutex
isActive bool
ready chan bool
messages chan *Msg
stop chan bool
exit chan bool
closed chan bool
logger *log.Logger
}
func newSimpleFetcher(queue string, opts Options, isActive bool) *simpleFetcher {
logger := opts.Logger
if logger == nil {
logger = log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds)
}
return &simpleFetcher{
store: opts.store,
processID: opts.ProcessID,
queue: queue,
isActive: isActive,
ready: make(chan bool),
messages: make(chan *Msg),
stop: make(chan bool),
exit: make(chan bool),
closed: make(chan bool),
logger: logger,
}
}
func (f *simpleFetcher) Queue() string {
return f.queue
}
func (f *simpleFetcher) processOldMessages() {
messages := f.inprogressMessages()
for _, message := range messages {
<-f.Ready()
f.sendMessage(message)
}
}
func (f *simpleFetcher) Fetch() {
for !f.isActive {
select {
case <-f.stop:
close(f.closed)
close(f.exit)
return
}
}
f.processOldMessages()
go func() {
for {
// f.Close() has been called
if f.Closed() {
break
}
<-f.Ready()
if f.IsActive() {
f.tryFetchMessage()
}
}
}()
for {
select {
case <-f.stop:
// Stop the redis-polling goroutine
close(f.closed)
// Signal to Close() that the fetcher has stopped
close(f.exit)
break
}
}
}
func (f *simpleFetcher) tryFetchMessage() {
message, err := f.store.DequeueMessage(context.Background(), f.queue, f.InProgressQueue(), 1*time.Second)
if err != nil {
// If redis returns null, the queue is empty.
// Just ignore empty queue errors; print all other errors.
if err != storage.NoMessage {
f.logger.Println("ERR: ", f.queue, err)
}
} else {
f.sendMessage(message)
}
}
func (f *simpleFetcher) sendMessage(message string) {
msg, err := NewMsg(message)
if err != nil {
f.logger.Println("ERR: Couldn't create message from", message, ":", err)
return
}
f.Messages() <- msg
}
func (f *simpleFetcher) Acknowledge(message *Msg) {
f.store.AcknowledgeMessage(context.Background(), f.InProgressQueue(), message.OriginalJson())
}
func (f *simpleFetcher) Messages() chan *Msg {
return f.messages
}
func (f *simpleFetcher) SetActive(active bool) {
f.lock.Lock()
defer f.lock.Unlock()
f.isActive = active
}
func (f *simpleFetcher) IsActive() bool {
f.lock.Lock()
defer f.lock.Unlock()
return f.isActive
}
func (f *simpleFetcher) Ready() chan bool {
return f.ready
}
func (f *simpleFetcher) Close() {
f.stop <- true
<-f.exit
}
func (f *simpleFetcher) Closed() bool {
select {
case <-f.closed:
return true
default:
return false
}
}
func (f *simpleFetcher) inprogressMessages() []string {
messages, err := f.store.ListMessages(context.Background(), f.InProgressQueue())
if err != nil {
f.logger.Println("ERR: ", err)
}
return messages
}
func (f *simpleFetcher) InProgressQueue() string {
return fmt.Sprint(f.queue, ":", f.processID, ":inprogress")
}