fix(NSQ): close service after waiting all job completed. (#601)

This commit is contained in:
Bo-Yi Wu
2021-07-18 00:19:17 +08:00
committed by GitHub
parent 87be86be08
commit d6b4a0ae39
6 changed files with 58 additions and 2 deletions

View File

@@ -98,7 +98,10 @@ func (s *Worker) AfterRun() error {
// Run start the worker
func (s *Worker) Run(quit chan struct{}) error {
wg := &sync.WaitGroup{}
s.q.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
wg.Add(1)
defer wg.Done()
var notification gorush.PushNotification
if err := json.Unmarshal(msg.Body, &notification); err != nil {
return err
@@ -110,6 +113,8 @@ func (s *Worker) Run(quit chan struct{}) error {
select {
case <-quit:
}
wg.Wait()
return nil
}

34
queue/nsq/nsq_test.go Normal file
View File

@@ -0,0 +1,34 @@
package nsq
import (
"log"
"testing"
"time"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/queue"
)
func TestMain(m *testing.M) {
if err := logx.InitLog(
"debug",
"stdout",
"debug",
"stdout",
); err != nil {
log.Fatalf("Can't load log module, error: %v", err)
}
m.Run()
}
func TestShutdown(t *testing.T) {
w := NewWorker(
WithAddr("nsq:4150"),
WithTopic("test"),
)
q := queue.NewQueue(w, 2)
q.Start()
time.Sleep(1 * time.Second)
q.Shutdown()
}