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.

184 lines
4.0 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
  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. // The "subclass" of BaseService
  54. impl Service
  55. }
  56. func NewBaseService(log log15.Logger, name string, impl Service) *BaseService {
  57. return &BaseService{
  58. log: log,
  59. name: name,
  60. impl: impl,
  61. }
  62. }
  63. // Implements Servce
  64. func (bs *BaseService) Start() (bool, error) {
  65. if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
  66. if atomic.LoadUint32(&bs.stopped) == 1 {
  67. if bs.log != nil {
  68. bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
  69. }
  70. return false, nil
  71. } else {
  72. if bs.log != nil {
  73. bs.log.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
  74. }
  75. }
  76. err := bs.impl.OnStart()
  77. return true, err
  78. } else {
  79. if bs.log != nil {
  80. bs.log.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
  81. }
  82. return false, nil
  83. }
  84. }
  85. // Implements Service
  86. func (bs *BaseService) OnStart() error { return nil }
  87. // Implements Service
  88. func (bs *BaseService) Stop() bool {
  89. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  90. if bs.log != nil {
  91. bs.log.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
  92. }
  93. bs.impl.OnStop()
  94. return true
  95. } else {
  96. if bs.log != nil {
  97. bs.log.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
  98. }
  99. return false
  100. }
  101. }
  102. // Implements Service
  103. func (bs *BaseService) OnStop() {}
  104. // Implements Service
  105. func (bs *BaseService) Reset() (bool, error) {
  106. if atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
  107. // whether or not we've started, we can reset
  108. atomic.CompareAndSwapUint32(&bs.started, 1, 0)
  109. return true, bs.impl.OnReset()
  110. } else {
  111. if bs.log != nil {
  112. bs.log.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
  113. }
  114. return false, nil
  115. }
  116. // never happens
  117. return false, nil
  118. }
  119. // Implements Service
  120. func (bs *BaseService) OnReset() error {
  121. PanicSanity("The service cannot be reset")
  122. return nil
  123. }
  124. // Implements Service
  125. func (bs *BaseService) IsRunning() bool {
  126. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  127. }
  128. // Implements Servce
  129. func (bs *BaseService) String() string {
  130. return bs.name
  131. }
  132. //----------------------------------------
  133. type QuitService struct {
  134. BaseService
  135. Quit chan struct{}
  136. }
  137. func NewQuitService(log log15.Logger, name string, impl Service) *QuitService {
  138. return &QuitService{
  139. BaseService: *NewBaseService(log, name, impl),
  140. Quit: nil,
  141. }
  142. }
  143. // NOTE: when overriding OnStart, must call .QuitService.OnStart().
  144. func (qs *QuitService) OnStart() error {
  145. qs.Quit = make(chan struct{})
  146. return nil
  147. }
  148. // NOTE: when overriding OnStop, must call .QuitService.OnStop().
  149. func (qs *QuitService) OnStop() {
  150. if qs.Quit != nil {
  151. close(qs.Quit)
  152. }
  153. }