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.

154 lines
3.3 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() 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 "sync/atomic"
  34. import "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/log15"
  35. type Service interface {
  36. Start() (bool, error)
  37. OnStart() error
  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, error) {
  60. if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
  61. if atomic.LoadUint32(&bs.stopped) == 1 {
  62. if bs.log != nil {
  63. bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
  64. }
  65. return false, nil
  66. } else {
  67. if bs.log != nil {
  68. bs.log.Notice(Fmt("Starting %v", bs.name), "impl", bs.impl)
  69. }
  70. }
  71. err := bs.impl.OnStart()
  72. return true, err
  73. } else {
  74. if bs.log != nil {
  75. bs.log.Info(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
  76. }
  77. return false, nil
  78. }
  79. }
  80. // Implements Service
  81. func (bs *BaseService) OnStart() error { return nil }
  82. // Implements Service
  83. func (bs *BaseService) Stop() bool {
  84. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  85. if bs.log != nil {
  86. bs.log.Notice(Fmt("Stopping %v", bs.name), "impl", bs.impl)
  87. }
  88. bs.impl.OnStop()
  89. return true
  90. } else {
  91. if bs.log != nil {
  92. bs.log.Notice(Fmt("Not stopping %v", bs.name), "impl", bs.impl)
  93. }
  94. return false
  95. }
  96. }
  97. // Implements Service
  98. func (bs *BaseService) OnStop() {}
  99. // Implements Service
  100. func (bs *BaseService) IsRunning() bool {
  101. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  102. }
  103. // Implements Servce
  104. func (bs *BaseService) String() string {
  105. return bs.name
  106. }
  107. //----------------------------------------
  108. type QuitService struct {
  109. BaseService
  110. Quit chan struct{}
  111. }
  112. func NewQuitService(log log15.Logger, name string, impl Service) *QuitService {
  113. return &QuitService{
  114. BaseService: *NewBaseService(log, name, impl),
  115. Quit: nil,
  116. }
  117. }
  118. // NOTE: when overriding OnStart, must call .QuitService.OnStart().
  119. func (qs *QuitService) OnStart() error {
  120. qs.Quit = make(chan struct{})
  121. return nil
  122. }
  123. // NOTE: when overriding OnStop, must call .QuitService.OnStop().
  124. func (qs *QuitService) OnStop() {
  125. if qs.Quit != nil {
  126. close(qs.Quit)
  127. }
  128. }