2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00
asynq/poller.go

60 lines
1.0 KiB
Go
Raw Normal View History

2019-11-19 13:23:49 +08:00
package asynq
import (
"fmt"
"log"
"time"
)
type poller struct {
2019-11-20 11:44:41 +08:00
rdb *rdb
2019-11-19 13:23:49 +08:00
// channel to communicate back to the long running "poller" goroutine.
2019-11-19 13:23:49 +08:00
done chan struct{}
// poll interval on average
avgInterval time.Duration
// redis ZSETs to poll
zsets []string
}
2019-11-20 11:44:41 +08:00
func newPoller(rdb *rdb, avgInterval time.Duration, zsets []string) *poller {
return &poller{
rdb: rdb,
done: make(chan struct{}),
avgInterval: avgInterval,
zsets: zsets,
}
}
2019-11-19 13:23:49 +08:00
func (p *poller) terminate() {
2019-11-27 22:33:04 +08:00
fmt.Print("Poller shutting down...")
2019-11-21 12:27:01 +08:00
// Signal the poller goroutine to stop polling.
2019-11-19 13:23:49 +08:00
p.done <- struct{}{}
}
2019-11-22 12:22:55 +08:00
// start starts the "poller" goroutine.
2019-11-19 13:23:49 +08:00
func (p *poller) start() {
go func() {
for {
select {
case <-p.done:
2019-11-27 22:33:04 +08:00
fmt.Println("Done")
return
default:
2019-11-21 12:27:01 +08:00
p.exec()
time.Sleep(p.avgInterval)
2019-11-19 13:23:49 +08:00
}
}
}()
}
2019-11-21 12:27:01 +08:00
func (p *poller) exec() {
2019-11-19 13:23:49 +08:00
for _, zset := range p.zsets {
2019-11-26 22:52:58 +08:00
if err := p.rdb.forward(zset); err != nil {
2019-11-27 22:21:57 +08:00
log.Printf("[ERROR] could not forward scheduled tasks from %q: %v\n", zset, err)
2019-11-19 13:23:49 +08:00
}
}
}