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.

141 lines
3.1 KiB

  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() {
  22. fs.BaseService.OnStart() // Always call the overridden method.
  23. // initialize private fields
  24. // start subroutines, etc.
  25. }
  26. func (fs *FooService) OnStop() {
  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 "sync/atomic"
  34. import "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/log15"
  35. type Service interface {
  36. Start() bool
  37. OnStart()
  38. Stop() bool
  39. OnStop()
  40. IsRunning() bool
  41. String() string
  42. }
  43. type BaseService struct {
  44. log log15.Logger
  45. name string
  46. started uint32 // atomic
  47. stopped uint32 // atomic
  48. // The "subclass" of BaseService
  49. impl Service
  50. }
  51. func NewBaseService(log log15.Logger, name string, impl Service) *BaseService {
  52. return &BaseService{
  53. log: log,
  54. name: name,
  55. impl: impl,
  56. }
  57. }
  58. // Implements Servce
  59. func (bs *BaseService) Start() bool {
  60. if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
  61. if atomic.LoadUint32(&bs.stopped) == 1 {
  62. bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
  63. return false
  64. } else {
  65. bs.log.Notice(Fmt("Starting %v", bs.name), "impl", bs.impl)
  66. }
  67. bs.impl.OnStart()
  68. return true
  69. } else {
  70. bs.log.Info(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
  71. return false
  72. }
  73. }
  74. // Implements Service
  75. func (bs *BaseService) OnStart() {}
  76. // Implements Service
  77. func (bs *BaseService) Stop() bool {
  78. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  79. bs.log.Notice(Fmt("Stopping %v", bs.name), "impl", bs.impl)
  80. bs.impl.OnStop()
  81. return true
  82. } else {
  83. bs.log.Notice(Fmt("Not stopping %v", bs.name), "impl", bs.impl)
  84. return false
  85. }
  86. }
  87. // Implements Service
  88. func (bs *BaseService) OnStop() {}
  89. // Implements Service
  90. func (bs *BaseService) IsRunning() bool {
  91. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  92. }
  93. // Implements Servce
  94. func (bs *BaseService) String() string {
  95. return bs.name
  96. }
  97. //----------------------------------------
  98. type QuitService struct {
  99. BaseService
  100. Quit chan struct{}
  101. }
  102. func NewQuitService(log log15.Logger, name string, impl Service) *QuitService {
  103. return &QuitService{
  104. BaseService: *NewBaseService(log, name, impl),
  105. Quit: nil,
  106. }
  107. }
  108. // NOTE: when overriding OnStart, must call .QuitService.OnStart().
  109. func (qs *QuitService) OnStart() {
  110. qs.Quit = make(chan struct{})
  111. }
  112. // NOTE: when overriding OnStop, must call .QuitService.OnStop().
  113. func (qs *QuitService) OnStop() {
  114. close(qs.Quit)
  115. }