diff --git a/Dockerfile b/Dockerfile index 8853608..bbf967b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,5 +25,5 @@ ENV TZ Asia/Shanghai WORKDIR /app COPY --from=builder /src/bin/server /app/server COPY --from=builder /src/configs /app/configs -EXPOSE 9000 +#EXPOSE 9000 ENTRYPOINT ["./server"] \ No newline at end of file diff --git a/README.md b/README.md index 1863d57..fdb6b05 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## 房间服务 +## 房间清除多余容器任务 ```sh docker login -u devops-internal -p yVZUucBSMTJ8HDct https://registry.internal.jumaiyx.cn diff --git a/cmd/server/main.go b/cmd/server/main.go index 0585f42..bd68080 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "flag" "fmt" "git.gz.internal.jumaiyx.cn/pkg/config" @@ -26,12 +27,12 @@ func main() { bc := c.Get() l := log.NewLogger(c.Get().Log) - biz, cleanup, err := wireApp(bc.Server, l) if err != nil { panic(err) } defer cleanup() - fmt.Println(biz) - biz.Check() + ctx := context.TODO() + l.WithContext(ctx) + biz.Check(ctx) } diff --git a/go.mod b/go.mod index 645d2c5..8b4d4af 100644 --- a/go.mod +++ b/go.mod @@ -3,20 +3,23 @@ module git.gz.internal.jumaiyx.cn/job/room-server-clear go 1.18 require ( - git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20221110074911-4914133827fb + git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20221111065023-9245cd697853 git.gz.internal.jumaiyx.cn/pkg/config v0.0.0-20221014090146-8fd721834886 - git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221013012151-3198603d6fe1 + git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221111070703-c8cf47f0b4d9 git.gz.internal.jumaiyx.cn/pkg/kubediscovery v0.0.0-20221104101240-01cad3403ec0 git.gz.internal.jumaiyx.cn/pkg/log v0.0.0-20221029084114-f67ca5716ca1 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 github.com/davecgh/go-spew v1.1.1 // indirect - github.com/emicklei/go-restful/v3 v3.9.0 // indirect - github.com/fsnotify/fsnotify v1.5.4 // indirect - github.com/go-kratos/kratos/v2 v2.5.2 // indirect + github.com/emicklei/go-restful/v3 v3.10.0 // indirect + github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/go-kratos/kratos/v2 v2.5.3 // indirect github.com/go-logr/logr v1.2.3 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.20.0 // indirect @@ -38,6 +41,7 @@ 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 @@ -55,11 +59,9 @@ require ( 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.3 // indirect - k8s.io/apimachinery v0.25.3 // indirect - k8s.io/client-go v0.25.3 // indirect + k8s.io/client-go v0.25.4 // indirect k8s.io/klog/v2 v2.80.1 // indirect - k8s.io/kube-openapi v0.0.0-20221106113015-f73e7dbcfe29 // indirect + k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70 // indirect k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect diff --git a/go.sum b/go.sum index 5d5971c..1b07eb3 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,15 @@ 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-20221110074911-4914133827fb h1:LNMUgFFA8Zsv7v8lYxWL6MU7UH9S0D+UvOIEhVrt3vU= -git.gz.internal.jumaiyx.cn/jm/jmproto v0.0.0-20221110074911-4914133827fb/go.mod h1:RuwVCwjm2L1MfZAePy5exPb/0OKHglXM+Q5lXrcdGxs= +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/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/k8s-client v0.0.0-20221013012151-3198603d6fe1 h1:v64Q2KVTfqKunZ59mAQ88nNn8LR5faUIGkjGcdR6fSw= -git.gz.internal.jumaiyx.cn/pkg/k8s-client v0.0.0-20221013012151-3198603d6fe1/go.mod h1:9F4BK7ZDg00BHB3rQdD+rkC/eGjMp+5NNrcZs6U1D4I= +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/kubediscovery v0.0.0-20221104101240-01cad3403ec0 h1:G3Hb4Bz9Ku3hSkIIZZu33iN+CNqPEnjVbsOa+MVnat4= @@ -32,8 +36,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= -github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= -github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/emicklei/go-restful/v3 v3.10.0 h1:X4gma4HM7hFm6WMeAsTfqA0GOfdNoCzBIkHGoRLGXuM= +github.com/emicklei/go-restful/v3 v3.10.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -42,12 +46,13 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0= -github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= +github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= +github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kratos/aegis v0.1.2/go.mod h1:jYeSQ3Gesba478zEnujOiG5QdsyF3Xk/8owFUeKcHxw= -github.com/go-kratos/kratos/v2 v2.5.2 h1:Oo2/YFl4ThCcGNZjAFwSCE7BXM9t5ljw3RBw+gKK6h4= -github.com/go-kratos/kratos/v2 v2.5.2/go.mod h1:5acyLj4EgY428AJnZl2EwCrMV1OVlttQFBum+SghMiA= +github.com/go-kratos/kratos/v2 v2.5.3 h1:v3F0fIFXh4HJgXprHKPGhRaobggolwPAbaBAjR+wC+Q= +github.com/go-kratos/kratos/v2 v2.5.3/go.mod h1:5acyLj4EgY428AJnZl2EwCrMV1OVlttQFBum+SghMiA= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= @@ -151,6 +156,8 @@ 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= @@ -233,6 +240,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -319,16 +327,16 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -k8s.io/api v0.25.3 h1:Q1v5UFfYe87vi5H7NU0p4RXC26PPMT8KOpr1TLQbCMQ= -k8s.io/api v0.25.3/go.mod h1:o42gKscFrEVjHdQnyRenACrMtbuJsVdP+WVjqejfzmI= -k8s.io/apimachinery v0.25.3 h1:7o9ium4uyUOM76t6aunP0nZuex7gDf8VGwkR5RcJnQc= -k8s.io/apimachinery v0.25.3/go.mod h1:jaF9C/iPNM1FuLl7Zuy5b9v+n35HGSh6AQ4HYRkCqwo= -k8s.io/client-go v0.25.3 h1:oB4Dyl8d6UbfDHD8Bv8evKylzs3BXzzufLiO27xuPs0= -k8s.io/client-go v0.25.3/go.mod h1:t39LPczAIMwycjcXkVc+CB+PZV69jQuNx4um5ORDjQA= +k8s.io/api v0.25.4 h1:3YO8J4RtmG7elEgaWMb4HgmpS2CfY1QlaOz9nwB+ZSs= +k8s.io/api v0.25.4/go.mod h1:IG2+RzyPQLllQxnhzD8KQNEu4c4YvyDTpSMztf4A0OQ= +k8s.io/apimachinery v0.25.4 h1:CtXsuaitMESSu339tfhVXhQrPET+EiWnIY1rcurKnAc= +k8s.io/apimachinery v0.25.4/go.mod h1:jaF9C/iPNM1FuLl7Zuy5b9v+n35HGSh6AQ4HYRkCqwo= +k8s.io/client-go v0.25.4 h1:3RNRDffAkNU56M/a7gUfXaEzdhZlYhoW8dgViGy5fn8= +k8s.io/client-go v0.25.4/go.mod h1:8trHCAC83XKY0wsBIpbirZU4NTUpbuhc2JnI7OruGZw= k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= -k8s.io/kube-openapi v0.0.0-20221106113015-f73e7dbcfe29 h1:tya1+VIpw4iOtih5oB7B7nMINohQcyedmlTzdDwSHPA= -k8s.io/kube-openapi v0.0.0-20221106113015-f73e7dbcfe29/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4= +k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70 h1:zfqQc1V6/ZgGpvrOVvr62OjiqQX4lZjfznK34NQwkqw= +k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4= k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 h1:GfD9OzL11kvZN5iArC6oTS7RTj7oJOIfnislxYlqTj8= k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 h1:iXTIw73aPyC+oRdyqqvVJuloN1p0AC/kzH07hu3NE+k= diff --git a/internal/biz/biz.go b/internal/biz/biz.go index c41d48e..651046f 100644 --- a/internal/biz/biz.go +++ b/internal/biz/biz.go @@ -2,101 +2,188 @@ 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/google/wire" "strings" + "sync" "time" ) +var wg sync.WaitGroup + // ProviderBizSet is biz providers. var ProviderBizSet = wire.NewSet(NewBiz) +type PodData struct { + RoomId int64 + BranchId int64 + Name string +} + type Biz struct { log log.Logger room *room.RoomService - podChan chan PodName + depChan chan PodData + serChan chan PodData + deploy deployment.Deployment + ser service.Service } -func NewBiz(logger log.Logger, room *room.RoomService) *Biz { +func NewBiz(logger log.Logger, roomServer *room.RoomService) *Biz { return &Biz{ log: logger, - podChan: make(chan PodName, 2), - room: room, - //data: data, + depChan: make(chan PodData, 1000), + serChan: make(chan PodData, 1000), + room: roomServer, } } -func (biz *Biz) Check() { +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) + if err != nil { + biz.log.Errorf("Service connect failed:%v", err) + return + } + biz.deploy = deploy + biz.ser = ser -type PodName struct { - RoomId int64 - BranchId int64 - Name string + wg.Add(3) + go func() { + defer wg.Done() + biz.Deployment(ctx) + }() + go func() { + defer wg.Done() + biz.Service(ctx) + }() + + go func() { + defer wg.Done() + biz.Room(ctx) + }() + + wg.Wait() + biz.log.Info("Task done") } -func (biz *Biz) pods(ctx context.Context) { - pod, err := k8s_client.NewPod(k8s_client.UnityNamespace, biz.log) +func (biz *Biz) Deployment(ctx context.Context) { + deployments, err := biz.deploy.List(ctx) if err != nil { - biz.log.Errorf("New pod failed:%v", err) + biz.log.Errorf("Get pod list failed:%v", err) return } - pods, err := pod.All(ctx, 0) + + 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]) + } + select { + case biz.depChan <- pn: + + case <-time.After(30 * time.Second): //最多等到30秒,避免死锁 + biz.log.Info("deployment quit") + return + } + + } +} + +func (biz *Biz) Service(ctx context.Context) { + services, err := biz.ser.List(ctx) if err != nil { biz.log.Errorf("Get pod list failed:%v", err) return } - for _, v := range pods { - name := v.Name[18:] - names := strings.Split(name, "-") - roomId := util.StringTarnsInt64(names[0]) - if roomId > 0 { - biz.podChan <- PodName{ - RoomId: roomId, - BranchId: util.StringTarnsInt64(names[1]), - Name: v.Name, - } + for _, v := range services { + 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]) + } + select { + case biz.serChan <- pn: + case <-time.After(30 * time.Second): //最多等到30秒,避免死锁 + biz.log.Info("service quit") + return } - - //time.Sleep(time.Second * 1) } } -func (biz *Biz) getRoom(ctx context.Context) { +func (biz *Biz) Room(ctx context.Context) { for { select { - case pod := <-biz.podChan: - r, err := biz.room.GetRoom(ctx, &v1.GetRoomRequest{ - RoomId: pod.RoomId, + 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) }) if err != nil { - biz.log.Errorf("Get room failed:%v", err) - continue - } - // 删除 - if r.RoomId == 0 || r.Status != 1 { - fmt.Println(r) - fmt.Println(r.RoomId, r.Status) - continue + biz.log.Errorf("Delete deployment failed:%v", err) } - var flag bool - for _, branch := range r.Branches { - fmt.Println(branch) - if branch.BranchId == pod.BranchId { - flag = true - } + 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) + }) + if err != nil { + biz.log.Errorf("Delete deployment failed:%v", err) } - fmt.Println(flag) - case <-time.After(10 * time.Second): - biz.log.Info("ok") + continue + case <-time.After(30 * time.Second): //最多等到30秒,避免死锁 + biz.log.Info("room quit") return } } } + +func (biz *Biz) del(ctx context.Context, p PodData, del func(string) error) error { + // 无房间直接删除 + if p.RoomId == 0 { + return del(p.Name) + } + r, err := biz.room.GetRoom(ctx, &v1.GetRoomRequest{ + RoomId: p.RoomId, + }) + if err != nil { + biz.log.Errorf("Get room failed:%v", err) + return err + } + // 删除不存在的房间 + 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 + } + } + + if !flag { + return del(p.Name) + } + return nil +} diff --git a/internal/biz/biz_test.go b/internal/biz/biz_test.go index a96ac72..4bfe4fc 100644 --- a/internal/biz/biz_test.go +++ b/internal/biz/biz_test.go @@ -3,6 +3,7 @@ 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" @@ -10,16 +11,31 @@ import ( func TestNewBiz(t *testing.T) { l := log.DefaultLogger() - service, err := room.NewRoomService(l) + roomServer, err := room.NewRoomService(l) if err != nil { t.Fatal(err) } - n := NewBiz(l, service) + deploy, err := k8s_client.NewDeployment(k8s_client.UnityNamespace, l) + if err != nil { + t.Fatal(err) + } + ser, err := k8s_client.NewService(k8s_client.UnityNamespace, l) + if err != nil { + t.Fatal(err) + } + n := NewBiz(l, roomServer) + n.room = roomServer + n.deploy = deploy + n.ser = ser + ctx := context.TODO() + go func() { + n.Deployment(ctx) + }() go func() { - n.pods(context.TODO()) + n.Service(ctx) }() go func() { - n.getRoom(context.TODO()) + n.Room(ctx) }() time.Sleep(time.Second * 30) }