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 (
|
2020-02-16 15:14:30 +08:00
|
|
|
"sync"
|
2019-12-29 05:33:24 +08:00
|
|
|
"time"
|
2020-04-18 22:55:10 +08:00
|
|
|
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2020-05-06 13:10:11 +08:00
|
|
|
"github.com/hibiken/asynq/internal/log"
|
2019-12-29 05:33:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type scheduler struct {
|
2020-05-06 13:10:11 +08:00
|
|
|
logger *log.Logger
|
2020-04-18 22:55:10 +08:00
|
|
|
broker base.Broker
|
2019-12-29 05:33:24 +08:00
|
|
|
|
|
|
|
// channel to communicate back to the long running "scheduler" goroutine.
|
|
|
|
done chan struct{}
|
|
|
|
|
2020-08-09 22:13:42 +08:00
|
|
|
// list of queue names to check and enqueue.
|
|
|
|
queues []string
|
|
|
|
|
2019-12-29 05:33:24 +08:00
|
|
|
// poll interval on average
|
|
|
|
avgInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:33:55 +08:00
|
|
|
type schedulerParams struct {
|
|
|
|
logger *log.Logger
|
|
|
|
broker base.Broker
|
2020-08-09 22:13:42 +08:00
|
|
|
queues []string
|
2020-05-18 03:33:55 +08:00
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func newScheduler(params schedulerParams) *scheduler {
|
2019-12-29 05:33:24 +08:00
|
|
|
return &scheduler{
|
2020-05-18 03:33:55 +08:00
|
|
|
logger: params.logger,
|
|
|
|
broker: params.broker,
|
2019-12-29 05:33:24 +08:00
|
|
|
done: make(chan struct{}),
|
2020-08-09 22:13:42 +08:00
|
|
|
queues: params.queues,
|
2020-05-18 03:33:55 +08:00
|
|
|
avgInterval: params.interval,
|
2019-12-29 05:33:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scheduler) terminate() {
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Scheduler shutting down...")
|
2019-12-29 05:33:24 +08:00
|
|
|
// Signal the scheduler goroutine to stop polling.
|
|
|
|
s.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start starts the "scheduler" goroutine.
|
2020-02-16 15:14:30 +08:00
|
|
|
func (s *scheduler) start(wg *sync.WaitGroup) {
|
|
|
|
wg.Add(1)
|
2019-12-29 05:33:24 +08:00
|
|
|
go func() {
|
2020-02-16 15:14:30 +08:00
|
|
|
defer wg.Done()
|
2019-12-29 05:33:24 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.done:
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Scheduler done")
|
2019-12-29 05:33:24 +08:00
|
|
|
return
|
|
|
|
case <-time.After(s.avgInterval):
|
|
|
|
s.exec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scheduler) exec() {
|
2020-08-09 22:13:42 +08:00
|
|
|
if err := s.broker.CheckAndEnqueue(s.queues...); err != nil {
|
2020-05-06 13:10:11 +08:00
|
|
|
s.logger.Errorf("Could not enqueue scheduled tasks: %v", err)
|
2019-12-29 05:33:24 +08:00
|
|
|
}
|
|
|
|
}
|