2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00
asynq/scheduler.go

66 lines
1.3 KiB
Go
Raw Normal View History

2020-01-03 10:13:16 +08:00
// Copyright 2020 Kentaro Hibino. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
2019-12-29 05:33:24 +08:00
package asynq
import (
"log"
"time"
"github.com/hibiken/asynq/internal/rdb"
)
type scheduler struct {
rdb *rdb.RDB
// channel to communicate back to the long running "scheduler" goroutine.
done chan struct{}
// poll interval on average
avgInterval time.Duration
// list of queues to move the tasks into.
qnames []string
2019-12-29 05:33:24 +08:00
}
func newScheduler(r *rdb.RDB, avgInterval time.Duration, qcfg map[string]uint) *scheduler {
var qnames []string
for q := range qcfg {
qnames = append(qnames, q)
}
2019-12-29 05:33:24 +08:00
return &scheduler{
rdb: r,
done: make(chan struct{}),
avgInterval: avgInterval,
qnames: qnames,
2019-12-29 05:33:24 +08:00
}
}
func (s *scheduler) terminate() {
log.Println("[INFO] Scheduler shutting down...")
// Signal the scheduler goroutine to stop polling.
s.done <- struct{}{}
}
// start starts the "scheduler" goroutine.
func (s *scheduler) start() {
go func() {
for {
select {
case <-s.done:
log.Println("[INFO] Scheduler done.")
return
case <-time.After(s.avgInterval):
s.exec()
}
}
}()
}
func (s *scheduler) exec() {
if err := s.rdb.CheckAndEnqueue(s.qnames...); err != nil {
2019-12-29 05:33:24 +08:00
log.Printf("[ERROR] could not forward scheduled tasks: %v\n", err)
}
}