2019-11-19 22:48:54 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"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
|
|
|
|
2019-12-03 12:42:21 +08:00
|
|
|
handler Handler
|
2019-11-19 22:48:54 +08:00
|
|
|
|
2019-11-30 04:48:54 +08:00
|
|
|
// timeout for blocking dequeue operation.
|
|
|
|
// dequeue needs to timeout to avoid blocking forever
|
|
|
|
// in case of a program shutdown or additon of a new queue.
|
|
|
|
dequeueTimeout time.Duration
|
|
|
|
|
2019-11-19 22:48:54 +08:00
|
|
|
// sema is a counting semaphore to ensure the number of active workers
|
2019-11-30 04:48:54 +08:00
|
|
|
// does not exceed the limit.
|
2019-11-19 22:48:54 +08:00
|
|
|
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-12-03 12:42:21 +08:00
|
|
|
func newProcessor(rdb *rdb, numWorkers int, handler Handler) *processor {
|
2019-11-21 12:08:03 +08:00
|
|
|
return &processor{
|
2019-11-30 04:48:54 +08:00
|
|
|
rdb: rdb,
|
|
|
|
handler: handler,
|
|
|
|
dequeueTimeout: 5 * time.Second,
|
|
|
|
sema: make(chan struct{}, numWorkers),
|
|
|
|
done: make(chan struct{}),
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 04:48:54 +08:00
|
|
|
// NOTE: once terminated, processor cannot be re-started.
|
2019-11-21 12:08:03 +08:00
|
|
|
func (p *processor) terminate() {
|
2019-11-29 23:14:28 +08:00
|
|
|
log.Println("[INFO] Processor shutting down...")
|
2019-11-22 12:22:55 +08:00
|
|
|
// Signal the 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
|
|
|
|
2019-11-29 23:14:28 +08:00
|
|
|
log.Println("[INFO] Waiting for all workers to finish...")
|
2019-11-28 11:36:56 +08:00
|
|
|
// block until all workers have released the token
|
2019-11-21 12:08:03 +08:00
|
|
|
for i := 0; i < cap(p.sema); i++ {
|
|
|
|
p.sema <- struct{}{}
|
2019-11-19 23:38:09 +08:00
|
|
|
}
|
2019-11-29 23:14:28 +08:00
|
|
|
log.Println("[INFO] 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-24 07:09:50 +08:00
|
|
|
// NOTE: The call to "restore" needs to complete before starting
|
|
|
|
// the processor goroutine.
|
|
|
|
p.restore()
|
2019-11-19 22:48:54 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2019-11-21 12:08:03 +08:00
|
|
|
case <-p.done:
|
2019-11-29 23:14:28 +08:00
|
|
|
log.Println("[INFO] Processor done.")
|
2019-11-19 23:38:09 +08:00
|
|
|
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-22 13:45:27 +08:00
|
|
|
// exec pulls a task out of the queue and starts a worker goroutine to
|
|
|
|
// process the task.
|
2019-11-21 12:08:03 +08:00
|
|
|
func (p *processor) exec() {
|
2019-11-30 04:48:54 +08:00
|
|
|
msg, err := p.rdb.dequeue(defaultQueue, p.dequeueTimeout)
|
2019-11-28 11:36:56 +08:00
|
|
|
if err == errDequeueTimeout {
|
|
|
|
// timed out, this is a normal behavior.
|
|
|
|
return
|
|
|
|
}
|
2019-11-19 22:48:54 +08:00
|
|
|
if err != nil {
|
2019-11-28 11:36:56 +08:00
|
|
|
log.Printf("[ERROR] unexpected error while pulling a task out of queue: %v\n", err)
|
|
|
|
return
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:43:42 +08:00
|
|
|
task := &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-28 06:26:04 +08:00
|
|
|
// NOTE: This deferred anonymous function needs to take taskMessage as a value because
|
|
|
|
// the message can be mutated by the time this function is called.
|
|
|
|
defer func(msg taskMessage) {
|
2019-11-28 12:05:31 +08:00
|
|
|
if err := p.rdb.remove(inProgress, &msg); err != nil {
|
2019-11-27 22:21:57 +08:00
|
|
|
log.Printf("[ERROR] could not remove %+v from %q: %v\n", msg, inProgress, err)
|
2019-11-22 13:45:27 +08:00
|
|
|
}
|
|
|
|
<-p.sema // release token
|
2019-11-28 06:26:04 +08:00
|
|
|
}(*msg)
|
2019-11-28 06:03:04 +08:00
|
|
|
err := perform(p.handler, task)
|
2019-11-19 22:48:54 +08:00
|
|
|
if err != nil {
|
2019-11-21 12:38:49 +08:00
|
|
|
retryTask(p.rdb, msg, err)
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
2019-11-22 22:43:42 +08:00
|
|
|
}(task)
|
2019-11-19 22:48:54 +08:00
|
|
|
}
|
2019-11-24 07:09:50 +08:00
|
|
|
|
|
|
|
// restore moves all tasks from "in-progress" back to queue
|
|
|
|
// to restore all unfinished tasks.
|
|
|
|
func (p *processor) restore() {
|
|
|
|
err := p.rdb.moveAll(inProgress, defaultQueue)
|
|
|
|
if err != nil {
|
2019-11-27 22:21:57 +08:00
|
|
|
log.Printf("[ERROR] could not move tasks from %q to %q\n", inProgress, defaultQueue)
|
2019-11-24 07:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 06:03:04 +08:00
|
|
|
|
|
|
|
// perform calls the handler with the given task.
|
|
|
|
// If the call returns without panic, it simply returns the value,
|
|
|
|
// otherwise, it recovers from panic and returns an error.
|
2019-12-03 12:42:21 +08:00
|
|
|
func perform(h Handler, task *Task) (err error) {
|
2019-11-28 06:03:04 +08:00
|
|
|
defer func() {
|
|
|
|
if x := recover(); x != nil {
|
|
|
|
err = fmt.Errorf("panic: %v", x)
|
|
|
|
}
|
|
|
|
}()
|
2019-12-03 12:42:21 +08:00
|
|
|
return h.ProcessTask(task)
|
2019-11-28 06:03:04 +08:00
|
|
|
}
|