2019-11-19 22:48:54 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-11-20 11:44:41 +08:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2019-11-19 22:48:54 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-11-21 12:08:03 +08:00
|
|
|
type processor struct {
|
2019-11-20 11:44:41 +08:00
|
|
|
rdb *rdb
|
2019-11-19 22:48:54 +08:00
|
|
|
|
|
|
|
handler TaskHandler
|
|
|
|
|
|
|
|
// sema is a counting semaphore to ensure the number of active workers
|
|
|
|
// does not exceed the limit
|
|
|
|
sema chan struct{}
|
|
|
|
|
2019-11-21 12:08:03 +08:00
|
|
|
// channel to communicate back to the long running "processor" goroutine.
|
2019-11-19 22:48:54 +08:00
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:08:03 +08:00
|
|
|
func newProcessor(rdb *rdb, numWorkers int, handler TaskHandler) *processor {
|
|
|
|
return &processor{
|
2019-11-19 22:48:54 +08:00
|
|
|
rdb: rdb,
|
|
|
|
handler: handler,
|
|
|
|
sema: make(chan struct{}, numWorkers),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:08:03 +08:00
|
|
|
func (p *processor) terminate() {
|
2019-11-21 12:27:01 +08:00
|
|
|
// Signal he processor goroutine to stop processing tasks from the queue.
|
2019-11-21 12:08:03 +08:00
|
|
|
p.done <- struct{}{}
|
2019-11-19 23:38:09 +08:00
|
|
|
|
|
|
|
fmt.Println("--- Waiting for all workers to finish ---")
|
2019-11-21 12:08:03 +08:00
|
|
|
for i := 0; i < cap(p.sema); i++ {
|
2019-11-19 23:38:09 +08:00
|
|
|
// block until all workers have released the token
|
2019-11-21 12:08:03 +08:00
|
|
|
p.sema <- struct{}{}
|
2019-11-19 23:38:09 +08:00
|
|
|
}
|
|
|
|
fmt.Println("--- All workers have finished! ----")
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:08:03 +08:00
|
|
|
func (p *processor) start() {
|
2019-11-19 22:48:54 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2019-11-21 12:08:03 +08:00
|
|
|
case <-p.done:
|
|
|
|
fmt.Println("-------------[Processor]---------------")
|
|
|
|
fmt.Println("Processor shutting down...")
|
2019-11-19 23:38:09 +08:00
|
|
|
fmt.Println("-------------------------------------")
|
|
|
|
return
|
2019-11-19 22:48:54 +08:00
|
|
|
default:
|
2019-11-21 12:08:03 +08:00
|
|
|
p.exec()
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:08:03 +08:00
|
|
|
func (p *processor) exec() {
|
2019-11-19 22:48:54 +08:00
|
|
|
// pull message out of the queue and process it
|
|
|
|
// TODO(hibiken): sort the list of queues in order of priority
|
2019-11-20 23:01:24 +08:00
|
|
|
// NOTE: BLPOP needs to timeout in case a new queue is added.
|
2019-11-21 12:08:03 +08:00
|
|
|
msg, err := p.rdb.bpop(5*time.Second, p.rdb.listQueues()...)
|
2019-11-19 22:48:54 +08:00
|
|
|
if err != nil {
|
2019-11-20 11:44:41 +08:00
|
|
|
switch err {
|
|
|
|
case errQueuePopTimeout:
|
|
|
|
// timed out, this is a normal behavior.
|
|
|
|
return
|
|
|
|
case errDeserializeTask:
|
|
|
|
log.Println("[Servere Error] could not parse json encoded message")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Printf("[Servere Error] unexpected error while pulling message out of queues: %v\n", err)
|
|
|
|
return
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t := &Task{Type: msg.Type, Payload: msg.Payload}
|
2019-11-21 12:08:03 +08:00
|
|
|
p.sema <- struct{}{} // acquire token
|
2019-11-19 22:48:54 +08:00
|
|
|
go func(task *Task) {
|
2019-11-21 12:08:03 +08:00
|
|
|
defer func() { <-p.sema }() // release token
|
|
|
|
err := p.handler(task)
|
2019-11-19 22:48:54 +08:00
|
|
|
if err != nil {
|
|
|
|
if msg.Retried >= msg.Retry {
|
|
|
|
fmt.Println("Retry exhausted!!!")
|
2019-11-21 12:08:03 +08:00
|
|
|
if err := p.rdb.kill(msg); err != nil {
|
2019-11-19 22:48:54 +08:00
|
|
|
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()
|
2019-11-21 12:08:03 +08:00
|
|
|
if err := p.rdb.zadd(retry, float64(retryAt.Unix()), msg); err != nil {
|
2019-11-19 22:48:54 +08:00
|
|
|
// 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)
|
|
|
|
}
|
2019-11-20 11:44:41 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|