unity容器监听
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
5.0 KiB

2 years ago
package biz
import (
"context"
2 years ago
"fmt"
2 years ago
v1 "git.gz.internal.jumaiyx.cn/jm/jmproto/room/v1"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/internal/util"
k8s_client "git.gz.internal.jumaiyx.cn/pkg/k8s-client"
2 years ago
"git.gz.internal.jumaiyx.cn/pkg/k8s-client/deployment"
"git.gz.internal.jumaiyx.cn/pkg/k8s-client/service"
2 years ago
"git.gz.internal.jumaiyx.cn/pkg/log"
2 years ago
"github.com/go-kratos/kratos/v2/transport/grpc"
2 years ago
"github.com/google/wire"
2 years ago
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2 years ago
"strings"
2 years ago
"sync"
2 years ago
"time"
)
2 years ago
const (
limit = 100
outTime = 10
)
2 years ago
var wg sync.WaitGroup
2 years ago
// ProviderBizSet is biz providers.
var ProviderBizSet = wire.NewSet(NewBiz)
2 years ago
type PodData struct {
2 years ago
RoomId int64
BranchId int64
Name string
StartTime string
2 years ago
}
2 years ago
type Biz struct {
log log.Logger
2 years ago
depChan chan []PodData
serChan chan []PodData
2 years ago
deploy deployment.Deployment
ser service.Service
2 years ago
}
2 years ago
func NewBiz(logger log.Logger) *Biz {
2 years ago
return &Biz{
log: logger,
2 years ago
depChan: make(chan []PodData, 1),
serChan: make(chan []PodData, 1),
2 years ago
}
}
2 years ago
func (biz *Biz) roomServerClient(ctx context.Context) (v1.RoomClient, error) {
//discovery, err := kubediscovery.Discovery(ctx, biz.log, base.RoomServiceName, kubediscovery.Namespace(k8s_client.DevNamespace))
discovery, err := grpc.DialInsecure(ctx, grpc.WithEndpoint("127.0.0.1:9000"), grpc.WithTimeout(10*time.Second))
if err != nil {
biz.log.Errorf("room client failed:%v", err)
return nil, err
}
return v1.NewRoomClient(discovery), nil
}
2 years ago
2 years ago
// Check
// 定期删除unity的空置容器
func (biz *Biz) Check(ctx context.Context) {
2 years ago
deploy, err := k8s_client.NewDeployment(k8s_client.UnityNamespace, biz.log)
if err != nil {
biz.log.Errorf("Service connect failed:%v", err)
return
}
2 years ago
biz.deploy = deploy
serice, err := k8s_client.NewService(k8s_client.UnityNamespace, biz.log)
2 years ago
if err != nil {
biz.log.Errorf("Service connect failed:%v", err)
return
}
2 years ago
biz.ser = serice
wg.Add(4)
go func() {
defer wg.Done()
biz.deployment(ctx)
return
}()
2 years ago
2 years ago
go func() {
defer wg.Done()
2 years ago
biz.deploymentDel(ctx)
return
2 years ago
}()
2 years ago
2 years ago
go func() {
defer wg.Done()
2 years ago
biz.service(ctx)
2 years ago
}()
go func() {
defer wg.Done()
2 years ago
biz.serviceDel(ctx)
2 years ago
}()
wg.Wait()
2 years ago
}
2 years ago
// 获取unity的deployment
func (biz *Biz) deployment(ctx context.Context) {
2 years ago
2 years ago
var cont string
for {
rsp, err := biz.deploy.List(ctx, metav1.ListOptions{
Limit: limit,
Continue: cont,
})
if err != nil {
biz.log.Errorf("get deployment fail:%v", err)
return
2 years ago
}
2 years ago
var podDatas []PodData
for _, item := range rsp.Items {
names := strings.Split(item.Name, "-")
podDatas = append(podDatas, PodData{
Name: item.Name,
RoomId: util.StringTarnsInt64(names[3]),
BranchId: util.StringTarnsInt64(names[4]),
StartTime: names[5],
})
2 years ago
}
2 years ago
if len(podDatas) > 0 {
biz.depChan <- podDatas
}
cont = rsp.Continue
if cont == "" {
break
}
2 years ago
}
2 years ago
return
2 years ago
}
2 years ago
func (biz *Biz) deploymentDel(ctx context.Context) {
for {
select {
case pods := <-biz.depChan:
var ids []int64
for _, pod := range pods {
ids = append(ids, pod.RoomId)
}
roomMap, err := biz.roomList(ctx, ids)
if err != nil {
biz.log.Errorf("get room list fail:%v", err)
}
for _, pod := range pods {
if _, ok := roomMap[pod.RoomId]; !ok {
fmt.Println(pod.Name)
biz.deploy.Delete(ctx, pod.Name)
}
}
case <-time.After(time.Second * outTime):
return
}
2 years ago
}
2 years ago
}
2 years ago
2 years ago
// 获取unity的service
func (biz *Biz) service(ctx context.Context) {
var cont string
for {
rsp, err := biz.ser.List(ctx, metav1.ListOptions{
Limit: limit,
Continue: cont,
})
if err != nil {
biz.log.Errorf("get deployment fail:%v", err)
return
2 years ago
}
2 years ago
var podDatas []PodData
for _, item := range rsp.Items {
names := strings.Split(item.Name, "-")
podDatas = append(podDatas, PodData{
Name: item.Name,
RoomId: util.StringTarnsInt64(names[3]),
BranchId: util.StringTarnsInt64(names[4]),
StartTime: names[5],
})
2 years ago
}
2 years ago
biz.serChan <- podDatas
cont = rsp.Continue
if cont == "" {
break
2 years ago
}
}
2 years ago
return
2 years ago
}
2 years ago
func (biz *Biz) serviceDel(ctx context.Context) {
2 years ago
for {
select {
2 years ago
case pods := <-biz.serChan:
var ids []int64
for _, pod := range pods {
ids = append(ids, pod.RoomId)
2 years ago
}
2 years ago
roomMap, err := biz.roomList(ctx, ids)
2 years ago
if err != nil {
2 years ago
biz.log.Errorf("get room list fail:%v", err)
2 years ago
}
2 years ago
for _, pod := range pods {
if _, ok := roomMap[pod.RoomId]; !ok {
biz.ser.Delete(ctx, pod.Name)
}
}
case <-time.After(time.Second * outTime):
2 years ago
return
}
}
}
2 years ago
2 years ago
// 查看房间信息
func (biz *Biz) roomList(ctx context.Context, ids []int64) (map[int64]int32, error) {
client, err := biz.roomServerClient(ctx)
if err != nil {
biz.log.Errorf("room server client fail:%v", err)
return nil, err
2 years ago
}
2 years ago
rsp, err := client.GetRoomList(ctx, &v1.GetRoomListRequest{
Ids: ids,
Status: int32(v1.RoomStatus_RoomStatusUp),
2 years ago
})
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
items := make(map[int64]int32)
for _, item := range rsp.Data {
items[item.RoomId] = 1
2 years ago
}
2 years ago
return items, nil
2 years ago
}