Skip to content

Commit

Permalink
Bring Name() back on the host interface
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuravi committed Mar 9, 2017
1 parent 47bf7e7 commit 33c6993
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
10 changes: 10 additions & 0 deletions modules/task/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (b NopBackend) Encoder() Encoding {
return &NopEncoding{}
}

// Name returns the name
func (b NopBackend) Name() string {
return "nopEncoding"
}

// Start implements the Module interface
func (b NopBackend) Start() error {
return nil
Expand Down Expand Up @@ -84,6 +89,11 @@ func (b *inMemBackend) Encoder() Encoding {
return gobEncoding
}

// Name returns the name
func (b *inMemBackend) Name() string {
return "nopEncoding"
}

// Start implements the Module interface
func (b *inMemBackend) Start() error {
go b.consumeFromQueue()
Expand Down
7 changes: 7 additions & 0 deletions modules/uhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var _ service.Module = &Module{}
// A Module is a module to handle HTTP requests
type Module struct {
service.Host
name string
config Config
log *zap.Logger
srv *http.Server
Expand Down Expand Up @@ -113,6 +114,7 @@ func newModule(
log.Error("Error loading http module configuration", zap.Error(err))
}
module := &Module{
name: _modName,
Host: host,
handlers: addHealth(getHandlers(host)),
mcb: defaultInboundMiddlewareChainBuilder(log, host.AuthClient(), newStatsClient(host.Metrics())),
Expand All @@ -123,6 +125,11 @@ func newModule(
return module, nil
}

// Name returns the name
func (m *Module) Name() string {
return m.name
}

// Start begins serving requests over HTTP
func (m *Module) Start() error {
mux := http.NewServeMux()
Expand Down
10 changes: 9 additions & 1 deletion modules/yarpc/yarpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func New(hookup ServiceCreateFunc, options ...ModuleOptionFn) service.ModuleProv
// the lifecycle of all of the in/out bound traffic, so we will
// register it in a dig.Graph provided with options/default graph.
type Module struct {
name string
host service.Host
statsClient *statsClient
config yarpcConfig
Expand Down Expand Up @@ -132,11 +133,13 @@ func newModule(
}
}
module := &Module{
name: _modName,
host: host,
statsClient: newStatsClient(host.Metrics()),
log: ulog.Logger(context.Background()).With(zap.String("module", _modName)),
}
if err := host.Config().Scope("modules").Get(_modName).PopulateStruct(&module.config); err != nil {
s := host.Config().Scope("modules")
if err := s.Get(module.name).PopulateStruct(&module.config); err != nil {
return nil, errs.Wrap(err, "can't read inbounds")
}

Expand Down Expand Up @@ -181,6 +184,11 @@ func newModule(
return module, nil
}

// Name returns the module name
func (m *Module) Name() string {
return m.name
}

// Start begins serving requests with YARPC.
func (m *Module) Start() error {
// TODO(alsam) allow services to advertise with a name separate from the host name.
Expand Down
17 changes: 14 additions & 3 deletions service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,29 @@ func (m *manager) startModules() []error {
select {
case err := <-errC:
if err != nil {
zap.L().Error("Error received while starting module", zap.String("module", modWrapper.Name()), zap.Error(err))
zap.L().Error(
"Error received while starting module",
zap.String("module", modWrapper.module.Name()),
zap.Error(err),
)
lock.Lock()
results = append(results, err)
lock.Unlock()
} else {
zap.L().Info("Module started up cleanly", zap.String("module", modWrapper.Name()))
zap.L().Info(
"Module started up cleanly",
zap.String("module", modWrapper.module.Name()),
)
}
case <-time.After(defaultStartupWait):
lock.Lock()
results = append(
results,
fmt.Errorf("module: %s didn't start after %v", modWrapper.Name(), defaultStartupWait),
fmt.Errorf(
"module: %s didn't start after %v",
modWrapper.module.Name(),
defaultStartupWait,
),
)
lock.Unlock()
}
Expand Down
13 changes: 4 additions & 9 deletions service/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package service

import (
"fmt"
"reflect"
"sync"
)

Expand All @@ -36,6 +35,8 @@ type ModuleProvider interface {

// Module is the basic building block of an UberFx service.
type Module interface {
// Name is the name of the module
Name() string
// Start the Module. If an error is returned, the Module is assumed to be not started.
// There is no need for this to be thread-safe, it will be called in a thread-safe manner.
Start() error
Expand Down Expand Up @@ -96,7 +97,6 @@ type moduleProvider struct {
func (m *moduleProvider) Create(host Host) (Module, error) { return m.createFunc(host) }

type moduleWrapper struct {
name string
module Module
scopedHost *scopedHost
isRunning bool
Expand Down Expand Up @@ -138,21 +138,16 @@ func newModuleWrapper(
return nil, nil
}
return &moduleWrapper{
name: reflect.TypeOf(module).PkgPath(),
module: module,
scopedHost: scopedHost,
}, nil
}

func (m *moduleWrapper) Name() string {
return m.name
}

func (m *moduleWrapper) Start() error {
m.lock.Lock()
defer m.lock.Unlock()
if m.isRunning {
return fmt.Errorf("module %s is running", m.name)
return fmt.Errorf("module %s is running", m.module.Name())
}
if err := m.module.Start(); err != nil {
return err
Expand All @@ -165,7 +160,7 @@ func (m *moduleWrapper) Stop() error {
m.lock.Lock()
defer m.lock.Unlock()
if !m.isRunning {
return fmt.Errorf("module %s is not running", m.name)
return fmt.Errorf("module %s is not running", m.module.Name())
}
m.isRunning = false
return m.module.Stop()
Expand Down
3 changes: 3 additions & 0 deletions service/stub_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func NewStubModule(host Host) *StubModule {
return &StubModule{Host: host}
}

// Name returns the name
func (s *StubModule) Name() string { return "stubModule" }

// Start mimics startup
func (s *StubModule) Start() error { return s.StartError }

Expand Down

0 comments on commit 33c6993

Please sign in to comment.