container-go/client/client-handler.go

87 lines
1.9 KiB
Go
Raw Normal View History

2018-05-02 10:59:45 +00:00
package client
import (
"fmt"
"reflect"
"sync/atomic"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
2018-05-11 05:29:28 +00:00
csc "git.loafle.net/commons/server-go/client"
2018-05-02 10:59:45 +00:00
occi "git.loafle.net/overflow/commons-go/core/interfaces"
)
type ClientHandler interface {
crc.ClientHandler
}
type ClientHandlers struct {
crc.ClientHandlers
Services []interface{}
OrderedServices []reflect.Type
validated atomic.Value
}
2018-05-11 05:29:28 +00:00
func (ch *ClientHandlers) Init(clientCtx csc.ClientCtx) error {
2018-05-02 10:59:45 +00:00
if err := ch.ClientHandlers.Init(clientCtx); nil != err {
return err
}
if err := occi.ExecServices(ch.Services, occi.ServiceMethodInit, ch.OrderedServices, false); nil != err {
return err
}
return nil
}
2018-05-11 05:29:28 +00:00
func (ch *ClientHandlers) OnStart(clientCtx csc.ClientCtx) error {
2018-05-02 10:59:45 +00:00
if err := ch.ClientHandlers.OnStart(clientCtx); nil != err {
return err
}
if err := occi.ExecServices(ch.Services, occi.ServiceMethodStart, ch.OrderedServices, false); nil != err {
return err
}
return nil
}
2018-05-11 05:29:28 +00:00
func (ch *ClientHandlers) OnStop(clientCtx csc.ClientCtx) {
2018-05-02 10:59:45 +00:00
if err := occi.ExecServices(ch.Services, occi.ServiceMethodStop, ch.OrderedServices, true); nil != err {
logging.Logger().Errorf("Container[%s]: Service stop err %v", ch.Name, err)
}
ch.ClientHandlers.OnStop(clientCtx)
}
2018-05-11 05:29:28 +00:00
func (ch *ClientHandlers) Destroy(clientCtx csc.ClientCtx) {
2018-05-02 10:59:45 +00:00
if err := occi.ExecServices(ch.Services, occi.ServiceMethodDestroy, ch.OrderedServices, true); nil != err {
logging.Logger().Errorf("Container[%s]: Service destroy err %v", ch.Name, err)
}
ch.ClientHandlers.Destroy(clientCtx)
}
func (ch *ClientHandlers) Validate() error {
if nil != ch.validated.Load() {
return nil
}
ch.validated.Store(true)
if err := ch.ClientHandlers.Validate(); nil != err {
return err
}
if nil == ch.Services {
return fmt.Errorf("Services is not valid")
}
if nil == ch.OrderedServices {
return fmt.Errorf("OrderedServices is not valid")
}
return nil
}