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.

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