Skip to content
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

support graceful shutdown, stop wait all processing completed #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 22 additions & 5 deletions dq/consumernode.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package dq

import (
"sync/atomic"
"time"

"github.com/beanstalkd/go-beanstalk"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/core/threading"
)

type (
consumerNode struct {
conn *connection
tube string
on *syncx.AtomicBool
conn *connection
tube string
on *syncx.AtomicBool
processingNum int64
}

consumeService struct {
Expand Down Expand Up @@ -53,8 +56,14 @@ func (c *consumerNode) consumeEvents(consume Consume) {
conn.TubeSet.Name[c.tube] = true
id, body, err := conn.Reserve(reserveTimeout)
if err == nil {
conn.Delete(id)
consume(body)
if err := conn.Delete(id); err != nil {
logx.Error(err)
}
threading.GoSafe(func() {
atomic.AddInt64(&c.processingNum, 1)
defer atomic.AddInt64(&c.processingNum, -1)
consume(body)
})
continue
}

Expand Down Expand Up @@ -91,4 +100,12 @@ func (cs consumeService) Start() {

func (cs consumeService) Stop() {
cs.c.dispose()
for {
if atomic.LoadInt64(&cs.c.processingNum) == 0 {
// wait all service consumer func process complete
break
}
// wait 100 millisecond check again
time.Sleep(time.Millisecond * 100)
}
}