2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-24 23:02:18 +08:00

Fix subscriber to not panic when it cannot establish pubsub channel on

startup
This commit is contained in:
Ken Hibino 2020-04-16 08:58:44 -07:00
parent 4ec2dc9e47
commit 42453280f4
2 changed files with 26 additions and 7 deletions

View File

@ -6,7 +6,9 @@ package asynq
import ( import (
"sync" "sync"
"time"
"github.com/go-redis/redis/v7"
"github.com/hibiken/asynq/internal/base" "github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/rdb" "github.com/hibiken/asynq/internal/rdb"
) )
@ -38,15 +40,29 @@ func (s *subscriber) terminate() {
} }
func (s *subscriber) start(wg *sync.WaitGroup) { func (s *subscriber) start(wg *sync.WaitGroup) {
pubsub, err := s.rdb.CancelationPubSub()
cancelCh := pubsub.Channel()
if err != nil {
s.logger.Error("cannot subscribe to cancelation channel: %v", err)
return
}
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
var (
pubsub *redis.PubSub
err error
)
// Try until successfully connect to Redis.
for {
pubsub, err = s.rdb.CancelationPubSub()
if err != nil {
s.logger.Error("cannot subscribe to cancelation channel: %v", err)
select {
case <-time.After(5 * time.Second): // retry in 5s
continue
case <-s.done:
s.logger.Info("Subscriber done")
return
}
}
break
}
cancelCh := pubsub.Channel()
for { for {
select { select {
case <-s.done: case <-s.done:

View File

@ -41,12 +41,15 @@ func TestSubscriber(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
subscriber.start(&wg) subscriber.start(&wg)
// wait for subscriber to establish connection to pubsub channel
time.Sleep(time.Second)
if err := rdbClient.PublishCancelation(tc.publishID); err != nil { if err := rdbClient.PublishCancelation(tc.publishID); err != nil {
subscriber.terminate() subscriber.terminate()
t.Fatalf("could not publish cancelation message: %v", err) t.Fatalf("could not publish cancelation message: %v", err)
} }
// allow for redis to publish message // wait for redis to publish message
time.Sleep(time.Second) time.Sleep(time.Second)
mu.Lock() mu.Lock()