2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 08:12:19 +08:00
asynq/recoverer.go

102 lines
2.5 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 (
"fmt"
"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
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
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,
}
}
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()
timer := time.NewTimer(r.interval)
for {
select {
case <-r.done:
r.logger.Debug("Recoverer done")
timer.Stop()
return
case <-timer.C:
// Get all tasks which have expired 30 seconds ago or earlier.
deadline := time.Now().Add(-30 * time.Second)
2020-08-10 21:10:14 +08:00
msgs, err := r.broker.ListDeadlineExceeded(deadline, r.queues...)
2020-06-21 22:05:57 +08:00
if err != nil {
r.logger.Warn("recoverer: could not list deadline exceeded tasks")
continue
}
const errMsg = "deadline exceeded" // TODO: better error message
for _, msg := range msgs {
if msg.Retried >= msg.Retry {
r.archive(msg, errMsg)
2020-06-21 22:05:57 +08:00
} else {
r.retry(msg, errMsg)
}
}
}
}
}()
}
func (r *recoverer) retry(msg *base.TaskMessage, errMsg string) {
delay := r.retryDelayFunc(msg.Retried, fmt.Errorf(errMsg), NewTask(msg.Type, msg.Payload))
retryAt := time.Now().Add(delay)
if err := r.broker.Retry(msg, retryAt, errMsg); err != nil {
r.logger.Warnf("recoverer: could not retry deadline exceeded task: %v", err)
}
}
func (r *recoverer) archive(msg *base.TaskMessage, errMsg string) {
if err := r.broker.Archive(msg, errMsg); err != nil {
r.logger.Warnf("recoverer: could not move task to archive: %v", err)
2020-06-21 22:05:57 +08:00
}
}