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 (
|
2022-02-14 23:17:51 +08:00
|
|
|
"context"
|
2020-06-21 22:05:57 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2022-02-11 22:18:27 +08:00
|
|
|
"github.com/hibiken/asynq/internal/errors"
|
2020-06-21 22:05:57 +08:00
|
|
|
"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
|
2021-09-01 21:00:54 +08:00
|
|
|
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
|
2021-09-01 21:00:54 +08:00
|
|
|
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,
|
2021-09-01 21:00:54 +08:00
|
|
|
isFailureFunc: params.isFailureFunc,
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:54 +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()
|
2021-07-04 21:21:56 +08:00
|
|
|
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:
|
2021-07-04 21:20:36 +08:00
|
|
|
r.recover()
|
|
|
|
timer.Reset(r.interval)
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:18:27 +08:00
|
|
|
// ErrLeaseExpired error indicates that the task failed because the worker working on the task
|
|
|
|
// could not extend its lease due to missing heartbeats. The worker may have crashed or got cutoff from the network.
|
|
|
|
var ErrLeaseExpired = errors.New("asynq: task lease expired")
|
|
|
|
|
2021-07-04 21:20:36 +08:00
|
|
|
func (r *recoverer) recover() {
|
2022-02-11 22:18:27 +08:00
|
|
|
// Get all tasks which have expired 30 seconds ago or earlier to accomodate certain amount of clock skew.
|
|
|
|
cutoff := time.Now().Add(-30 * time.Second)
|
|
|
|
msgs, err := r.broker.ListLeaseExpired(cutoff, r.queues...)
|
2021-07-04 21:20:36 +08:00
|
|
|
if err != nil {
|
2022-02-11 22:18:27 +08:00
|
|
|
r.logger.Warn("recoverer: could not list lease expired tasks")
|
2021-07-04 21:20:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, msg := range msgs {
|
|
|
|
if msg.Retried >= msg.Retry {
|
2022-02-11 22:18:27 +08:00
|
|
|
r.archive(msg, ErrLeaseExpired)
|
2021-07-04 21:20:36 +08:00
|
|
|
} else {
|
2022-02-11 22:18:27 +08:00
|
|
|
r.retry(msg, ErrLeaseExpired)
|
2021-07-04 21:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:00:54 +08:00
|
|
|
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)
|
2022-02-14 23:17:51 +08:00
|
|
|
if err := r.broker.Retry(context.Background(), msg, retryAt, err.Error(), r.isFailureFunc(err)); err != nil {
|
2022-02-11 22:18:27 +08:00
|
|
|
r.logger.Warnf("recoverer: could not retry lease expired task: %v", err)
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:00:54 +08:00
|
|
|
func (r *recoverer) archive(msg *base.TaskMessage, err error) {
|
2022-02-14 23:17:51 +08:00
|
|
|
if err := r.broker.Archive(context.Background(), msg, err.Error()); err != nil {
|
2021-01-13 03:01:21 +08:00
|
|
|
r.logger.Warnf("recoverer: could not move task to archive: %v", err)
|
2020-06-21 22:05:57 +08:00
|
|
|
}
|
|
|
|
}
|