add option for web service signal handler (#1091)

This commit is contained in:
shikbupt 2020-01-10 22:25:28 +08:00 committed by Asim Aslam
parent 37d1139a57
commit 32a2005f6d
2 changed files with 15 additions and 1 deletions

View File

@ -46,6 +46,8 @@ type Options struct {
// Static directory // Static directory
StaticDir string StaticDir string
Signal bool
} }
func newOptions(opts ...Option) Options { func newOptions(opts ...Option) Options {
@ -59,6 +61,7 @@ func newOptions(opts ...Option) Options {
StaticDir: DefaultStaticDir, StaticDir: DefaultStaticDir,
Service: micro.NewService(), Service: micro.NewService(),
Context: context.TODO(), Context: context.TODO(),
Signal: true,
} }
for _, o := range opts { for _, o := range opts {
@ -242,3 +245,12 @@ func RegisterCheck(fn func(context.Context) error) Option {
o.RegisterCheck = fn o.RegisterCheck = fn
} }
} }
// HandleSignal toggles automatic installation of the signal handler that
// traps TERM, INT, and QUIT. Users of this feature to disable the signal
// handler, should control liveness of the service through the context.
func HandleSignal(b bool) Option {
return func(o *Options) {
o.Signal = b
}
}

View File

@ -383,7 +383,9 @@ func (s *service) Run() error {
go s.run(ex) go s.run(ex)
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)
if s.opts.Signal {
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT) signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
}
select { select {
// wait on kill signal // wait on kill signal