chore(queue): Shutdown service once. (#610)
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
d9947ea44d
commit
6ebbbe5026
|
@ -38,6 +38,8 @@ func TestShutdown(t *testing.T) {
|
||||||
q.Start()
|
q.Start()
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
q.Shutdown()
|
q.Shutdown()
|
||||||
|
// check shutdown once
|
||||||
|
q.Shutdown()
|
||||||
q.Wait()
|
q.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package queue
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -13,6 +14,7 @@ type (
|
||||||
routineGroup *routineGroup
|
routineGroup *routineGroup
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
worker Worker
|
worker Worker
|
||||||
|
stopOnce sync.Once
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -82,8 +84,10 @@ func (q *Queue) Start() {
|
||||||
|
|
||||||
// Shutdown stops all queues.
|
// Shutdown stops all queues.
|
||||||
func (q *Queue) Shutdown() {
|
func (q *Queue) Shutdown() {
|
||||||
q.worker.Shutdown()
|
q.stopOnce.Do(func() {
|
||||||
close(q.quit)
|
q.worker.Shutdown()
|
||||||
|
close(q.quit)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait all process
|
// Wait all process
|
||||||
|
|
|
@ -76,3 +76,23 @@ func TestCustomFuncAndWait(t *testing.T) {
|
||||||
q.Wait()
|
q.Wait()
|
||||||
// you will see the execute time > 1000ms
|
// you will see the execute time > 1000ms
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShutDonwPanic(t *testing.T) {
|
||||||
|
w := NewWorker(
|
||||||
|
WithRunFunc(func(msg queue.QueuedMessage) error {
|
||||||
|
logx.LogAccess.Infof("get message: %s", msg.Bytes())
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
q, err := queue.NewQueue(
|
||||||
|
queue.WithWorker(w),
|
||||||
|
queue.WithWorkerCount(2),
|
||||||
|
)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
q.Start()
|
||||||
|
q.Shutdown()
|
||||||
|
// check shutdown once
|
||||||
|
q.Shutdown()
|
||||||
|
q.Wait()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue