@ -130,12 +130,13 @@ func (bs *BaseService) SetLogger(l log.Logger) {
func ( bs * BaseService ) Start ( ) error {
func ( bs * BaseService ) Start ( ) error {
if atomic . CompareAndSwapUint32 ( & bs . started , 0 , 1 ) {
if atomic . CompareAndSwapUint32 ( & bs . started , 0 , 1 ) {
if atomic . LoadUint32 ( & bs . stopped ) == 1 {
if atomic . LoadUint32 ( & bs . stopped ) == 1 {
bs . Logger . Error ( fmt . Sprintf ( "Not starting %v -- already stopped" , bs . name ) , "impl" , bs . impl )
bs . Logger . Error ( fmt . Sprintf ( "Not starting %v service -- already stopped" , bs . name ) ,
"impl" , bs . impl )
// revert flag
// revert flag
atomic . StoreUint32 ( & bs . started , 0 )
atomic . StoreUint32 ( & bs . started , 0 )
return ErrAlreadyStopped
return ErrAlreadyStopped
}
}
bs . Logger . Info ( fmt . Sprintf ( "Starting %v" , bs . name ) , "impl" , bs . impl )
bs . Logger . Info ( fmt . Sprintf ( "Starting %v service " , bs . name ) , "impl" , bs . impl )
err := bs . impl . OnStart ( )
err := bs . impl . OnStart ( )
if err != nil {
if err != nil {
// revert flag
// revert flag
@ -144,7 +145,7 @@ func (bs *BaseService) Start() error {
}
}
return nil
return nil
}
}
bs . Logger . Debug ( fmt . Sprintf ( "Not starting %v -- already started" , bs . name ) , "impl" , bs . impl )
bs . Logger . Debug ( fmt . Sprintf ( "Not starting %v service -- already started" , bs . name ) , "impl" , bs . impl )
return ErrAlreadyStarted
return ErrAlreadyStarted
}
}
@ -158,17 +159,18 @@ func (bs *BaseService) OnStart() error { return nil }
func ( bs * BaseService ) Stop ( ) error {
func ( bs * BaseService ) Stop ( ) error {
if atomic . CompareAndSwapUint32 ( & bs . stopped , 0 , 1 ) {
if atomic . CompareAndSwapUint32 ( & bs . stopped , 0 , 1 ) {
if atomic . LoadUint32 ( & bs . started ) == 0 {
if atomic . LoadUint32 ( & bs . started ) == 0 {
bs . Logger . Error ( fmt . Sprintf ( "Not stopping %v -- have not been started yet" , bs . name ) , "impl" , bs . impl )
bs . Logger . Error ( fmt . Sprintf ( "Not stopping %v service -- has not been started yet" , bs . name ) ,
"impl" , bs . impl )
// revert flag
// revert flag
atomic . StoreUint32 ( & bs . stopped , 0 )
atomic . StoreUint32 ( & bs . stopped , 0 )
return ErrNotStarted
return ErrNotStarted
}
}
bs . Logger . Info ( fmt . Sprintf ( "Stopping %v" , bs . name ) , "impl" , bs . impl )
bs . Logger . Info ( fmt . Sprintf ( "Stopping %v service " , bs . name ) , "impl" , bs . impl )
bs . impl . OnStop ( )
bs . impl . OnStop ( )
close ( bs . quit )
close ( bs . quit )
return nil
return nil
}
}
bs . Logger . Debug ( fmt . Sprintf ( "Stopping %v (ignoring: already stopped)" , bs . name ) , "impl" , bs . impl )
bs . Logger . Debug ( fmt . Sprintf ( "Stopping %v service ( already stopped)" , bs . name ) , "impl" , bs . impl )
return ErrAlreadyStopped
return ErrAlreadyStopped
}
}
@ -181,7 +183,7 @@ func (bs *BaseService) OnStop() {}
// will be returned if the service is running.
// will be returned if the service is running.
func ( bs * BaseService ) Reset ( ) error {
func ( bs * BaseService ) Reset ( ) error {
if ! atomic . CompareAndSwapUint32 ( & bs . stopped , 1 , 0 ) {
if ! atomic . CompareAndSwapUint32 ( & bs . stopped , 1 , 0 ) {
bs . Logger . Debug ( fmt . Sprintf ( "Can't reset %v. Not stopped" , bs . name ) , "impl" , bs . impl )
bs . Logger . Debug ( fmt . Sprintf ( "Can't reset %v service . Not stopped" , bs . name ) , "impl" , bs . impl )
return fmt . Errorf ( "can't reset running %s" , bs . name )
return fmt . Errorf ( "can't reset running %s" , bs . name )
}
}