forked from digitalocean/go-workers2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
155 lines (131 loc) · 2.91 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
package workers
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/fcarrero/go-workers2/storage"
)
// Fetcher is an interface for managing work messages
type Fetcher interface {
Queue() string
Fetch()
Acknowledge(*Msg)
Ready() chan bool
Messages() chan *Msg
Close()
Closed() bool
}
type simpleFetcher struct {
store storage.Store
processID string
queue string
ready chan bool
messages chan *Msg
stop chan bool
exit chan bool
closed chan bool
logger *log.Logger
}
func newSimpleFetcher(queue string, opts Options) *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,
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() {
f.processOldMessages()
go func() {
for {
// f.Close() has been called
if f.Closed() {
break
}
<-f.Ready()
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) 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")
}