close micro-roadmap/issues/12
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
cacd33e84f
commit
24be220f91
14
options.go
14
options.go
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/unistack-org/micro/v3/config"
|
"github.com/unistack-org/micro/v3/config"
|
||||||
"github.com/unistack-org/micro/v3/debug/profile"
|
"github.com/unistack-org/micro/v3/debug/profile"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"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/registry"
|
||||||
"github.com/unistack-org/micro/v3/router"
|
"github.com/unistack-org/micro/v3/router"
|
||||||
"github.com/unistack-org/micro/v3/runtime"
|
"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/server"
|
||||||
"github.com/unistack-org/micro/v3/store"
|
"github.com/unistack-org/micro/v3/store"
|
||||||
"github.com/unistack-org/micro/v3/tracer"
|
"github.com/unistack-org/micro/v3/tracer"
|
||||||
"github.com/unistack-org/micro/v3/network/transport"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Options for micro service
|
// Options for micro service
|
||||||
@ -47,14 +47,11 @@ type Options struct {
|
|||||||
// Other options for implementations of the interface
|
// Other options for implementations of the interface
|
||||||
// can be stored in a context
|
// can be stored in a context
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
|
||||||
Signal bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newOptions(opts ...Option) Options {
|
func newOptions(opts ...Option) Options {
|
||||||
opt := Options{
|
opt := Options{
|
||||||
Context: context.Background(),
|
Context: context.Background(),
|
||||||
Signal: true,
|
|
||||||
Server: server.DefaultServer,
|
Server: server.DefaultServer,
|
||||||
Client: client.DefaultClient,
|
Client: client.DefaultClient,
|
||||||
Broker: broker.DefaultBroker,
|
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
|
// Profile to be used for debug profile
|
||||||
func Profile(p profile.Profile) Option {
|
func Profile(p profile.Profile) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
|
26
service.go
26
service.go
@ -1,8 +1,6 @@
|
|||||||
package micro
|
package micro
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
rtime "runtime"
|
rtime "runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -10,7 +8,6 @@ import (
|
|||||||
"github.com/unistack-org/micro/v3/client"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
signalutil "github.com/unistack-org/micro/v3/util/signal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
@ -20,11 +17,9 @@ type service struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newService(opts ...Option) Service {
|
func newService(opts ...Option) Service {
|
||||||
service := &service{}
|
|
||||||
options := newOptions(opts...)
|
options := newOptions(opts...)
|
||||||
|
|
||||||
// set opts
|
service := &service{opts: options}
|
||||||
service.opts = options
|
|
||||||
|
|
||||||
return service
|
return service
|
||||||
}
|
}
|
||||||
@ -127,6 +122,10 @@ func (s *service) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Start() error {
|
func (s *service) Start() error {
|
||||||
|
if logger.V(logger.InfoLevel) {
|
||||||
|
logger.Infof("Starting [service] %s", s.Name())
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
for _, fn := range s.opts.BeforeStart {
|
for _, fn := range s.opts.BeforeStart {
|
||||||
if err = fn(); err != nil {
|
if err = fn(); err != nil {
|
||||||
@ -148,6 +147,10 @@ func (s *service) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Stop() error {
|
func (s *service) Stop() error {
|
||||||
|
if logger.V(logger.InfoLevel) {
|
||||||
|
logger.Infof("Stoppping [service] %s", s.Name())
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
for _, fn := range s.opts.BeforeStop {
|
for _, fn := range s.opts.BeforeStop {
|
||||||
if err = fn(); err != nil {
|
if err = fn(); err != nil {
|
||||||
@ -182,22 +185,11 @@ func (s *service) Run() error {
|
|||||||
defer s.opts.Profile.Stop()
|
defer s.opts.Profile.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
if logger.V(logger.InfoLevel) {
|
|
||||||
logger.Infof("Starting [service] %s", s.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.Start(); err != nil {
|
if err := s.Start(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ch := make(chan os.Signal, 1)
|
|
||||||
if s.opts.Signal {
|
|
||||||
signal.Notify(ch, signalutil.Shutdown()...)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
// wait on kill signal
|
|
||||||
case <-ch:
|
|
||||||
// wait on context cancel
|
// wait on context cancel
|
||||||
case <-s.opts.Context.Done():
|
case <-s.opts.Context.Done():
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user