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.

208 lines
5.5 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
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. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync/atomic"
  6. "github.com/tendermint/tmlibs/log"
  7. )
  8. var (
  9. ErrAlreadyStarted = errors.New("already started")
  10. ErrAlreadyStopped = errors.New("already stopped")
  11. )
  12. // Service defines a service that can be started, stopped, and reset.
  13. type Service interface {
  14. // Start the service.
  15. // If it's already started or stopped, will return an error.
  16. // If OnStart() returns an error, it's returned by Start()
  17. Start() error
  18. OnStart() error
  19. // Stop the service.
  20. // If it's already stopped, will return an error.
  21. // OnStop must never error.
  22. Stop() error
  23. OnStop()
  24. // Reset the service.
  25. // Panics by default - must be overwritten to enable reset.
  26. Reset() error
  27. OnReset() error
  28. // Return true if the service is running
  29. IsRunning() bool
  30. // Quit returns a channel, which is closed once service is stopped.
  31. Quit() <-chan struct{}
  32. // String representation of the service
  33. String() string
  34. // SetLogger sets a logger.
  35. SetLogger(log.Logger)
  36. }
  37. /*
  38. Classical-inheritance-style service declarations. Services can be started, then
  39. stopped, then optionally restarted.
  40. Users can override the OnStart/OnStop methods. In the absence of errors, these
  41. methods are guaranteed to be called at most once. If OnStart returns an error,
  42. service won't be marked as started, so the user can call Start again.
  43. A call to Reset will panic, unless OnReset is overwritten, allowing
  44. OnStart/OnStop to be called again.
  45. The caller must ensure that Start and Stop are not called concurrently.
  46. It is ok to call Stop without calling Start first.
  47. Typical usage:
  48. type FooService struct {
  49. BaseService
  50. // private fields
  51. }
  52. func NewFooService() *FooService {
  53. fs := &FooService{
  54. // init
  55. }
  56. fs.BaseService = *NewBaseService(log, "FooService", fs)
  57. return fs
  58. }
  59. func (fs *FooService) OnStart() error {
  60. fs.BaseService.OnStart() // Always call the overridden method.
  61. // initialize private fields
  62. // start subroutines, etc.
  63. }
  64. func (fs *FooService) OnStop() error {
  65. fs.BaseService.OnStop() // Always call the overridden method.
  66. // close/destroy private fields
  67. // stop subroutines, etc.
  68. }
  69. */
  70. type BaseService struct {
  71. Logger log.Logger
  72. name string
  73. started uint32 // atomic
  74. stopped uint32 // atomic
  75. quit chan struct{}
  76. // The "subclass" of BaseService
  77. impl Service
  78. }
  79. // NewBaseService creates a new BaseService.
  80. func NewBaseService(logger log.Logger, name string, impl Service) *BaseService {
  81. if logger == nil {
  82. logger = log.NewNopLogger()
  83. }
  84. return &BaseService{
  85. Logger: logger,
  86. name: name,
  87. quit: make(chan struct{}),
  88. impl: impl,
  89. }
  90. }
  91. // SetLogger implements Service by setting a logger.
  92. func (bs *BaseService) SetLogger(l log.Logger) {
  93. bs.Logger = l
  94. }
  95. // Start implements Service by calling OnStart (if defined). An error will be
  96. // returned if the service is already running or stopped. Not to start the
  97. // stopped service, you need to call Reset.
  98. func (bs *BaseService) Start() error {
  99. if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
  100. if atomic.LoadUint32(&bs.stopped) == 1 {
  101. bs.Logger.Error(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl)
  102. return ErrAlreadyStopped
  103. } else {
  104. bs.Logger.Info(Fmt("Starting %v", bs.name), "impl", bs.impl)
  105. }
  106. err := bs.impl.OnStart()
  107. if err != nil {
  108. // revert flag
  109. atomic.StoreUint32(&bs.started, 0)
  110. return err
  111. }
  112. return nil
  113. } else {
  114. bs.Logger.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl)
  115. return ErrAlreadyStarted
  116. }
  117. }
  118. // OnStart implements Service by doing nothing.
  119. // NOTE: Do not put anything in here,
  120. // that way users don't need to call BaseService.OnStart()
  121. func (bs *BaseService) OnStart() error { return nil }
  122. // Stop implements Service by calling OnStop (if defined) and closing quit
  123. // channel. An error will be returned if the service is already stopped.
  124. func (bs *BaseService) Stop() error {
  125. if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) {
  126. bs.Logger.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl)
  127. bs.impl.OnStop()
  128. close(bs.quit)
  129. return nil
  130. } else {
  131. bs.Logger.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl)
  132. return ErrAlreadyStopped
  133. }
  134. }
  135. // OnStop implements Service by doing nothing.
  136. // NOTE: Do not put anything in here,
  137. // that way users don't need to call BaseService.OnStop()
  138. func (bs *BaseService) OnStop() {}
  139. // Reset implements Service by calling OnReset callback (if defined). An error
  140. // will be returned if the service is running.
  141. func (bs *BaseService) Reset() error {
  142. if !atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
  143. bs.Logger.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
  144. return fmt.Errorf("can't reset running %s", bs.name)
  145. }
  146. // whether or not we've started, we can reset
  147. atomic.CompareAndSwapUint32(&bs.started, 1, 0)
  148. bs.quit = make(chan struct{})
  149. return bs.impl.OnReset()
  150. }
  151. // OnReset implements Service by panicking.
  152. func (bs *BaseService) OnReset() error {
  153. PanicSanity("The service cannot be reset")
  154. return nil
  155. }
  156. // IsRunning implements Service by returning true or false depending on the
  157. // service's state.
  158. func (bs *BaseService) IsRunning() bool {
  159. return atomic.LoadUint32(&bs.started) == 1 && atomic.LoadUint32(&bs.stopped) == 0
  160. }
  161. // Wait blocks until the service is stopped.
  162. func (bs *BaseService) Wait() {
  163. <-bs.quit
  164. }
  165. // String implements Servce by returning a string representation of the service.
  166. func (bs *BaseService) String() string {
  167. return bs.name
  168. }
  169. // Quit Implements Service by returning a quit channel.
  170. func (bs *BaseService) Quit() <-chan struct{} {
  171. return bs.quit
  172. }