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-07-01 04:27:02 +00:00
cssc "git.loafle.net/commons/server-go/socket/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-07-01 04:27:02 +00:00
func (ch *ClientHandlers) Init(clientCtx cssc.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-07-01 04:27:02 +00:00
func (ch *ClientHandlers) OnStart(clientCtx cssc.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-07-01 04:27:02 +00:00
func (ch *ClientHandlers) OnStop(clientCtx cssc.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-07-01 04:27:02 +00:00
func (ch *ClientHandlers) Destroy(clientCtx cssc.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
}