-
Notifications
You must be signed in to change notification settings - Fork 166
/
fetcher.go
156 lines (129 loc) · 2.63 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
package workers
import (
"fmt"
"time"
"github.com/garyburd/redigo/redis"
)
type Fetcher interface {
Queue() string
Fetch()
Acknowledge(*Msg)
Ready() chan bool
FinishedWork() chan bool
Messages() chan *Msg
Close()
Closed() bool
}
type fetch struct {
queue string
ready chan bool
finishedwork chan bool
messages chan *Msg
stop chan bool
exit chan bool
closed chan bool
}
func NewFetch(queue string, messages chan *Msg, ready chan bool) Fetcher {
return &fetch{
queue,
ready,
make(chan bool),
messages,
make(chan bool),
make(chan bool),
make(chan bool),
}
}
func (f *fetch) Queue() string {
return f.queue
}
func (f *fetch) processOldMessages() {
messages := f.inprogressMessages()
for _, message := range messages {
<-f.Ready()
f.sendMessage(message)
}
}
func (f *fetch) 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 *fetch) tryFetchMessage() {
conn := Config.Pool.Get()
defer conn.Close()
message, err := redis.String(conn.Do("brpoplpush", f.queue, f.inprogressQueue(), 1))
if err != nil {
// If redis returns null, the queue is empty. Just ignore the error.
if err.Error() != "redigo: nil returned" {
Logger.Println("ERR: ", err)
time.Sleep(1 * time.Second)
}
} else {
f.sendMessage(message)
}
}
func (f *fetch) sendMessage(message string) {
msg, err := NewMsg(message)
if err != nil {
Logger.Println("ERR: Couldn't create message from", message, ":", err)
return
}
f.Messages() <- msg
}
func (f *fetch) Acknowledge(message *Msg) {
conn := Config.Pool.Get()
defer conn.Close()
conn.Do("lrem", f.inprogressQueue(), -1, message.OriginalJson())
}
func (f *fetch) Messages() chan *Msg {
return f.messages
}
func (f *fetch) Ready() chan bool {
return f.ready
}
func (f *fetch) FinishedWork() chan bool {
return f.finishedwork
}
func (f *fetch) Close() {
f.stop <- true
<-f.exit
}
func (f *fetch) Closed() bool {
select {
case <-f.closed:
return true
default:
return false
}
}
func (f *fetch) inprogressMessages() []string {
conn := Config.Pool.Get()
defer conn.Close()
messages, err := redis.Strings(conn.Do("lrange", f.inprogressQueue(), 0, -1))
if err != nil {
Logger.Println("ERR: ", err)
}
return messages
}
func (f *fetch) inprogressQueue() string {
return fmt.Sprint(f.queue, ":", Config.processId, ":inprogress")
}