-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller.go
68 lines (59 loc) · 1.27 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
package kick
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/go-redis/redis"
)
// Poller is responsible to move jobs from scheduledSet to normal list
type Poller struct {
queue *Queue
redisClient *redis.Client
stopPoll chan bool
*sync.WaitGroup
}
func NewPoller(queue *Queue) *Poller {
return &Poller{
queue,
queue.redisClient,
make(chan bool),
&sync.WaitGroup{},
}
}
func (p *Poller) Run() {
p.Add(1)
go p.poll()
}
func (p *Poller) Close() {
p.stopPoll <- true
}
func (p *Poller) poll() {
for {
select {
case <-p.stopPoll:
fmt.Println("Gracefully shutting poller")
p.Done()
return
case <-time.After(1 * time.Second):
z := redis.ZRangeBy{
Min: "-inf",
Max: fmt.Sprintf("%f", float64(time.Now().UnixNano())),
Count: 10,
}
vals, err := p.redisClient.ZRangeByScore(p.queue.ScheduledSetName(), z).Result()
if err != nil {
fmt.Println(err)
} else {
for _, val := range vals {
// TODO: ensure the ZREM operation is success
p.redisClient.ZRem(p.queue.ScheduledSetName(), val)
var job Job
json.Unmarshal([]byte(val), &job)
fmt.Printf("enqueue schedule job %s %s \n", job.JobDefinitionName, job.ID)
p.queue.EnqueueJob(time.Now().Add(0*time.Second), &job)
}
}
}
}
}