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.

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