Esempio n. 1
0
func (manager *Manager) registerManagerResource(grpcAddr string) error {
	node, err := scale.GetOwnNodeName()
	if err != nil {
		return err
	}
	ttl := 30 * time.Second
	resource := "swarm://" + node
	res, success, err := scale.RegisterResourceCustom(
		ManagerService, resource, grpcAddr, node, ttl)
	if err != nil {
		return err
	}
	if !success {
		// Leftover from a previous life. Clean up and try again.
		err := scale.ExistingResource(ManagerService, resource, "").Deregister()
		if err != nil {
			return err
		}
		return manager.registerManagerResource(grpcAddr)
	}
	manager.resource = res
	manager.sessionSKA = scale.NewSessionSelfKeepAlive(
		res.GetSessionID(), ttl/2)
	return nil
}
Esempio n. 2
0
// NewLeverResource returns a new instance of LeverResource.
func NewLeverResource(
	leverEnv, leverService, instanceID, leverResource, levResResourceID,
	levResSessionID, instanceAddr string,
	conns *scale.GRPCPool, onCloseFun OnCloseFun) (*LeverResource, error) {
	expiryDur := ResourceExpiryTimeFlag.Get()
	logger := leverutil.GetLogger(PackageName, "leverresource").WithFields(
		"leverEnv", leverEnv,
		"leverService", leverService,
		"leverInstanceID", instanceID,
		"leverResource", leverResource,
	)
	resource := &LeverResource{
		leverEnv:      leverEnv,
		leverService:  leverService,
		leverResource: leverResource,
		levResResource: scale.ExistingResource(
			leverutil.ServiceFlag.Get(), levResResourceID, levResSessionID),
		instanceAddr: instanceAddr,
		conns:        conns,
		sessionKAB: scale.NewSessionKeepAliveBuffer(
			levResSessionID, expiryDur),
		expiryDur:  expiryDur,
		onCloseFun: onCloseFun,
		logger:     logger,
	}
	resource.expiryTimer = time.AfterFunc(expiryDur, resource.onExpired)
	go resource.monitorResourceLock(levResResourceID)
	resource.construct()
	return resource, nil
}
Esempio n. 3
0
// NewLeverInstance returns a new instance of LeverInstance.
func NewLeverInstance(
	info *InstanceInfo, instanceAddr string, proxyInAddr string,
	conns *scale.GRPCPool, docker *dockerapi.Client,
	onCloseFun OnCloseFun) *LeverInstance {
	expiryDur := InstanceExpiryTimeFlag.Get()
	logger := leverutil.GetLogger(PackageName, "leverinstance").WithFields(
		"leverEnv", info.Environment,
		"leverService", info.Service,
		"leverInstanceID", info.InstanceID,
	)
	instance := &LeverInstance{
		leverEnv:       info.Environment,
		instanceID:     info.InstanceID,
		leverService:   info.Service,
		containerID:    info.ContainerID,
		servingID:      info.ServingID,
		instanceAddr:   instanceAddr,
		conns:          conns,
		docker:         docker,
		expiryDur:      expiryDur,
		onCloseFun:     onCloseFun,
		logger:         logger,
		leverResources: make(map[string]*LeverResource),
	}
	instance.expiryTimer = time.AfterFunc(expiryDur, instance.onExpired)
	instance.registerAsService(proxyInAddr)
	go instance.monitorContainer()
	if info.LevInstResourceID != "" {
		// This instance has a resource + session associated with it. Manage
		// its lifecycle.
		instance.levInstResource = scale.ExistingResource(
			leverutil.ServiceFlag.Get(), info.LevInstResourceID,
			info.LevInstSessionID)
		instance.sessionKAB = scale.NewSessionKeepAliveBuffer(
			info.LevInstSessionID, expiryDur)
		go instance.monitorInstanceLock(info.LevInstResourceID)
	}
	return instance
}