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.

157 lines
3.3 KiB

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.
  4. Users can override the OnStart/OnStop methods.
  5. These methods are guaranteed to be called at most once.
  6. Caller must ensure that Start() and Stop() are not called concurrently.
  7. It is ok to call Stop() without calling Start() first.
  8. Services cannot be re-started unless otherwise documented.
  9. Typical usage:
  10. type FooService struct {
  11. BaseService
  12. // private fields
  13. }
  14. func NewFooService() *FooService {
  15. fs := &FooService{
  16. // init
  17. }
  18. fs.BaseService = *NewBaseService(log, "FooService", fs)
  19. return fs
  20. }
  21. func (fs *FooService) OnStart() error {
  22. fs.BaseService.OnStart() // Always call the overridden method.
  23. // initialize private fields
  24. // start subroutines, etc.
  25. }
  26. func (fs *FooService) OnStop() error {
  27. fs.BaseService.OnStop() // Always call the overridden method.
  28. // close/destroy private fields
  29. // stop subroutines, etc.
  30. }
  31. */
  32. package common
  33. import (
  34. "sync/atomic"
  35. "github.com/tendermint/log15"
  36. )
  37. type Service interface {
  38. Start() (bool, error)
  39. OnStart() error
  40. Stop() bool
  41. OnStop()
  42. IsRunning() bool
  43. String() string
  44. }
  45. type BaseService struct {
  46. log log15.Logger
  47. name string
  48. started uint32 // atomic
  49. stopped uint32 // atomic
  50. // The "subclass" of BaseService
  51. impl Service
  52. }
  53. func NewBaseService(log log15.Logger, name string, impl Service) *BaseService {
  54. return &BaseService{
  55. log: log,
  56. name: name,
  57. impl: impl,
  58. }
  59. }
  60. // Implements Servce
  61. func (bs *BaseService) Start() (bool, error) {
  62. if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
  63. if atomic.LoadUint32(&bs.stopped) == 1 {
  64. if bs.log != nil {
  65. bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
  66. }
  67. return false, nil
  68. } else {
  69. if bs.log != nil {
  70. bs.log.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
  71. }
  72. }
  73. err := bs.impl.OnStart()
  74. return true, err
  75. } else {
  76. if bs.log != nil {
  77. bs.log.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
  78. }
  79. return false, nil
  80. }
  81. }
  82. // Implements Service
  83. func (bs *BaseService) OnStart() error { return nil }
  84. // Implements Service
  85. func (bs *BaseService) Stop() bool {
  86. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  87. if bs.log != nil {
  88. bs.log.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
  89. }
  90. bs.impl.OnStop()
  91. return true
  92. } else {
  93. if bs.log != nil {
  94. bs.log.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
  95. }
  96. return false
  97. }
  98. }
  99. // Implements Service
  100. func (bs *BaseService) OnStop() {}
  101. // Implements Service
  102. func (bs *BaseService) IsRunning() bool {
  103. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  104. }
  105. // Implements Servce
  106. func (bs *BaseService) String() string {
  107. return bs.name
  108. }
  109. //----------------------------------------
  110. type QuitService struct {
  111. BaseService
  112. Quit chan struct{}
  113. }
  114. func NewQuitService(log log15.Logger, name string, impl Service) *QuitService {
  115. return &QuitService{
  116. BaseService: *NewBaseService(log, name, impl),
  117. Quit: nil,
  118. }
  119. }
  120. // NOTE: when overriding OnStart, must call .QuitService.OnStart().
  121. func (qs *QuitService) OnStart() error {
  122. qs.Quit = make(chan struct{})
  123. return nil
  124. }
  125. // NOTE: when overriding OnStop, must call .QuitService.OnStop().
  126. func (qs *QuitService) OnStop() {
  127. if qs.Quit != nil {
  128. close(qs.Quit)
  129. }
  130. }