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.

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