Skip to content

chore: add stop channel in worker #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions nsq/nsq.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ type Worker struct {
q *nsq.Consumer
p *nsq.Producer
startOnce sync.Once
stopOnce sync.Once
stop chan struct{}
maxInFlight int
addr string
topic string
channel string
runFunc func(queue.QueuedMessage) error
runFunc func(queue.QueuedMessage, <-chan struct{}) error
}

// WithAddr setup the addr of NSQ
Expand All @@ -59,7 +61,7 @@ func WithChannel(channel string) Option {
}

// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(queue.QueuedMessage) error) Option {
func WithRunFunc(fn func(queue.QueuedMessage, <-chan struct{}) error) Option {
return func(w *Worker) {
w.runFunc = fn
}
Expand All @@ -80,7 +82,8 @@ func NewWorker(opts ...Option) *Worker {
topic: "gorush",
channel: "ch",
maxInFlight: runtime.NumCPU(),
runFunc: func(queue.QueuedMessage) error {
stop: make(chan struct{}),
runFunc: func(queue.QueuedMessage, <-chan struct{}) error {
return nil
},
}
Expand Down Expand Up @@ -125,7 +128,7 @@ func (s *Worker) AfterRun() error {
}

// Run start the worker
func (s *Worker) Run(quit chan struct{}) error {
func (s *Worker) Run() error {
wg := &sync.WaitGroup{}
s.q.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
wg.Add(1)
Expand All @@ -141,11 +144,11 @@ func (s *Worker) Run(quit chan struct{}) error {
}

// run custom process function
return s.runFunc(job)
return s.runFunc(job, s.stop)
}))

// wait close signal
<-quit
<-s.stop

// wait job completed
wg.Wait()
Expand All @@ -155,8 +158,11 @@ func (s *Worker) Run(quit chan struct{}) error {

// Shutdown worker
func (s *Worker) Shutdown() error {
s.q.Stop()
s.p.Stop()
s.stopOnce.Do(func() {
s.q.Stop()
s.p.Stop()
close(s.stop)
})
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion nsq/nsq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestCustomFuncAndWait(t *testing.T) {
WithAddr(host+":4150"),
WithTopic("test"),
WithMaxInFlight(2),
WithRunFunc(func(msg queue.QueuedMessage) error {
WithRunFunc(func(msg queue.QueuedMessage, s <-chan struct{}) error {
time.Sleep(500 * time.Millisecond)
return nil
}),
Expand Down
2 changes: 1 addition & 1 deletion queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (q *Queue) work() {
}
}()
q.logger.Infof("start the worker num: %d", num)
if err := q.worker.Run(q.quit); err != nil {
if err := q.worker.Run(); err != nil {
q.logger.Error(err)
}
q.logger.Infof("stop the worker num: %d", num)
Expand Down
19 changes: 13 additions & 6 deletions simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simple
import (
"errors"
"runtime"
"sync"

"github.com/appleboy/queue"
)
Expand All @@ -17,7 +18,9 @@ var errMaxCapacity = errors.New("max capacity reached")
// Worker for simple queue using channel
type Worker struct {
queueNotification chan queue.QueuedMessage
runFunc func(queue.QueuedMessage) error
runFunc func(queue.QueuedMessage, <-chan struct{}) error
stop chan struct{}
stopOnce sync.Once
}

// BeforeRun run script before start worker
Expand All @@ -31,17 +34,20 @@ func (s *Worker) AfterRun() error {
}

// Run start the worker
func (s *Worker) Run(_ chan struct{}) error {
func (s *Worker) Run() error {
for notification := range s.queueNotification {
// run custom process function
_ = s.runFunc(notification)
_ = s.runFunc(notification, s.stop)
}
return nil
}

// Shutdown worker
func (s *Worker) Shutdown() error {
close(s.queueNotification)
s.stopOnce.Do(func() {
close(s.queueNotification)
close(s.stop)
})
return nil
}

Expand Down Expand Up @@ -73,7 +79,7 @@ func WithQueueNum(num int) Option {
}

// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(queue.QueuedMessage) error) Option {
func WithRunFunc(fn func(queue.QueuedMessage, <-chan struct{}) error) Option {
return func(w *Worker) {
w.runFunc = fn
}
Expand All @@ -83,7 +89,8 @@ func WithRunFunc(fn func(queue.QueuedMessage) error) Option {
func NewWorker(opts ...Option) *Worker {
w := &Worker{
queueNotification: make(chan queue.QueuedMessage, runtime.NumCPU()<<1),
runFunc: func(msg queue.QueuedMessage) error {
stop: make(chan struct{}),
runFunc: func(queue.QueuedMessage, <-chan struct{}) error {
return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestCustomFuncAndWait(t *testing.T) {
msg: "foo",
}
w := NewWorker(
WithRunFunc(func(msg queue.QueuedMessage) error {
WithRunFunc(func(msg queue.QueuedMessage, s <-chan struct{}) error {
time.Sleep(500 * time.Millisecond)
return nil
}),
Expand Down
6 changes: 3 additions & 3 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Worker interface {
// BeforeRun is called before starting the worker
BeforeRun() error
// Run is called to start the worker
Run(chan struct{}) error
Run() error
// BeforeRun is called after starting the worker
AfterRun() error
// Shutdown is called if stop all worker
Expand All @@ -37,7 +37,7 @@ type emptyWorker struct{}

func (w *emptyWorker) BeforeRun() error { return nil }
func (w *emptyWorker) AfterRun() error { return nil }
func (w *emptyWorker) Run(chan struct{}) error { return nil }
func (w *emptyWorker) Run() error { return nil }
func (w *emptyWorker) Shutdown() error { return nil }
func (w *emptyWorker) Queue(job QueuedMessage) error { return nil }
func (w *emptyWorker) Capacity() int { return 0 }
Expand All @@ -49,7 +49,7 @@ type queueWorker struct {

func (w *queueWorker) BeforeRun() error { return nil }
func (w *queueWorker) AfterRun() error { return nil }
func (w *queueWorker) Run(chan struct{}) error {
func (w *queueWorker) Run() error {
for msg := range w.messages {
if string(msg.Bytes()) == "panic" {
panic("show panic")
Expand Down