2019-11-19 13:23:49 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
2019-12-04 13:01:26 +08:00
|
|
|
|
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
2019-11-19 13:23:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type poller struct {
|
2019-12-04 13:01:26 +08:00
|
|
|
rdb *rdb.RDB
|
2019-11-19 13:23:49 +08:00
|
|
|
|
2019-11-19 23:38:09 +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
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:14:37 +08:00
|
|
|
func newPoller(r *rdb.RDB, avgInterval time.Duration) *poller {
|
2019-11-20 11:44:41 +08:00
|
|
|
return &poller{
|
2019-12-04 13:01:26 +08:00
|
|
|
rdb: r,
|
2019-11-20 11:44:41 +08:00
|
|
|
done: make(chan struct{}),
|
|
|
|
avgInterval: avgInterval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 13:23:49 +08:00
|
|
|
func (p *poller) terminate() {
|
2019-11-29 23:14:28 +08:00
|
|
|
log.Println("[INFO] 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-29 23:14:28 +08:00
|
|
|
log.Println("[INFO] Poller done.")
|
2019-11-19 23:38:09 +08:00
|
|
|
return
|
2019-11-19 22:20:59 +08:00
|
|
|
default:
|
2019-11-21 12:27:01 +08:00
|
|
|
p.exec()
|
2019-11-19 22:20:59 +08:00
|
|
|
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-12-04 23:28:57 +08:00
|
|
|
if err := p.rdb.CheckAndEnqueue(); err != nil {
|
2019-12-04 23:14:37 +08:00
|
|
|
log.Printf("[ERROR] could not forward scheduled tasks: %v\n", err)
|
2019-11-19 13:23:49 +08:00
|
|
|
}
|
|
|
|
}
|