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.

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