otto 2 years ago
parent
commit
8e095b6412
  1. 18
      Jenkinsfile
  2. 6
      Makefile
  3. 7
      README.md
  4. 2
      cmd/server/main.go
  5. 3
      cmd/server/wire.go
  6. 7
      cmd/server/wire_gen.go
  7. 26
      go.mod
  8. 50
      go.sum
  9. 228
      internal/biz/biz.go
  10. 52
      internal/biz/biz_test.go
  11. 9
      internal/util/time.go
  12. 48
      job.yaml
  13. 8
      pkg/pkg.go
  14. 42
      pkg/room/room.go

18
Jenkinsfile vendored

@ -10,17 +10,17 @@ pipeline {
environment {
// 服务名
SERVER_NAME = 'feedback'
SERVER_NAME = 'room-hot-compute-job'
// 镜像名称,对应K8S_FILE里面Deployment.containers的name
IMAGE_NAME = 'feedback'
IMAGE_NAME = 'room-hot-compute-job'
// git路径
GIT_URL = 'http://git.gz.internal.jumaiyx.cn/job/room-server-clear.git'
// 分支
GIT_BRANCH = '*/master'
// kubectl执行文件
K8S_FILE = 'feedback-dev.yaml'
K8S_FILE = 'job.yaml'
// k8s deployment名称,对应K8S_FILE里面Deployment的name
K8S_DEPLOYMENT = 'feedback-deployment'
K8S_DEPLOYMENT = 'room-hot-compute-job'
}
stages {
@ -39,13 +39,13 @@ pipeline {
}
stage('传入k8s-master') {
steps {
//sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${env.K8S_FILE}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: "${env.K8S_FILE}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '将yaml传入k8s-master'
}
}
stage('部署服务') {
steps {
//sh "ssh root@192.168.1.156 kubectl apply -f /usr/local/k8s/${env.K8S_FILE}"
sh "ssh root@192.168.1.156 kubectl apply -f /usr/local/k8s/${env.K8S_FILE}"
echo '部署服务'
//sh '''if if (( ${env.BUILD_ID} == 1 ))
//then
@ -54,11 +54,5 @@ pipeline {
//fi'''
}
}
stage('更新image') {
steps {
sh "ssh root@192.168.1.156 kubectl set image Deployment/${env.K8S_DEPLOYMENT} ${env.IMAGE_NAME}=registry.internal.jumaiyx.cn/jm/${env.SERVER_NAME}:${env.BUILD_ID} -n dev"
echo '部署服务'
}
}
}
}

6
Makefile

@ -26,11 +26,7 @@ run:
up:
go mod tidy
go get -u git.gz.internal.jumaiyx.cn/jm/jmproto
go get -u git.gz.internal.jumaiyx.cn/pkg/k8s-client
go get -u git.gz.internal.jumaiyx.cn/pkg/log
go get -u git.gz.internal.jumaiyx.cn/pkg/config
go get -u git.gz.internal.jumaiyx.cn/pkg/id-encode
go get git.gz.internal.jumaiyx.cn/jm/jmproto
.PHONY: generate
# generate

7
README.md

@ -2,9 +2,10 @@
```sh
docker login -u devops-internal -p yVZUucBSMTJ8HDct https://registry.internal.jumaiyx.cn
docker build -t registry.internal.jumaiyx.cn/jm/message:latest .
docker tag registry.internal.jumaiyx.cn/jm/message:latest registry.internal.jumaiyx.cn/jm/message:v1.0.0
docker push registry.internal.jumaiyx.cn/jm/message:v1.0.0
docker build -t registry.internal.jumaiyx.cn/job/room-server-clear:latest .
docker tag registry.internal.jumaiyx.cn/job/room-server-clear:latest registry.internal.jumaiyx.cn/job/room-server-clear:v1.0.0
docker push registry.internal.jumaiyx.cn/job/room-server-clear:v1.0.0
```

2
cmd/server/main.go

@ -26,7 +26,7 @@ func main() {
}
bc := c.Get()
l := log.NewLogger(c.Get().Log)
l := log.DefaultLogger()
biz, cleanup, err := wireApp(bc.Server, l)
if err != nil {
panic(err)

3
cmd/server/wire.go

@ -8,12 +8,11 @@ package main
import (
"git.gz.internal.jumaiyx.cn/jm/jmproto/conf"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/internal/biz"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/pkg"
"git.gz.internal.jumaiyx.cn/pkg/log"
"github.com/google/wire"
)
// wireApp init kratos application.
func wireApp(*conf.Server, log.Logger) (*biz.Biz, func(), error) {
panic(wire.Build(biz.ProviderBizSet, pkg.ProviderPkgSet))
panic(wire.Build(biz.ProviderBizSet))
}

7
cmd/server/wire_gen.go

@ -9,7 +9,6 @@ package main
import (
"git.gz.internal.jumaiyx.cn/jm/jmproto/conf"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/internal/biz"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/pkg/room"
"git.gz.internal.jumaiyx.cn/pkg/log"
)
@ -17,11 +16,7 @@ import (
// wireApp init kratos application.
func wireApp(server *conf.Server, logger log.Logger) (*biz.Biz, func(), error) {
roomService, err := room.NewRoomService(logger)
if err != nil {
return nil, nil, err
}
bizBiz := biz.NewBiz(logger, roomService)
bizBiz := biz.NewBiz(logger)
return bizBiz, func() {
}, nil
}

26
go.mod

@ -3,19 +3,17 @@ module git.gz.internal.jumaiyx.cn/job/room-server-clear
go 1.18
require (
git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20221111065023-9245cd697853
git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20230217083354-fc40467648e7
git.gz.internal.jumaiyx.cn/pkg/config v0.0.0-20221014090146-8fd721834886
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221111070703-c8cf47f0b4d9
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20230213091252-ba02ce8af13a
git.gz.internal.jumaiyx.cn/pkg/kubediscovery v0.0.0-20221104101240-01cad3403ec0
git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20221029084114-f67ca5716ca1
git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20230213074339-8fa1e676b686
github.com/google/wire v0.5.0
k8s.io/api v0.25.4
k8s.io/apimachinery v0.25.4
)
require (
git.gz.internal.jumaiyx.cn/pkg/id-encode v0.0.0-20221018014421-fe47b648265b // indirect
git.gz.internal.jumaiyx.cn/pkg/kafka v0.0.0-20221010085437-f3a4ed57181c // indirect
git.gz.internal.jumaiyx.cn/pkg/kafka v0.0.0-20221118023940-844d051e6449 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
@ -41,24 +39,24 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/segmentio/kafka-go v0.4.38 // indirect
github.com/speps/go-hashids/v2 v2.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xdg/scram v1.0.5 // indirect
github.com/xdg/stringprep v1.0.3 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/oauth2 v0.2.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221109142239-94d6d90a7d66 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.25.4 // indirect
k8s.io/client-go v0.25.4 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70 // indirect

50
go.sum

@ -1,21 +1,17 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20221111065023-9245cd697853 h1:Dz6fcvEPxOZMDqFQBRKR+CLj1uzwfvkrxMqYSytFNGk=
git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20221111065023-9245cd697853/go.mod h1:RuwVCwjm2L1MfZAePy5exPb/0OKHglXM+Q5lXrcdGxs=
git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20230217083354-fc40467648e7 h1:s6K3T28drWlvwK+xn4jzs3r9RzXeQvCCCEUdWktk3zU=
git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20230217083354-fc40467648e7/go.mod h1:U6I+UIgNboy04gVNv6/5azHTSjAJJICfu0vBxTgv3PE=
git.gz.internal.jumaiyx.cn/pkg/config v0.0.0-20221014090146-8fd721834886 h1:LP4psj3ygJEKL6748ZQPNwYnYPmbo8dDwtwU08coRiw=
git.gz.internal.jumaiyx.cn/pkg/config v0.0.0-20221014090146-8fd721834886/go.mod h1:yEnzjNrnPNIr0Zq926QiJrbyReS+VZqX5bmkYhnSV7I=
git.gz.internal.jumaiyx.cn/pkg/id-encode v0.0.0-20221018014421-fe47b648265b h1:Bb+j4SrJMb00+QKQO1FcCDDOxzTnoBZCi2n0DrT39lE=
git.gz.internal.jumaiyx.cn/pkg/id-encode v0.0.0-20221018014421-fe47b648265b/go.mod h1:+1j9TQXFahHrK3TYJjXLuuDNCqKRscAWR2fD1ZhZE0c=
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221111064954-c309d611aba4 h1:+2z24g9wgM+ntRUpRKGIEmFkJo8Mkjflk2EzgDxS6ec=
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221111064954-c309d611aba4/go.mod h1:PjyxKfTWowQjJcim7h5DB+9pE7gI0U7BYx2z200hVyc=
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221111070703-c8cf47f0b4d9 h1:pVl/ra/vqTHTPEnp2MGxMZLi8Txp0u3GHK+2EHU8oYQ=
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221111070703-c8cf47f0b4d9/go.mod h1:PjyxKfTWowQjJcim7h5DB+9pE7gI0U7BYx2z200hVyc=
git.gz.internal.jumaiyx.cn/pkg/kafka v0.0.0-20221010085437-f3a4ed57181c h1:C/1lKDrxi2xM2BPGG843kEcmZivhOdEr7Eg86bDIBVY=
git.gz.internal.jumaiyx.cn/pkg/kafka v0.0.0-20221010085437-f3a4ed57181c/go.mod h1:TujX5RTIJ6vz8QYG1R6JwssH5DHGrAVpdGBaWG+uVqY=
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20230213091252-ba02ce8af13a h1:LE6nlpvHgKnHXuSsPN7WHLjVQxR82TtSJ4HkKo2GJPA=
git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20230213091252-ba02ce8af13a/go.mod h1:YLXO2BSKUZ0/RzVdbsBKAeTHDI75/NMCmt8SsB9eeCU=
git.gz.internal.jumaiyx.cn/pkg/kafka v0.0.0-20221118023940-844d051e6449 h1:kIzpUIKxa9yLZUfgYK+z3uBAzF0BCkauNxhlUEL5n+c=
git.gz.internal.jumaiyx.cn/pkg/kafka v0.0.0-20221118023940-844d051e6449/go.mod h1:rspqWOZacckvYwl1M6tikwkdsOlorUmt3CylJ3cGQV8=
git.gz.internal.jumaiyx.cn/pkg/kubediscovery v0.0.0-20221104101240-01cad3403ec0 h1:G3Hb4Bz9Ku3hSkIIZZu33iN+CNqPEnjVbsOa+MVnat4=
git.gz.internal.jumaiyx.cn/pkg/kubediscovery v0.0.0-20221104101240-01cad3403ec0/go.mod h1:oc2Nm1tO4n09zn9u0KGlQzWzthO5WZ3PPq0CUMCYByg=
git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20221029084114-f67ca5716ca1 h1:IWra+Xfxk9ay45YZL+Qlb/LiplX+RRwhdJ1Gwa9mxxw=
git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20221029084114-f67ca5716ca1/go.mod h1:1sRiFBXx4Mc4/HXl2SqnuP3iqUtu5f6XOC+uFaBXltA=
git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20230213074339-8fa1e676b686 h1:aJePiLxITBenCZAP/l6XXAEH7H5WIXQgOO/nmc0WvZU=
git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20230213074339-8fa1e676b686/go.mod h1:b+W/opUzkHTSFb73EWaqBHWswRp69u1pWac6JWdc9SA=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
@ -156,8 +152,6 @@ github.com/segmentio/kafka-go v0.4.38 h1:iQdOBbUSdfuYlFpvjuALgj7N6DrdPA0HfB4AhRE
github.com/segmentio/kafka-go v0.4.38/go.mod h1:ikyuGon/60MN/vXFgykf7Zm8P5Be49gJU6vezwjnnhU=
github.com/shirou/gopsutil/v3 v3.21.8/go.mod h1:YWp/H8Qs5fVmf17v7JNZzA0mPJ+mS2e9JdiUF9LlKzQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/speps/go-hashids/v2 v2.0.1 h1:ViWOEqWES/pdOSq+C1SLVa8/Tnsd52XC34RY7lt7m4g=
github.com/speps/go-hashids/v2 v2.0.1/go.mod h1:47LKunwvDZki/uRVD6NImtyk712yFzIs3UF3KlHohGw=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
@ -188,8 +182,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
@ -211,8 +205,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220706163947-c90051bbdb60/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.2.0 h1:GtQkldQ9m7yvzCL1V+LrYow3Khe0eJH0w7RbX/VbaIU=
@ -241,20 +235,20 @@ golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.2.0 h1:52I/1L54xyEQAYdtcSuxtiT84KGYTBGXwayxmIpNJhE=
golang.org/x/time v0.2.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@ -280,8 +274,8 @@ google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfG
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20221109142239-94d6d90a7d66 h1:wx7sJ5GRBQLRcslTNcrTklsHhHevQvxgztW18txbbZM=
google.golang.org/genproto v0.0.0-20221109142239-94d6d90a7d66/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37 h1:jmIfw8+gSvXcZSgaFAGyInDXeWzUhvYH57G/5GKMn70=
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
@ -291,8 +285,8 @@ google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.46.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc v1.46.2/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY=
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U=
google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=

228
internal/biz/biz.go

@ -2,19 +2,26 @@ package biz
import (
"context"
"fmt"
v1 "git.gz.internal.jumaiyx.cn/jm/jmproto/room/v1"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/internal/util"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/pkg/room"
k8s_client "git.gz.internal.jumaiyx.cn/pkg/k8s-client"
"git.gz.internal.jumaiyx.cn/pkg/k8s-client/deployment"
"git.gz.internal.jumaiyx.cn/pkg/k8s-client/service"
"git.gz.internal.jumaiyx.cn/pkg/log"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/google/wire"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"sync"
"time"
)
const (
limit = 100
outTime = 10
)
var wg sync.WaitGroup
// ProviderBizSet is biz providers.
@ -24,166 +31,213 @@ type PodData struct {
RoomId int64
BranchId int64
Name string
StartTime string
}
type Biz struct {
log log.Logger
room *room.RoomService
depChan chan PodData
serChan chan PodData
depChan chan []PodData
serChan chan []PodData
deploy deployment.Deployment
ser service.Service
}
func NewBiz(logger log.Logger, roomServer *room.RoomService) *Biz {
func NewBiz(logger log.Logger) *Biz {
return &Biz{
log: logger,
depChan: make(chan PodData, 1000),
serChan: make(chan PodData, 1000),
room: roomServer,
depChan: make(chan []PodData, 1),
serChan: make(chan []PodData, 1),
}
}
func (biz *Biz) Check(ctx context.Context) {
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
}
// Check
// 定期删除unity的空置容器
func (biz *Biz) Check(ctx context.Context) {
deploy, err := k8s_client.NewDeployment(k8s_client.UnityNamespace, biz.log)
if err != nil {
biz.log.Errorf("Service connect failed:%v", err)
return
}
ser, err := k8s_client.NewService(k8s_client.UnityNamespace, biz.log)
biz.deploy = deploy
serice, err := k8s_client.NewService(k8s_client.UnityNamespace, biz.log)
if err != nil {
biz.log.Errorf("Service connect failed:%v", err)
return
}
biz.deploy = deploy
biz.ser = ser
biz.ser = serice
wg.Add(4)
go func() {
defer wg.Done()
biz.deployment(ctx)
return
}()
wg.Add(3)
go func() {
defer wg.Done()
biz.Deployment(ctx)
biz.deploymentDel(ctx)
return
}()
go func() {
defer wg.Done()
biz.Service(ctx)
biz.service(ctx)
}()
go func() {
defer wg.Done()
biz.Room(ctx)
biz.serviceDel(ctx)
}()
wg.Wait()
biz.log.Info("Task done")
}
func (biz *Biz) Deployment(ctx context.Context) {
deployments, err := biz.deploy.List(ctx)
// 获取unity的deployment
func (biz *Biz) deployment(ctx context.Context) {
var cont string
for {
rsp, err := biz.deploy.List(ctx, metav1.ListOptions{
Limit: limit,
Continue: cont,
})
if err != nil {
biz.log.Errorf("Get pod list failed:%v", err)
biz.log.Errorf("get deployment fail:%v", err)
return
}
for _, v := range deployments {
names := strings.Split(v.Name, "-")
pn := PodData{
Name: v.Name,
}
if len(names) == 5 {
pn.RoomId = util.StringTarnsInt64(names[3])
pn.BranchId = util.StringTarnsInt64(names[4])
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],
})
}
select {
case biz.depChan <- pn:
case <-time.After(30 * time.Second): //最多等到30秒,避免死锁
biz.log.Info("deployment quit")
return
if len(podDatas) > 0 {
biz.depChan <- podDatas
}
cont = rsp.Continue
if cont == "" {
break
}
}
return
}
func (biz *Biz) Service(ctx context.Context) {
services, err := biz.ser.List(ctx)
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 pod list failed:%v", err)
return
biz.log.Errorf("get room list fail:%v", err)
}
for _, v := range services {
names := strings.Split(v.Name, "-")
pn := PodData{
Name: v.Name,
for _, pod := range pods {
if _, ok := roomMap[pod.RoomId]; !ok {
fmt.Println(pod.Name)
biz.deploy.Delete(ctx, pod.Name)
}
if len(names) == 5 {
pn.RoomId = util.StringTarnsInt64(names[3])
pn.BranchId = util.StringTarnsInt64(names[4])
}
select {
case biz.serChan <- pn:
case <-time.After(30 * time.Second): //最多等到30秒,避免死锁
biz.log.Info("service quit")
case <-time.After(time.Second * outTime):
return
}
}
}
func (biz *Biz) Room(ctx context.Context) {
// 获取unity的service
func (biz *Biz) service(ctx context.Context) {
var cont string
for {
select {
case dep := <-biz.depChan:
err := biz.del(ctx, dep, func(name string) error {
biz.log.Infof("deployment name:%s", name)
return biz.deploy.Delete(ctx, name)
rsp, err := biz.ser.List(ctx, metav1.ListOptions{
Limit: limit,
Continue: cont,
})
if err != nil {
biz.log.Errorf("Delete deployment failed:%v", err)
biz.log.Errorf("get deployment fail:%v", err)
return
}
continue
case ser := <-biz.serChan:
err := biz.del(ctx, ser, func(name string) error {
biz.log.Infof("service name:%s", name)
return biz.ser.Delete(ctx, name)
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],
})
if err != nil {
biz.log.Errorf("Delete deployment failed:%v", err)
}
continue
case <-time.After(30 * time.Second): //最多等到30秒,避免死锁
biz.log.Info("room quit")
return
biz.serChan <- podDatas
cont = rsp.Continue
if cont == "" {
break
}
}
return
}
func (biz *Biz) del(ctx context.Context, p PodData, del func(string) error) error {
// 无房间直接删除
if p.RoomId == 0 {
return del(p.Name)
func (biz *Biz) serviceDel(ctx context.Context) {
for {
select {
case pods := <-biz.serChan:
var ids []int64
for _, pod := range pods {
ids = append(ids, pod.RoomId)
}
r, err := biz.room.GetRoom(ctx, &v1.GetRoomRequest{
RoomId: p.RoomId,
})
roomMap, err := biz.roomList(ctx, ids)
if err != nil {
biz.log.Errorf("Get room failed:%v", err)
return err
biz.log.Errorf("get room list fail:%v", err)
}
for _, pod := range pods {
if _, ok := roomMap[pod.RoomId]; !ok {
biz.ser.Delete(ctx, pod.Name)
}
}
case <-time.After(time.Second * outTime):
return
}
}
// 删除不存在的房间
if r.RoomId == 0 || r.Status != 1 {
return del(p.Name)
}
// 删除不存在的支线
var flag bool
for _, branch := range r.Branches {
if branch.BranchId == p.BranchId {
flag = true
// 查看房间信息
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
}
rsp, err := client.GetRoomList(ctx, &v1.GetRoomListRequest{
Ids: ids,
Status: int32(v1.RoomStatus_RoomStatusUp),
})
if err != nil {
return nil, err
}
if !flag {
return del(p.Name)
items := make(map[int64]int32)
for _, item := range rsp.Data {
items[item.RoomId] = 1
}
return nil
return items, nil
}

52
internal/biz/biz_test.go

@ -2,40 +2,48 @@ package biz
import (
"context"
"git.gz.internal.jumaiyx.cn/job/room-server-clear/pkg/room"
k8s_client "git.gz.internal.jumaiyx.cn/pkg/k8s-client"
"git.gz.internal.jumaiyx.cn/pkg/log"
"testing"
"time"
)
func TestNewBiz(t *testing.T) {
l := log.DefaultLogger()
roomServer, err := room.NewRoomService(l)
if err != nil {
t.Fatal(err)
}
deploy, err := k8s_client.NewDeployment(k8s_client.UnityNamespace, l)
if err != nil {
t.Fatal(err)
time.Sleep(time.Second * 30)
}
ser, err := k8s_client.NewService(k8s_client.UnityNamespace, l)
if err != nil {
t.Fatal(err)
func TestCheck(t *testing.T) {
l := log.DefaultLogger()
build := NewBiz(l)
build.Check(context.TODO())
}
n := NewBiz(l, roomServer)
n.room = roomServer
n.deploy = deploy
n.ser = ser
ctx := context.TODO()
func TestNewBiz_Deployment(t *testing.T) {
l := log.DefaultLogger()
build := NewBiz(l)
wg.Add(2)
go func() {
defer wg.Done()
build.deployment(context.TODO())
}()
go func() {
n.Deployment(ctx)
defer wg.Done()
build.deploymentDel(context.TODO())
}()
wg.Wait()
}
func TestNewBiz_Service(t *testing.T) {
l := log.DefaultLogger()
build := NewBiz(l)
wg.Add(2)
go func() {
n.Service(ctx)
defer wg.Done()
build.service(context.TODO())
}()
go func() {
n.Room(ctx)
defer wg.Done()
build.serviceDel(context.TODO())
}()
time.Sleep(time.Second * 30)
wg.Wait()
}

9
internal/util/time.go

@ -11,3 +11,12 @@ func GetNowYmdHms() string {
func GetNowUnix() int64 {
return time.Now().Unix()
}
func ParseInLocation(str string) time.Time {
//str := "2020-10-12 14:19:53"
tmp := "2006-01-02 15:04:05"
t, _ := time.ParseInLocation(tmp, str, time.Local)
return t
}

48
job.yaml

@ -1,44 +1,20 @@
apiVersion: apps/v1
kind: Deployment
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: feedback-deployment
name: room-hot-compute-job
namespace: dev
labels:
app: feedback
spec:
replicas: 2 # 节点资源
selector:
matchLabels:
app: feedback
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
metadata: # 资源的元数据/属性
labels: # 设定资源的标签
app: feedback
spec:
containers:
- image: registry.internal.jumaiyx.cn/jm/feedback:1 # 自动化版本往后配置
name: feedback
imagePullPolicy: Always
ports:
- name: grpc
containerPort: 9000
protocol: TCP
- name: room-hot-compute-job
image: registry.internal.jumaiyx.cn/job/room-server-clear:1
imagePullPolicy: IfNotPresent
command:
- ./server
restartPolicy: OnFailure
imagePullSecrets:
- name: registry-harbor
---
apiVersion: v1
kind: Service
metadata:
name: feedback-service # 改名字对应服务发现serviceName
namespace: dev
labels:
name: feedback
spec:
type: ClusterIP
ports:
- name: grpc
port: 9000
targetPort: grpc
protocol: TCP
selector:
app: feedback

8
pkg/pkg.go

@ -1,8 +0,0 @@
package pkg
import (
"git.gz.internal.jumaiyx.cn/job/room-server-clear/pkg/room"
"github.com/google/wire"
)
var ProviderPkgSet = wire.NewSet(room.NewRoomService)

42
pkg/room/room.go

@ -1,42 +0,0 @@
package room
import (
"context"
v1 "git.gz.internal.jumaiyx.cn/jm/jmproto/room/v1"
k8s_client "git.gz.internal.jumaiyx.cn/pkg/k8s-client"
"git.gz.internal.jumaiyx.cn/pkg/kubediscovery"
"git.gz.internal.jumaiyx.cn/pkg/log"
)
type RoomService struct {
client v1.RoomClient
log log.Logger
}
func NewRoomService(log log.Logger) (*RoomService, error) {
discovery, err := kubediscovery.Discovery(context.TODO(), log, "room-service", kubediscovery.Namespace(k8s_client.DevNamespace))
if err != nil {
log.Errorf("Connect room service failed:%v", err)
return nil, err
}
client := v1.NewRoomClient(discovery)
return &RoomService{client: client, log: log}, nil
}
func (ser *RoomService) GetList(ctx context.Context, req *v1.ListRoomRequest) (*v1.ListRoomReply, error) {
room, err := ser.client.ListRoom(ctx, req)
if err != nil {
ser.log.Errorf("Get room list failed:%v", err)
return nil, err
}
return room, nil
}
func (ser *RoomService) GetRoom(ctx context.Context, req *v1.GetRoomRequest) (*v1.GetRoomReply, error) {
room, err := ser.client.GetRoom(ctx, req)
if err != nil {
ser.log.Errorf("Get room list failed:%v", err)
return nil, err
}
return room, nil
}
Loading…
Cancel
Save