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.

221 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
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. }
  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 service -- already stopped", bs.name),
  109. "impl", bs.impl)
  110. // revert flag
  111. atomic.StoreUint32(&bs.started, 0)
  112. return ErrAlreadyStopped
  113. }
  114. bs.Logger.Info(fmt.Sprintf("Starting %v service", bs.name), "impl", bs.impl.String())
  115. err := bs.impl.OnStart()
  116. if err != nil {
  117. // revert flag
  118. atomic.StoreUint32(&bs.started, 0)
  119. return err
  120. }
  121. return nil
  122. }
  123. bs.Logger.Debug(fmt.Sprintf("Not starting %v service -- already started", bs.name), "impl", bs.impl)
  124. return ErrAlreadyStarted
  125. }
  126. // OnStart implements Service by doing nothing.
  127. // NOTE: Do not put anything in here,
  128. // that way users don't need to call BaseService.OnStart()
  129. func (bs *BaseService) OnStart() error { return nil }
  130. // Stop implements Service by calling OnStop (if defined) and closing quit
  131. // channel. An error will be returned if the service is already stopped.
  132. func (bs *BaseService) Stop() error {
  133. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  134. if atomic.LoadUint32(&bs.started) == 0 {
  135. bs.Logger.Error(fmt.Sprintf("Not stopping %v service -- has not been started yet", bs.name),
  136. "impl", bs.impl)
  137. // revert flag
  138. atomic.StoreUint32(&bs.stopped, 0)
  139. return ErrNotStarted
  140. }
  141. bs.Logger.Info(fmt.Sprintf("Stopping %v service", bs.name), "impl", bs.impl)
  142. bs.impl.OnStop()
  143. close(bs.quit)
  144. return nil
  145. }
  146. bs.Logger.Debug(fmt.Sprintf("Stopping %v service (already stopped)", bs.name), "impl", bs.impl)
  147. return ErrAlreadyStopped
  148. }
  149. // OnStop implements Service by doing nothing.
  150. // NOTE: Do not put anything in here,
  151. // that way users don't need to call BaseService.OnStop()
  152. func (bs *BaseService) OnStop() {}
  153. // Reset implements Service by calling OnReset callback (if defined). An error
  154. // will be returned if the service is running.
  155. func (bs *BaseService) Reset() error {
  156. if !atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
  157. bs.Logger.Debug(fmt.Sprintf("Can't reset %v service. Not stopped", bs.name), "impl", bs.impl)
  158. return fmt.Errorf("can't reset running %s", bs.name)
  159. }
  160. // whether or not we've started, we can reset
  161. atomic.CompareAndSwapUint32(&bs.started, 1, 0)
  162. bs.quit = make(chan struct{})
  163. return bs.impl.OnReset()
  164. }
  165. // OnReset implements Service by panicking.
  166. func (bs *BaseService) OnReset() error {
  167. panic("The service cannot be reset")
  168. }
  169. // IsRunning implements Service by returning true or false depending on the
  170. // service's state.
  171. func (bs *BaseService) IsRunning() bool {
  172. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  173. }
  174. // Wait blocks until the service is stopped.
  175. func (bs *BaseService) Wait() {
  176. <-bs.quit
  177. }
  178. // String implements Service by returning a string representation of the service.
  179. func (bs *BaseService) String() string {
  180. return bs.name
  181. }
  182. // Quit Implements Service by returning a quit channel.
  183. func (bs *BaseService) Quit() <-chan struct{} {
  184. return bs.quit
  185. }