tendermint/common/service.go

209 lines
5.5 KiB
Go
Raw Normal View History

2015-10-21 12:15:19 -07:00
package common
2015-10-21 12:26:50 -07:00
import (
"errors"
2017-11-27 21:42:36 -08:00
"fmt"
2015-10-21 12:26:50 -07:00
"sync/atomic"
"github.com/tendermint/tmlibs/log"
2015-10-21 12:26:50 -07:00
)
2015-10-21 12:15:19 -07:00
var (
ErrAlreadyStarted = errors.New("already started")
ErrAlreadyStopped = errors.New("already stopped")
)
2017-11-28 21:16:15 -08:00
// Service defines a service that can be started, stopped, and reset.
2015-10-21 12:15:19 -07:00
type Service interface {
2017-11-28 21:16:15 -08:00
// Start the service.
// If it's already started or stopped, will return an error.
// If OnStart() returns an error, it's returned by Start()
Start() error
2015-10-21 12:15:19 -07:00
OnStart() error
2017-11-28 21:16:15 -08:00
// Stop the service.
// If it's already stopped, will return an error.
// OnStop must never error.
Stop() error
2015-10-21 12:15:19 -07:00
OnStop()
2017-11-28 21:16:15 -08:00
// Reset the service.
// Panics by default - must be overwritten to enable reset.
2017-11-27 21:42:36 -08:00
Reset() error
2016-08-10 21:04:07 -07:00
OnReset() error
2017-11-28 21:16:15 -08:00
// Return true if the service is running
2015-10-21 12:15:19 -07:00
IsRunning() bool
// Quit returns a channel, which is closed once service is stopped.
Quit() <-chan struct{}
2017-11-28 21:16:15 -08:00
// String representation of the service
2015-10-21 12:15:19 -07:00
String() string
// SetLogger sets a logger.
SetLogger(log.Logger)
2015-10-21 12:15:19 -07:00
}
/*
Classical-inheritance-style service declarations. Services can be started, then
stopped, then optionally restarted.
Users can override the OnStart/OnStop methods. In the absence of errors, these
methods are guaranteed to be called at most once. If OnStart returns an error,
service won't be marked as started, so the user can call Start again.
A call to Reset will panic, unless OnReset is overwritten, allowing
OnStart/OnStop to be called again.
The caller must ensure that Start and Stop are not called concurrently.
It is ok to call Stop without calling Start first.
Typical usage:
type FooService struct {
BaseService
// private fields
}
func NewFooService() *FooService {
fs := &FooService{
// init
}
fs.BaseService = *NewBaseService(log, "FooService", fs)
return fs
}
func (fs *FooService) OnStart() error {
fs.BaseService.OnStart() // Always call the overridden method.
// initialize private fields
// start subroutines, etc.
}
func (fs *FooService) OnStop() error {
fs.BaseService.OnStop() // Always call the overridden method.
// close/destroy private fields
// stop subroutines, etc.
}
*/
2015-10-21 12:15:19 -07:00
type BaseService struct {
Logger log.Logger
2015-10-21 12:15:19 -07:00
name string
started uint32 // atomic
stopped uint32 // atomic
quit chan struct{}
2015-10-21 12:15:19 -07:00
// The "subclass" of BaseService
impl Service
}
// NewBaseService creates a new BaseService.
func NewBaseService(logger log.Logger, name string, impl Service) *BaseService {
if logger == nil {
logger = log.NewNopLogger()
}
2015-10-21 12:15:19 -07:00
return &BaseService{
Logger: logger,
name: name,
quit: make(chan struct{}),
impl: impl,
2015-10-21 12:15:19 -07:00
}
}
// SetLogger implements Service by setting a logger.
func (bs *BaseService) SetLogger(l log.Logger) {
bs.Logger = l
}
// Start implements Service by calling OnStart (if defined). An error will be
// returned if the service is already running or stopped. Not to start the
// stopped service, you need to call Reset.
func (bs *BaseService) Start() error {
2015-10-21 12:15:19 -07:00
if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
if atomic.LoadUint32(&bs.stopped) == 1 {
bs.Logger.Error(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
return ErrAlreadyStopped
2015-10-21 12:15:19 -07:00
} else {
bs.Logger.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
2015-10-21 12:15:19 -07:00
}
err := bs.impl.OnStart()
if err != nil {
// revert flag
atomic.StoreUint32(&bs.started, 0)
return err
}
return nil
2015-10-21 12:15:19 -07:00
} else {
bs.Logger.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
return ErrAlreadyStarted
2015-10-21 12:15:19 -07:00
}
}
// OnStart implements Service by doing nothing.
2016-10-28 12:09:22 -07:00
// NOTE: Do not put anything in here,
// that way users don't need to call BaseService.OnStart()
2015-10-21 12:15:19 -07:00
func (bs *BaseService) OnStart() error { return nil }
// Stop implements Service by calling OnStop (if defined) and closing quit
// channel. An error will be returned if the service is already stopped.
func (bs *BaseService) Stop() error {
2015-10-21 12:15:19 -07:00
if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
bs.Logger.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
2015-10-21 12:15:19 -07:00
bs.impl.OnStop()
close(bs.quit)
return nil
2015-10-21 12:15:19 -07:00
} else {
bs.Logger.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
return ErrAlreadyStopped
2015-10-21 12:15:19 -07:00
}
}
// OnStop implements Service by doing nothing.
2016-10-28 12:09:22 -07:00
// NOTE: Do not put anything in here,
// that way users don't need to call BaseService.OnStop()
2015-10-21 12:15:19 -07:00
func (bs *BaseService) OnStop() {}
// Reset implements Service by calling OnReset callback (if defined). An error
// will be returned if the service is running.
2017-11-27 21:42:36 -08:00
func (bs *BaseService) Reset() error {
2017-10-03 21:13:58 -07:00
if !atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
bs.Logger.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
2017-11-27 21:42:36 -08:00
return fmt.Errorf("can't reset running %s", bs.name)
2016-08-10 21:04:07 -07:00
}
2017-10-03 09:18:21 -07:00
// whether or not we've started, we can reset
atomic.CompareAndSwapUint32(&bs.started, 1, 0)
bs.quit = make(chan struct{})
2017-11-27 21:42:36 -08:00
return bs.impl.OnReset()
2016-08-10 21:04:07 -07:00
}
// OnReset implements Service by panicking.
2016-08-10 21:04:07 -07:00
func (bs *BaseService) OnReset() error {
PanicSanity("The service cannot be reset")
return nil
}
// IsRunning implements Service by returning true or false depending on the
// service's state.
2015-10-21 12:15:19 -07:00
func (bs *BaseService) IsRunning() bool {
return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
}
// Wait blocks until the service is stopped.
2016-10-28 12:09:34 -07:00
func (bs *BaseService) Wait() {
<-bs.quit
2016-10-28 12:09:22 -07:00
}
// String implements Servce by returning a string representation of the service.
2015-10-21 12:15:19 -07:00
func (bs *BaseService) String() string {
return bs.name
}
// Quit Implements Service by returning a quit channel.
func (bs *BaseService) Quit() <-chan struct{} {
return bs.quit
2015-10-21 12:15:19 -07:00
}