2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 07:12:17 +08:00

Stop processing more tasks from the queue once TSTP signal is received

This commit is contained in:
Ken Hibino 2019-12-17 06:18:22 -08:00
parent 3e30c5916b
commit 24dd78b31c
2 changed files with 16 additions and 5 deletions

View File

@ -80,9 +80,8 @@ func (bg *Background) Run(handler Handler) {
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGTSTP) signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGTSTP)
for { for {
sig := <-sigs sig := <-sigs
fmt.Printf("[DEBUG] Got %v\n", sig) // TODO: Remove this
if sig == syscall.SIGTSTP { if sig == syscall.SIGTSTP {
fmt.Println("[DEBUG] Stop processing tasks") bg.processor.stop()
continue continue
} }
break break

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"math" "math"
"math/rand" "math/rand"
"sync"
"time" "time"
"github.com/hibiken/asynq/internal/rdb" "github.com/hibiken/asynq/internal/rdb"
@ -25,7 +26,9 @@ type processor struct {
sema chan struct{} sema chan struct{}
// channel to communicate back to the long running "processor" goroutine. // channel to communicate back to the long running "processor" goroutine.
// once is used to send value to the channel only once.
done chan struct{} done chan struct{}
once sync.Once
// quit channel communicates to the in-flight worker goroutines to stop. // quit channel communicates to the in-flight worker goroutines to stop.
quit chan struct{} quit chan struct{}
@ -42,11 +45,20 @@ func newProcessor(r *rdb.RDB, numWorkers int, handler Handler) *processor {
} }
} }
// Note: stops only the "processor" goroutine, does not stop workers.
// It's safe to call this method multiple times.
func (p *processor) stop() {
p.once.Do(func() {
log.Println("[INFO] Processor shutting down...")
// Signal the processor goroutine to stop processing tasks
// from the queue.
p.done <- struct{}{}
})
}
// NOTE: once terminated, processor cannot be re-started. // NOTE: once terminated, processor cannot be re-started.
func (p *processor) terminate() { func (p *processor) terminate() {
log.Println("[INFO] Processor shutting down...") p.stop()
// Signal the processor goroutine to stop processing tasks from the queue.
p.done <- struct{}{}
// TODO(hibiken): Allow user to customize this timeout value. // TODO(hibiken): Allow user to customize this timeout value.
const timeout = 8 * time.Second const timeout = 8 * time.Second