mirror of
https://github.com/hibiken/asynq.git
synced 2024-11-10 11:31:58 +08:00
Move retry related logic to its own file
This commit is contained in:
parent
3dddcfbb14
commit
66930970f9
28
processor.go
28
processor.go
@ -3,8 +3,6 @@ package asynq
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"math/rand"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -83,31 +81,7 @@ func (p *processor) exec() {
|
|||||||
defer func() { <-p.sema }() // release token
|
defer func() { <-p.sema }() // release token
|
||||||
err := p.handler(task)
|
err := p.handler(task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if msg.Retried >= msg.Retry {
|
retryTask(p.rdb, msg, err)
|
||||||
fmt.Println("Retry exhausted!!!")
|
|
||||||
if err := p.rdb.kill(msg); err != nil {
|
|
||||||
log.Printf("[SERVER ERROR] could not add task %+v to 'dead' set\n", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println("RETRY!!!")
|
|
||||||
retryAt := time.Now().Add(delaySeconds((msg.Retried)))
|
|
||||||
fmt.Printf("[DEBUG] retying the task in %v\n", retryAt.Sub(time.Now()))
|
|
||||||
msg.Retried++
|
|
||||||
msg.ErrorMsg = err.Error()
|
|
||||||
if err := p.rdb.zadd(retry, float64(retryAt.Unix()), msg); err != nil {
|
|
||||||
// TODO(hibiken): Not sure how to handle this error
|
|
||||||
log.Printf("[SEVERE ERROR] could not add msg %+v to 'retry' set: %v\n", msg, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}(t)
|
}(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
36
retry.go
Normal file
36
retry.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package asynq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func retryTask(rdb *rdb, msg *taskMessage, err error) {
|
||||||
|
if msg.Retried >= msg.Retry {
|
||||||
|
fmt.Println("[DEBUG] Retry exhausted!!!")
|
||||||
|
if err := rdb.kill(msg); err != nil {
|
||||||
|
log.Printf("[SERVER ERROR] could not add task %+v to 'dead' set\n", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
retryAt := time.Now().Add(delaySeconds((msg.Retried)))
|
||||||
|
fmt.Printf("[DEBUG] Retrying the task in %v\n", retryAt.Sub(time.Now()))
|
||||||
|
msg.Retried++
|
||||||
|
msg.ErrorMsg = err.Error()
|
||||||
|
if err := rdb.zadd(retry, float64(retryAt.Unix()), msg); err != nil {
|
||||||
|
// TODO(hibiken): Not sure how to handle this error
|
||||||
|
log.Printf("[SEVERE ERROR] could not add msg %+v to 'retry' set: %v\n", msg, err)
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user