You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync/atomic"
  6. "github.com/tendermint/tendermint/libs/log"
  7. )
  8. var (
  9. ErrAlreadyStarted = errors.New("already started")
  10. ErrAlreadyStopped = errors.New("already stopped")
  11. )
  12. // Service defines a service that can be started, stopped, and reset.
  13. type Service interface {
  14. // Start the service.
  15. // If it's already started or stopped, will return an error.
  16. // If OnStart() returns an error, it's returned by Start()
  17. Start() error
  18. OnStart() error
  19. // Stop the service.
  20. // If it's already stopped, will return an error.
  21. // OnStop must never error.
  22. Stop() error
  23. OnStop()
  24. // Reset the service.
  25. // Panics by default - must be overwritten to enable reset.
  26. Reset() error
  27. OnReset() error
  28. // Return true if the service is running
  29. IsRunning() bool
  30. // Quit returns a channel, which is closed once service is stopped.
  31. Quit() <-chan struct{}
  32. // String representation of the service
  33. String() string
  34. // SetLogger sets a logger.
  35. SetLogger(log.Logger)
  36. }
  37. /*
  38. Classical-inheritance-style service declarations. Services can be started, then
  39. stopped, then optionally restarted.
  40. Users can override the OnStart/OnStop methods. In the absence of errors, these
  41. methods are guaranteed to be called at most once. If OnStart returns an error,
  42. service won't be marked as started, so the user can call Start again.
  43. A call to Reset will panic, unless OnReset is overwritten, allowing
  44. OnStart/OnStop to be called again.
  45. The caller must ensure that Start and Stop are not called concurrently.
  46. It is ok to call Stop without calling Start first.
  47. Typical usage:
  48. type FooService struct {
  49. BaseService
  50. // private fields
  51. }
  52. func NewFooService() *FooService {
  53. fs := &FooService{
  54. // init
  55. }
  56. fs.BaseService = *NewBaseService(log, "FooService", fs)
  57. return fs
  58. }
  59. func (fs *FooService) OnStart() error {
  60. fs.BaseService.OnStart() // Always call the overridden method.
  61. // initialize private fields
  62. // start subroutines, etc.
  63. }
  64. func (fs *FooService) OnStop() error {
  65. fs.BaseService.OnStop() // Always call the overridden method.
  66. // close/destroy private fields
  67. // stop subroutines, etc.
  68. }
  69. */
  70. type BaseService struct {
  71. Logger log.Logger
  72. name string
  73. started uint32 // atomic
  74. stopped uint32 // atomic
  75. quit chan struct{}
  76. // The "subclass" of BaseService
  77. impl Service
  78. }
  79. // NewBaseService creates a new BaseService.
  80. func NewBaseService(logger log.Logger, name string, impl Service) *BaseService {
  81. if logger == nil {
  82. logger = log.NewNopLogger()
  83. }
  84. return &BaseService{
  85. Logger: logger,
  86. name: name,
  87. quit: make(chan struct{}),
  88. impl: impl,
  89. }
  90. }
  91. // SetLogger implements Service by setting a logger.
  92. func (bs *BaseService) SetLogger(l log.Logger) {
  93. bs.Logger = l
  94. }
  95. // Start implements Service by calling OnStart (if defined). An error will be
  96. // returned if the service is already running or stopped. Not to start the
  97. // stopped service, you need to call Reset.
  98. func (bs *BaseService) Start() error {
  99. if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
  100. if atomic.LoadUint32(&bs.stopped) == 1 {
  101. bs.Logger.Error(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
  102. return ErrAlreadyStopped
  103. }
  104. bs.Logger.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
  105. err := bs.impl.OnStart()
  106. if err != nil {
  107. // revert flag
  108. atomic.StoreUint32(&bs.started, 0)
  109. return err
  110. }
  111. return nil
  112. }
  113. bs.Logger.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
  114. return ErrAlreadyStarted
  115. }
  116. // OnStart implements Service by doing nothing.
  117. // NOTE: Do not put anything in here,
  118. // that way users don't need to call BaseService.OnStart()
  119. func (bs *BaseService) OnStart() error { return nil }
  120. // Stop implements Service by calling OnStop (if defined) and closing quit
  121. // channel. An error will be returned if the service is already stopped.
  122. func (bs *BaseService) Stop() error {
  123. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  124. bs.Logger.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
  125. bs.impl.OnStop()
  126. close(bs.quit)
  127. return nil
  128. }
  129. bs.Logger.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
  130. return ErrAlreadyStopped
  131. }
  132. // OnStop implements Service by doing nothing.
  133. // NOTE: Do not put anything in here,
  134. // that way users don't need to call BaseService.OnStop()
  135. func (bs *BaseService) OnStop() {}
  136. // Reset implements Service by calling OnReset callback (if defined). An error
  137. // will be returned if the service is running.
  138. func (bs *BaseService) Reset() error {
  139. if !atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
  140. bs.Logger.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
  141. return fmt.Errorf("can't reset running %s", bs.name)
  142. }
  143. // whether or not we've started, we can reset
  144. atomic.CompareAndSwapUint32(&bs.started, 1, 0)
  145. bs.quit = make(chan struct{})
  146. return bs.impl.OnReset()
  147. }
  148. // OnReset implements Service by panicking.
  149. func (bs *BaseService) OnReset() error {
  150. PanicSanity("The service cannot be reset")
  151. return nil
  152. }
  153. // IsRunning implements Service by returning true or false depending on the
  154. // service's state.
  155. func (bs *BaseService) IsRunning() bool {
  156. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  157. }
  158. // Wait blocks until the service is stopped.
  159. func (bs *BaseService) Wait() {
  160. <-bs.quit
  161. }
  162. // String implements Servce by returning a string representation of the service.
  163. func (bs *BaseService) String() string {
  164. return bs.name
  165. }
  166. // Quit Implements Service by returning a quit channel.
  167. func (bs *BaseService) Quit() <-chan struct{} {
  168. return bs.quit
  169. }