fix graceful stop

This commit is contained in:
Adphi 2020-03-03 15:12:33 +01:00
parent ac9a81d3e7
commit af25e09154

View File

@ -1,6 +1,7 @@
package service
import (
"context"
"net"
"os"
"sync"
@ -29,6 +30,7 @@ func New(opts ...Option) (Service, error) {
type service struct {
cmd *cobra.Command
opts *options
cancel context.CancelFunc
server *grpc.Server
list net.Listener
mu sync.Mutex
@ -49,6 +51,7 @@ func newService(opts ...Option) (*service, error) {
if s.opts.error != nil {
return nil, s.opts.error
}
s.opts.ctx, s.cancel = context.WithCancel(s.opts.ctx)
go func() {
for {
select {
@ -136,6 +139,7 @@ func (s *service) Stop() error {
}
}
s.server.GracefulStop()
s.cancel()
s.running = false
for i := range s.opts.afterStop {
if err := s.opts.afterStop[i](); err != nil {