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.

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