Skip to content

Commit

Permalink
SignalChan provides a way to override micro's signal behavior
Browse files Browse the repository at this point in the history
If provided, it will use this provided channel and not install a signal
handler of its own.

This is the simplest approach I could think of.

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
  • Loading branch information
Erik Hollensbe committed Nov 14, 2019
1 parent 9f48154 commit 9a7bb8e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/mitchellh/hashstructure v1.0.0
github.com/nats-io/nats.go v1.9.1
github.com/nlopes/slack v0.6.0
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/pkg/errors v0.8.1
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
go.uber.org/zap v1.12.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mo
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/oracle/oci-go-sdk v7.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukwStZIg5F66tcBccjip/j888=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
32 changes: 25 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package micro

import (
"context"
"os"
"time"

"github.com/micro/cli"
Expand Down Expand Up @@ -31,17 +32,20 @@ type Options struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context

SignalChan chan os.Signal
}

func newOptions(opts ...Option) Options {
opt := Options{
Broker: broker.DefaultBroker,
Cmd: cmd.DefaultCmd,
Client: client.DefaultClient,
Server: server.DefaultServer,
Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport,
Context: context.Background(),
Broker: broker.DefaultBroker,
Cmd: cmd.DefaultCmd,
Client: client.DefaultClient,
Server: server.DefaultServer,
Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport,
Context: context.Background(),
SignalChan: make(chan os.Signal),
}

for _, o := range opts {
Expand Down Expand Up @@ -156,6 +160,20 @@ func Flags(flags ...cli.Flag) Option {
}
}

// SignalChan replaces the signal handler with the channel provided to the
// option; if this is not specified, a standard handler that traps TERM, INT
// and QUIT will be installed.
//
// If you wish to have no signals handled, simply pass nil.
func SignalChan(sigChan chan os.Signal) Option {
return func(o *Options) {
if sigChan == nil {
sigChan = make(chan os.Signal)
}
o.SignalChan = sigChan
}
}

func Action(a func(*cli.Context)) Option {
return func(o *Options) {
o.Cmd.App().Action = a
Expand Down
10 changes: 8 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ func (s *service) Run() error {
return err
}

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
var ch chan os.Signal

if s.opts.SignalChan != nil {
ch = s.opts.SignalChan
} else {
ch = make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
}

select {
// wait on kill signal
Expand Down

0 comments on commit 9a7bb8e

Please sign in to comment.