2020-02-13 09:12:09 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
2020-02-16 15:14:30 +08:00
|
|
|
"sync"
|
2020-04-16 23:58:44 +08:00
|
|
|
"time"
|
2020-02-16 15:14:30 +08:00
|
|
|
|
2020-02-13 09:12:09 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2020-05-06 13:10:11 +08:00
|
|
|
"github.com/hibiken/asynq/internal/log"
|
2023-04-19 22:33:44 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2020-02-13 09:12:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type subscriber struct {
|
2020-05-06 13:10:11 +08:00
|
|
|
logger *log.Logger
|
2020-04-18 22:55:10 +08:00
|
|
|
broker base.Broker
|
2020-02-13 09:12:09 +08:00
|
|
|
|
|
|
|
// channel to communicate back to the long running "subscriber" goroutine.
|
|
|
|
done chan struct{}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
// cancelations hold cancel functions for all active tasks.
|
2020-02-13 09:12:09 +08:00
|
|
|
cancelations *base.Cancelations
|
2020-04-17 22:52:12 +08:00
|
|
|
|
|
|
|
// time to wait before retrying to connect to redis.
|
|
|
|
retryTimeout time.Duration
|
2020-02-13 09:12:09 +08:00
|
|
|
}
|
|
|
|
|
2020-05-18 03:33:55 +08:00
|
|
|
type subscriberParams struct {
|
|
|
|
logger *log.Logger
|
|
|
|
broker base.Broker
|
|
|
|
cancelations *base.Cancelations
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubscriber(params subscriberParams) *subscriber {
|
2020-02-13 09:12:09 +08:00
|
|
|
return &subscriber{
|
2020-05-18 03:33:55 +08:00
|
|
|
logger: params.logger,
|
|
|
|
broker: params.broker,
|
2020-02-13 09:12:09 +08:00
|
|
|
done: make(chan struct{}),
|
2020-05-18 03:33:55 +08:00
|
|
|
cancelations: params.cancelations,
|
2020-04-17 22:52:12 +08:00
|
|
|
retryTimeout: 5 * time.Second,
|
2020-02-13 09:12:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
func (s *subscriber) shutdown() {
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Subscriber shutting down...")
|
2020-02-13 09:12:09 +08:00
|
|
|
// Signal the subscriber goroutine to stop.
|
|
|
|
s.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:14:30 +08:00
|
|
|
func (s *subscriber) start(wg *sync.WaitGroup) {
|
|
|
|
wg.Add(1)
|
2020-02-13 09:12:09 +08:00
|
|
|
go func() {
|
2020-02-16 15:14:30 +08:00
|
|
|
defer wg.Done()
|
2020-04-16 23:58:44 +08:00
|
|
|
var (
|
|
|
|
pubsub *redis.PubSub
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
// Try until successfully connect to Redis.
|
|
|
|
for {
|
2020-04-17 21:56:44 +08:00
|
|
|
pubsub, err = s.broker.CancelationPubSub()
|
2020-04-16 23:58:44 +08:00
|
|
|
if err != nil {
|
2020-05-06 13:10:11 +08:00
|
|
|
s.logger.Errorf("cannot subscribe to cancelation channel: %v", err)
|
2020-04-16 23:58:44 +08:00
|
|
|
select {
|
2020-04-17 22:52:12 +08:00
|
|
|
case <-time.After(s.retryTimeout):
|
2020-04-16 23:58:44 +08:00
|
|
|
continue
|
|
|
|
case <-s.done:
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Subscriber done")
|
2020-04-16 23:58:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cancelCh := pubsub.Channel()
|
2020-02-13 09:12:09 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.done:
|
|
|
|
pubsub.Close()
|
2020-05-11 22:02:26 +08:00
|
|
|
s.logger.Debug("Subscriber done")
|
2020-02-13 09:12:09 +08:00
|
|
|
return
|
|
|
|
case msg := <-cancelCh:
|
2020-02-20 23:44:13 +08:00
|
|
|
cancel, ok := s.cancelations.Get(msg.Payload)
|
|
|
|
if ok {
|
2020-02-13 09:12:09 +08:00
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|