close micro-roadmap/issues/12

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-10-09 12:15:55 +03:00
parent cacd33e84f
commit 24be220f91
2 changed files with 10 additions and 30 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/unistack-org/micro/v3/config"
"github.com/unistack-org/micro/v3/debug/profile"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/network/transport"
"github.com/unistack-org/micro/v3/registry"
"github.com/unistack-org/micro/v3/router"
"github.com/unistack-org/micro/v3/runtime"
@ -19,7 +20,6 @@ import (
"github.com/unistack-org/micro/v3/server"
"github.com/unistack-org/micro/v3/store"
"github.com/unistack-org/micro/v3/tracer"
"github.com/unistack-org/micro/v3/network/transport"
)
// Options for micro service
@ -47,14 +47,11 @@ type Options struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
Signal bool
}
func newOptions(opts ...Option) Options {
opt := Options{
Context: context.Background(),
Signal: true,
Server: server.DefaultServer,
Client: client.DefaultClient,
Broker: broker.DefaultBroker,
@ -113,15 +110,6 @@ func Context(ctx context.Context) Option {
}
}
// 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
}
}
// Profile to be used for debug profile
func Profile(p profile.Profile) Option {
return func(o *Options) {

View File

@ -1,8 +1,6 @@
package micro
import (
"os"
"os/signal"
rtime "runtime"
"sync"
@ -10,7 +8,6 @@ import (
"github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/server"
signalutil "github.com/unistack-org/micro/v3/util/signal"
)
type service struct {
@ -20,11 +17,9 @@ type service struct {
}
func newService(opts ...Option) Service {
service := &service{}
options := newOptions(opts...)
// set opts
service.opts = options
service := &service{opts: options}
return service
}
@ -127,6 +122,10 @@ func (s *service) String() string {
}
func (s *service) Start() error {
if logger.V(logger.InfoLevel) {
logger.Infof("Starting [service] %s", s.Name())
}
var err error
for _, fn := range s.opts.BeforeStart {
if err = fn(); err != nil {
@ -148,6 +147,10 @@ func (s *service) Start() error {
}
func (s *service) Stop() error {
if logger.V(logger.InfoLevel) {
logger.Infof("Stoppping [service] %s", s.Name())
}
var err error
for _, fn := range s.opts.BeforeStop {
if err = fn(); err != nil {
@ -182,22 +185,11 @@ func (s *service) Run() error {
defer s.opts.Profile.Stop()
}
if logger.V(logger.InfoLevel) {
logger.Infof("Starting [service] %s", s.Name())
}
if err := s.Start(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
if s.opts.Signal {
signal.Notify(ch, signalutil.Shutdown()...)
}
select {
// wait on kill signal
case <-ch:
// wait on context cancel
case <-s.opts.Context.Done():
}