container-go/client/client-handler.go
crusader 00e635a7b9 ing
2018-07-01 13:27:02 +09:00

87 lines
1.9 KiB
Go

package client
import (
"fmt"
"reflect"
"sync/atomic"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
cssc "git.loafle.net/commons/server-go/socket/client"
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
}
func (ch *ClientHandlers) Init(clientCtx cssc.ClientCtx) error {
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
}
func (ch *ClientHandlers) OnStart(clientCtx cssc.ClientCtx) error {
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
}
func (ch *ClientHandlers) OnStop(clientCtx cssc.ClientCtx) {
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)
}
func (ch *ClientHandlers) Destroy(clientCtx cssc.ClientCtx) {
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
}