tendermint/common/service.go

155 lines
3.3 KiB
Go
Raw Normal View History

/*
Classical-inheritance-style service declarations.
Services can be started, then stopped.
2015-07-21 18:31:01 -07:00
Users can override the OnStart/OnStop methods.
These methods are guaranteed to be called at most once.
Caller must ensure that Start() and Stop() are not called concurrently.
It is ok to call Stop() without calling Start() first.
Services cannot be re-started unless otherwise documented.
Typical usage:
type FooService struct {
BaseService
// private fields
}
func NewFooService() *FooService {
fs := &FooService{
// init
}
2015-07-21 18:31:01 -07:00
fs.BaseService = *NewBaseService(log, "FooService", fs)
return fs
}
func (fs *FooService) OnStart() error {
2015-07-21 18:31:01 -07:00
fs.BaseService.OnStart() // Always call the overridden method.
// initialize private fields
// start subroutines, etc.
}
func (fs *FooService) OnStop() error {
2015-07-21 18:31:01 -07:00
fs.BaseService.OnStop() // Always call the overridden method.
// close/destroy private fields
// stop subroutines, etc.
}
*/
2015-07-19 17:42:01 -07:00
package common
import "sync/atomic"
import "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/log15"
type Service interface {
Start() (bool, error)
OnStart() error
Stop() bool
2015-07-21 18:31:01 -07:00
OnStop()
IsRunning() bool
2015-07-20 16:55:05 -07:00
String() string
}
2015-07-19 17:42:01 -07:00
type BaseService struct {
log log15.Logger
2015-07-19 17:42:01 -07:00
name string
started uint32 // atomic
stopped uint32 // atomic
// The "subclass" of BaseService
impl Service
2015-07-19 17:42:01 -07:00
}
func NewBaseService(log log15.Logger, name string, impl Service) *BaseService {
2015-07-19 17:42:01 -07:00
return &BaseService{
log: log,
name: name,
impl: impl,
2015-07-19 17:42:01 -07:00
}
}
// Implements Servce
func (bs *BaseService) Start() (bool, error) {
2015-07-19 17:42:01 -07:00
if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
if atomic.LoadUint32(&bs.stopped) == 1 {
if bs.log != nil {
bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
}
return false, nil
2015-07-19 17:42:01 -07:00
} else {
if bs.log != nil {
bs.log.Notice(Fmt("Starting %v", bs.name), "impl", bs.impl)
}
2015-07-19 17:42:01 -07:00
}
err := bs.impl.OnStart()
return true, err
2015-07-19 17:42:01 -07:00
} else {
if bs.log != nil {
bs.log.Info(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
}
return false, nil
2015-07-19 17:42:01 -07:00
}
}
// Implements Service
func (bs *BaseService) OnStart() error { return nil }
// Implements Service
2015-07-19 17:42:01 -07:00
func (bs *BaseService) Stop() bool {
if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
if bs.log != nil {
bs.log.Notice(Fmt("Stopping %v", bs.name), "impl", bs.impl)
}
2015-07-21 18:31:01 -07:00
bs.impl.OnStop()
2015-07-19 17:42:01 -07:00
return true
} else {
if bs.log != nil {
bs.log.Notice(Fmt("Not stopping %v", bs.name), "impl", bs.impl)
}
2015-07-19 17:42:01 -07:00
return false
}
}
// Implements Service
2015-07-21 18:31:01 -07:00
func (bs *BaseService) OnStop() {}
// Implements Service
2015-07-19 17:42:01 -07:00
func (bs *BaseService) IsRunning() bool {
return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
}
2015-07-20 16:55:05 -07:00
// Implements Servce
func (bs *BaseService) String() string {
return bs.name
}
//----------------------------------------
type QuitService struct {
BaseService
Quit chan struct{}
}
func NewQuitService(log log15.Logger, name string, impl Service) *QuitService {
return &QuitService{
BaseService: *NewBaseService(log, name, impl),
Quit: nil,
}
}
2015-07-21 18:31:01 -07:00
// NOTE: when overriding OnStart, must call .QuitService.OnStart().
func (qs *QuitService) OnStart() error {
qs.Quit = make(chan struct{})
return nil
}
2015-07-21 18:31:01 -07:00
// NOTE: when overriding OnStop, must call .QuitService.OnStop().
func (qs *QuitService) OnStop() {
if qs.Quit != nil {
close(qs.Quit)
}
}