Skip to content
Permalink
Browse files

SignalChan provides a way to override micro's signal behavior

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
erikh committed Nov 14, 2019
1 parent 9f48154 commit 18ca90a54b631a7bfa28779099911eff0ce8d182
Showing with 28 additions and 2 deletions.
  1. +1 −0 go.mod
  2. +2 −0 go.sum
  3. +17 −0 options.go
  4. +8 −2 service.go
1 go.mod
@@ -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
2 go.sum
@@ -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=
@@ -2,6 +2,7 @@ package micro

import (
"context"
"os"
"time"

"github.com/micro/cli"
@@ -31,6 +32,8 @@ 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 {
@@ -156,6 +159,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
@@ -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

0 comments on commit 18ca90a

Please sign in to comment.
You can’t perform that action at this time.