2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 15:52:18 +08:00
asynq/recoverer.go

109 lines
2.6 KiB
Go
Raw Normal View History

2020-06-21 22:05:57 +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 (
"context"
2020-06-21 22:05:57 +08:00
"sync"
"time"
"github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/log"
)
type recoverer struct {
logger *log.Logger
broker base.Broker
2021-01-13 03:40:26 +08:00
retryDelayFunc RetryDelayFunc
isFailureFunc func(error) bool
2020-06-21 22:05:57 +08:00
// channel to communicate back to the long running "recoverer" goroutine.
done chan struct{}
2020-08-10 21:10:14 +08:00
// list of queues to check for deadline.
queues []string
2020-06-21 22:05:57 +08:00
// poll interval.
interval time.Duration
}
type recovererParams struct {
logger *log.Logger
broker base.Broker
2020-08-10 21:10:14 +08:00
queues []string
2020-06-21 22:05:57 +08:00
interval time.Duration
2021-01-13 03:40:26 +08:00
retryDelayFunc RetryDelayFunc
isFailureFunc func(error) bool
2020-06-21 22:05:57 +08:00
}
func newRecoverer(params recovererParams) *recoverer {
return &recoverer{
logger: params.logger,
broker: params.broker,
done: make(chan struct{}),
2020-08-10 21:10:14 +08:00
queues: params.queues,
2020-06-21 22:05:57 +08:00
interval: params.interval,
retryDelayFunc: params.retryDelayFunc,
isFailureFunc: params.isFailureFunc,
2020-06-21 22:05:57 +08:00
}
}
func (r *recoverer) shutdown() {
2020-06-21 22:05:57 +08:00
r.logger.Debug("Recoverer shutting down...")
// Signal the recoverer goroutine to stop polling.
r.done <- struct{}{}
}
func (r *recoverer) start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
r.recover()
2020-06-21 22:05:57 +08:00
timer := time.NewTimer(r.interval)
for {
select {
case <-r.done:
r.logger.Debug("Recoverer done")
timer.Stop()
return
case <-timer.C:
r.recover()
timer.Reset(r.interval)
2020-06-21 22:05:57 +08:00
}
}
}()
}
func (r *recoverer) recover() {
// Get all tasks which have expired 30 seconds ago or earlier.
deadline := time.Now().Add(-30 * time.Second)
msgs, err := r.broker.ListDeadlineExceeded(deadline, r.queues...)
if err != nil {
r.logger.Warn("recoverer: could not list deadline exceeded tasks")
return
}
for _, msg := range msgs {
if msg.Retried >= msg.Retry {
r.archive(msg, context.DeadlineExceeded)
} else {
r.retry(msg, context.DeadlineExceeded)
}
}
}
func (r *recoverer) retry(msg *base.TaskMessage, err error) {
delay := r.retryDelayFunc(msg.Retried, err, NewTask(msg.Type, msg.Payload))
2020-06-21 22:05:57 +08:00
retryAt := time.Now().Add(delay)
if err := r.broker.Retry(msg, retryAt, err.Error(), r.isFailureFunc(err)); err != nil {
2020-06-21 22:05:57 +08:00
r.logger.Warnf("recoverer: could not retry deadline exceeded task: %v", err)
}
}
func (r *recoverer) archive(msg *base.TaskMessage, err error) {
if err := r.broker.Archive(msg, err.Error()); err != nil {
r.logger.Warnf("recoverer: could not move task to archive: %v", err)
2020-06-21 22:05:57 +08:00
}
}