2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-14 11:31:18 +08:00
asynq/processor.go

107 lines
2.6 KiB
Go
Raw Normal View History

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
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-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{}{}
fmt.Println("--- Waiting for all workers to finish ---")
2019-11-21 12:08:03 +08:00
for i := 0; i < cap(p.sema); i++ {
// block until all workers have released the token
2019-11-21 12:08:03 +08:00
p.sema <- struct{}{}
}
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() {
// 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:
fmt.Println("-------------[Processor]---------------")
fmt.Println("Processor shutting down...")
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-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() {
// NOTE: dequeue needs to timeout to avoid blocking forever
2019-11-22 13:45:27 +08:00
// in case of a program shutdown or additon of a new queue.
2019-11-22 12:22:55 +08:00
const timeout = 5 * time.Second
msg, err := p.rdb.dequeue(defaultQueue, timeout)
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
}
}
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-22 13:45:27 +08:00
defer func() {
if err := p.rdb.lrem(inProgress, msg); err != nil {
log.Printf("[SERVER ERROR] LREM failed: %v\n", err)
2019-11-22 13:45:27 +08:00
}
<-p.sema // release token
}()
err := p.handler(task) // TODO(hibiken): maybe also handle panic?
2019-11-19 22:48:54 +08:00
if err != nil {
retryTask(p.rdb, msg, err)
2019-11-19 22:48:54 +08:00
}
}(task)
2019-11-19 22:48:54 +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 {
log.Printf("[SERVER ERROR] could not move tasks from %q to %q\n", inProgress, defaultQueue)
}
}