2020-01-18 23:32:06 +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.
|
|
|
|
|
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
2020-02-16 15:14:30 +08:00
|
|
|
"sync"
|
2020-01-18 23:32:06 +08:00
|
|
|
"time"
|
2020-05-06 13:10:11 +08:00
|
|
|
|
|
|
|
"github.com/hibiken/asynq/internal/log"
|
2020-01-18 23:32:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// syncer is responsible for queuing up failed requests to redis and retry
|
|
|
|
// those requests to sync state between the background process and redis.
|
|
|
|
type syncer struct {
|
2020-05-06 13:10:11 +08:00
|
|
|
logger *log.Logger
|
2020-03-09 22:11:16 +08:00
|
|
|
|
2020-01-18 23:32:06 +08:00
|
|
|
requestsCh <-chan *syncRequest
|
|
|
|
|
|
|
|
// channel to communicate back to the long running "syncer" goroutine.
|
|
|
|
done chan struct{}
|
|
|
|
|
|
|
|
// interval between sync operations.
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type syncRequest struct {
|
2020-06-19 20:51:50 +08:00
|
|
|
fn func() error // sync operation
|
|
|
|
errMsg string // error message
|
|
|
|
deadline time.Time // request should be dropped if deadline has been exceeded
|
2020-01-18 23:32:06 +08:00
|
|
|
}
|
|
|
|
|
2020-05-18 03:33:55 +08:00
|
|
|
type syncerParams struct {
|
|
|
|
logger *log.Logger
|
|
|
|
requestsCh <-chan *syncRequest
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSyncer(params syncerParams) *syncer {
|
2020-01-18 23:32:06 +08:00
|
|
|
return &syncer{
|
2020-05-18 03:33:55 +08:00
|
|
|
logger: params.logger,
|
|
|
|
requestsCh: params.requestsCh,
|
2020-01-18 23:32:06 +08:00
|
|
|
done: make(chan struct{}),
|
2020-05-18 03:33:55 +08:00
|
|
|
interval: params.interval,
|
2020-01-18 23:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *syncer) terminate() {
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Syncer shutting down...")
|
2020-01-18 23:32:06 +08:00
|
|
|
// Signal the syncer goroutine to stop.
|
|
|
|
s.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:14:30 +08:00
|
|
|
func (s *syncer) start(wg *sync.WaitGroup) {
|
|
|
|
wg.Add(1)
|
2020-01-18 23:32:06 +08:00
|
|
|
go func() {
|
2020-02-16 15:14:30 +08:00
|
|
|
defer wg.Done()
|
2020-01-18 23:32:06 +08:00
|
|
|
var requests []*syncRequest
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.done:
|
|
|
|
// Try sync one last time before shutting down.
|
|
|
|
for _, req := range requests {
|
|
|
|
if err := req.fn(); err != nil {
|
2020-03-09 22:11:16 +08:00
|
|
|
s.logger.Error(req.errMsg)
|
2020-01-18 23:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Syncer done")
|
2020-01-18 23:32:06 +08:00
|
|
|
return
|
|
|
|
case req := <-s.requestsCh:
|
|
|
|
requests = append(requests, req)
|
|
|
|
case <-time.After(s.interval):
|
|
|
|
var temp []*syncRequest
|
|
|
|
for _, req := range requests {
|
2020-06-19 20:51:50 +08:00
|
|
|
if req.deadline.Before(time.Now()) {
|
|
|
|
continue // drop stale request
|
|
|
|
}
|
2020-01-18 23:32:06 +08:00
|
|
|
if err := req.fn(); err != nil {
|
|
|
|
temp = append(temp, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
requests = temp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|