2019-11-21 12:38:49 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func retryTask(rdb *rdb, msg *taskMessage, err error) {
|
2019-11-29 03:22:42 +08:00
|
|
|
msg.ErrorMsg = err.Error()
|
2019-11-21 12:38:49 +08:00
|
|
|
if msg.Retried >= msg.Retry {
|
2019-11-29 03:22:42 +08:00
|
|
|
log.Printf("[WARN] Retry exhausted for task(Type: %q, ID: %v)\n", msg.Type, msg.ID)
|
2019-11-21 12:38:49 +08:00
|
|
|
if err := rdb.kill(msg); err != nil {
|
2019-11-29 03:22:42 +08:00
|
|
|
log.Printf("[ERROR] Could not add task %+v to 'dead'\n", err)
|
2019-11-21 12:38:49 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
retryAt := time.Now().Add(delaySeconds((msg.Retried)))
|
2019-11-29 03:22:42 +08:00
|
|
|
log.Printf("[INFO] Retrying task(Type: %q, ID: %v) in %v\n", msg.Type, msg.ID, retryAt.Sub(time.Now()))
|
2019-11-21 12:38:49 +08:00
|
|
|
msg.Retried++
|
2019-11-27 23:16:16 +08:00
|
|
|
if err := rdb.schedule(retry, retryAt, msg); err != nil {
|
2019-11-29 03:22:42 +08:00
|
|
|
log.Printf("[ERROR] Could not add msg %+v to 'retry': %v\n", msg, err)
|
2019-11-21 12:38:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delaySeconds returns a number seconds to delay before retrying.
|
|
|
|
// Formula taken from https://github.com/mperham/sidekiq.
|
|
|
|
func delaySeconds(count int) time.Duration {
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
s := int(math.Pow(float64(count), 4)) + 15 + (r.Intn(30) * (count + 1))
|
|
|
|
return time.Duration(s) * time.Second
|
|
|
|
}
|