-
Notifications
You must be signed in to change notification settings - Fork 241
/
poller.go
143 lines (125 loc) · 2.84 KB
/
poller.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
package goworker
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
type poller struct {
process
isStrict bool
}
func newPoller(queues []string, isStrict bool) (*poller, error) {
process, err := newProcess("poller", queues)
if err != nil {
return nil, err
}
return &poller{
process: *process,
isStrict: isStrict,
}, nil
}
func (p *poller) getJob(conn *RedisConn) (*Job, error) {
for _, queue := range p.queues(p.isStrict) {
logger.Debugf("Checking %s", queue)
reply, err := conn.Do("LPOP", fmt.Sprintf("%squeue:%s", workerSettings.Namespace, queue))
if err != nil {
return nil, err
}
if reply != nil {
logger.Debugf("Found job on %s", queue)
job := &Job{Queue: queue}
decoder := json.NewDecoder(bytes.NewReader(reply.([]byte)))
if workerSettings.UseNumber {
decoder.UseNumber()
}
if err := decoder.Decode(&job.Payload); err != nil {
return nil, err
}
return job, nil
}
}
return nil, nil
}
func (p *poller) poll(interval time.Duration, quit <-chan bool) (<-chan *Job, error) {
jobs := make(chan *Job)
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in poller %s: %v", p, err)
close(jobs)
return nil, err
} else {
p.open(conn)
p.start(conn)
PutConn(conn)
}
go func() {
defer func() {
close(jobs)
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in poller %s: %v", p, err)
return
} else {
p.finish(conn)
p.close(conn)
PutConn(conn)
}
}()
for {
select {
case <-quit:
return
default:
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in poller %s: %v", p, err)
return
}
job, err := p.getJob(conn)
if err != nil {
logger.Criticalf("Error on %v getting job from %v: %v", p, p.Queues, err)
PutConn(conn)
return
}
if job != nil {
conn.Send("INCR", fmt.Sprintf("%sstat:processed:%v", workerSettings.Namespace, p))
conn.Flush()
PutConn(conn)
select {
case jobs <- job:
case <-quit:
buf, err := json.Marshal(job.Payload)
if err != nil {
logger.Criticalf("Error requeueing %v: %v", job, err)
return
}
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection in poller %s: %v", p, err)
return
}
conn.Send("LPUSH", fmt.Sprintf("%squeue:%s", workerSettings.Namespace, job.Queue), buf)
conn.Flush()
PutConn(conn)
return
}
} else {
PutConn(conn)
if workerSettings.ExitOnComplete {
return
}
logger.Debugf("Sleeping for %v", interval)
logger.Debugf("Waiting for %v", p.Queues)
timeout := time.After(interval)
select {
case <-quit:
return
case <-timeout:
}
}
}
}
}()
return jobs, nil
}