2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00
asynq/poller.go
2019-12-04 07:28:57 -08:00

55 lines
982 B
Go

package asynq
import (
"log"
"time"
"github.com/hibiken/asynq/internal/rdb"
)
type poller struct {
rdb *rdb.RDB
// channel to communicate back to the long running "poller" goroutine.
done chan struct{}
// poll interval on average
avgInterval time.Duration
}
func newPoller(r *rdb.RDB, avgInterval time.Duration) *poller {
return &poller{
rdb: r,
done: make(chan struct{}),
avgInterval: avgInterval,
}
}
func (p *poller) terminate() {
log.Println("[INFO] Poller shutting down...")
// Signal the poller goroutine to stop polling.
p.done <- struct{}{}
}
// start starts the "poller" goroutine.
func (p *poller) start() {
go func() {
for {
select {
case <-p.done:
log.Println("[INFO] Poller done.")
return
default:
p.exec()
time.Sleep(p.avgInterval)
}
}
}()
}
func (p *poller) exec() {
if err := p.rdb.CheckAndEnqueue(); err != nil {
log.Printf("[ERROR] could not forward scheduled tasks: %v\n", err)
}
}