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.

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