From 632fbba093a41e0fdc2ad4aa1814fa303b4afe89 Mon Sep 17 00:00:00 2001 From: Humanz Date: Sun, 15 May 2022 00:08:17 +0700 Subject: [PATCH 1/8] Add expose port --- service/pilot/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/pilot/Dockerfile b/service/pilot/Dockerfile index 924fc738..cd00f760 100755 --- a/service/pilot/Dockerfile +++ b/service/pilot/Dockerfile @@ -3,5 +3,5 @@ FROM $BASE_IMAGE as base FROM alpine COPY --from=base /app/Go-Simp/bin/pilot /pilot -EXPOSE 9000 +EXPOSE 9000 8080 CMD ["./pilot"] \ No newline at end of file From 212108071c1805892e04bfab82ba9b3797123143 Mon Sep 17 00:00:00 2001 From: Humanz Date: Sun, 15 May 2022 01:32:33 +0700 Subject: [PATCH 2/8] Get api to check units payload --- service/pilot/grpc/grpc.go | 17 +++++++++++--- service/pilot/runner.go | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/service/pilot/grpc/grpc.go b/service/pilot/grpc/grpc.go index a974c38a..97649641 100755 --- a/service/pilot/grpc/grpc.go +++ b/service/pilot/grpc/grpc.go @@ -78,9 +78,15 @@ type Service struct { } type UnitService struct { - UUID string - Counter int - Payload []database.Group + UUID string + Counter int + Payload []database.Group + Metadata UnitMetadata +} + +type UnitMetadata struct { + Length int + AgencyList []string } type Server struct { @@ -167,6 +173,10 @@ func (s *Service) RemapPayload() { AgencyCount := 0 for _, v := range s.Unit { AgencyCount += len(v.Payload) + v.Metadata.Length = len(v.Payload) + for _, v2 := range v.Payload { + v.Metadata.AgencyList = append(v.Metadata.AgencyList, v2.GroupName) + } } if AgencyCount < 29 { @@ -223,6 +233,7 @@ func (s *Server) RequestRunJobsOfService(ctx context.Context, in *ServiceMessage in.ServiceUUID, 1, nil, + UnitMetadata{}, }) v.RemapPayload() diff --git a/service/pilot/runner.go b/service/pilot/runner.go index 8f29b886..579be391 100755 --- a/service/pilot/runner.go +++ b/service/pilot/runner.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "net/http" @@ -73,9 +74,53 @@ func main() { grpcServer := grpc.NewServer() + http.HandleFunc("/youtube/units/", func(w http.ResponseWriter, r *http.Request) { + Data := GetUnitsMetadata(s, config.YoutubeCheckerService) + if Data != nil { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(Data) + w.WriteHeader(http.StatusOK) + } else { + w.WriteHeader(http.StatusNotFound) + } + }) + + http.HandleFunc("/twitter/units/", func(w http.ResponseWriter, r *http.Request) { + Data := GetUnitsMetadata(s, config.TwitterService) + if Data != nil { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(Data) + w.WriteHeader(http.StatusOK) + } else { + w.WriteHeader(http.StatusNotFound) + } + }) + + go http.ListenAndServe(":8181", nil) + pilot.RegisterPilotServiceServer(grpcServer, &s) if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %s", err) } } + +func GetUnitsMetadata(s pilot.Server, name string) []pilot.UnitMetadata { + for _, v := range s.Service { + if v.Name == name { + if len(v.Unit) > 0 { + Data := []pilot.UnitMetadata{} + + for _, v2 := range v.Unit { + Data = append(Data, v2.Metadata) + } + return Data + + } + } + } + + return nil +} From 6891874536358d614ee2c40281cc4d56c6783cbc Mon Sep 17 00:00:00 2001 From: Humanz Date: Sun, 15 May 2022 01:33:00 +0700 Subject: [PATCH 3/8] Add expose port --- service/pilot/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/pilot/Dockerfile b/service/pilot/Dockerfile index cd00f760..0244a42b 100755 --- a/service/pilot/Dockerfile +++ b/service/pilot/Dockerfile @@ -3,5 +3,5 @@ FROM $BASE_IMAGE as base FROM alpine COPY --from=base /app/Go-Simp/bin/pilot /pilot -EXPOSE 9000 8080 +EXPOSE 9000 8080 8181 CMD ["./pilot"] \ No newline at end of file From fec407f67766808196db0df25502322ff2b712a5 Mon Sep 17 00:00:00 2001 From: Humanz Date: Tue, 17 May 2022 07:04:56 +0700 Subject: [PATCH 4/8] Change HeartBeat sleep --- service/pilot/grpc/grpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/pilot/grpc/grpc.go b/service/pilot/grpc/grpc.go index 97649641..54c65961 100755 --- a/service/pilot/grpc/grpc.go +++ b/service/pilot/grpc/grpc.go @@ -492,7 +492,7 @@ func (s *Server) HeartBeat(in *ServiceMessage, stream PilotService_HeartBeatServ ReportDeadService(err.Error(), in.Service) return err } - time.Sleep(5 * time.Second) + time.Sleep(1 * time.Second) } } } From c6d80b3574181c3de47d48c798431e31134d56a1 Mon Sep 17 00:00:00 2001 From: Humanz Date: Tue, 17 May 2022 08:32:41 +0700 Subject: [PATCH 5/8] Fix pilot metadata && Units send hostname --- pkg/engine/engine.go | 9 ++ service/fanart/bilibili/bilibili.go | 2 + service/fanart/pixiv/pixiv.go | 2 + service/fanart/twitter/twitter.go | 2 + service/frontend/runner.go | 3 + service/livestream/bilibili/live/live.go | 2 + service/livestream/bilibili/space/space.go | 3 + service/livestream/twitch/twitch.go | 2 + service/livestream/youtube/checker/youtube.go | 5 + service/livestream/youtube/counter/counter.go | 4 + service/migrate/main.go | 2 + service/pilot/grpc/grpc.go | 13 ++- service/pilot/grpc/grpc.pb.go | 104 ++++++++++-------- service/pilot/grpc/grpc.proto | 1 + service/rest-api/main.go | 4 + service/subscriber/runner.go | 4 + 16 files changed, 110 insertions(+), 52 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 6c385c39..b6fd2fb0 100755 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -760,3 +760,12 @@ func UnMarshalPayload(Payload []byte) []database.Group { } return agency } + +func GetHostname() string { + hostname, err := os.Hostname() + if err != nil { + log.Fatal(err) + } + + return hostname +} diff --git a/service/fanart/bilibili/bilibili.go b/service/fanart/bilibili/bilibili.go index 7df3b71a..4105ef7c 100755 --- a/service/fanart/bilibili/bilibili.go +++ b/service/fanart/bilibili/bilibili.go @@ -91,12 +91,14 @@ type checkBlJob struct { func ReqRunningJob(client pilot.PilotServiceClient) { Bili := &checkBlJob{} + hostname := engine.GetHostname() for { res, err := client.RequestRunJobsOfService(context.Background(), &pilot.ServiceMessage{ Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/fanart/pixiv/pixiv.go b/service/fanart/pixiv/pixiv.go index 940f2de8..ccfc3f5d 100644 --- a/service/fanart/pixiv/pixiv.go +++ b/service/fanart/pixiv/pixiv.go @@ -403,12 +403,14 @@ type checkPxJob struct { func ReqRunningJob(client pilot.PilotServiceClient) { Pix := &checkPxJob{} + hostname := engine.GetHostname() for { res, err := client.RequestRunJobsOfService(context.Background(), &pilot.ServiceMessage{ Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/fanart/twitter/twitter.go b/service/fanart/twitter/twitter.go index 857a3450..903e3ba4 100755 --- a/service/fanart/twitter/twitter.go +++ b/service/fanart/twitter/twitter.go @@ -88,6 +88,7 @@ func main() { func ReqRunningJob(client pilot.PilotServiceClient) { Twit := &checkTwJob{} + hostname := engine.GetHostname() for { @@ -95,6 +96,7 @@ func ReqRunningJob(client pilot.PilotServiceClient) { Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/frontend/runner.go b/service/frontend/runner.go index 460064fd..4d169b83 100755 --- a/service/frontend/runner.go +++ b/service/frontend/runner.go @@ -72,6 +72,8 @@ func main() { configfile.InitConf() + hostname := engine.GetHostname() + go func() { for { log.WithFields(log.Fields{ @@ -83,6 +85,7 @@ func main() { Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/livestream/bilibili/live/live.go b/service/livestream/bilibili/live/live.go index 91d2e05c..33c1c9f9 100755 --- a/service/livestream/bilibili/live/live.go +++ b/service/livestream/bilibili/live/live.go @@ -74,6 +74,7 @@ type checkBlLiveeJob struct { func ReqRunningJob(client pilot.PilotServiceClient) { Bili := &checkBlLiveeJob{} + hostname := engine.GetHostname() for { @@ -81,6 +82,7 @@ func ReqRunningJob(client pilot.PilotServiceClient) { Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/livestream/bilibili/space/space.go b/service/livestream/bilibili/space/space.go index e210f425..b059b2b7 100755 --- a/service/livestream/bilibili/space/space.go +++ b/service/livestream/bilibili/space/space.go @@ -100,12 +100,15 @@ func ReqRunningJob(client pilot.PilotServiceClient) { VideoIDTMP: make(map[string]string), } + hostname := engine.GetHostname() + for { res, err := client.RequestRunJobsOfService(context.Background(), &pilot.ServiceMessage{ Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/livestream/twitch/twitch.go b/service/livestream/twitch/twitch.go index f8b5a29d..16994025 100755 --- a/service/livestream/twitch/twitch.go +++ b/service/livestream/twitch/twitch.go @@ -88,12 +88,14 @@ type checkTwcJob struct { func ReqRunningJob(client pilot.PilotServiceClient) { Twitch := &checkTwcJob{} + hostname := engine.GetHostname() for { res, err := client.RequestRunJobsOfService(context.Background(), &pilot.ServiceMessage{ Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/livestream/youtube/checker/youtube.go b/service/livestream/youtube/checker/youtube.go index 351c5e73..5faf013d 100755 --- a/service/livestream/youtube/checker/youtube.go +++ b/service/livestream/youtube/checker/youtube.go @@ -88,6 +88,9 @@ func ReqRunningJob(client pilot.PilotServiceClient) { Counter: 1, Update: true, } + + hostname := engine.GetHostname() + for { if YoutubeChecker.Counter == 15 { @@ -101,6 +104,7 @@ func ReqRunningJob(client pilot.PilotServiceClient) { Service: ServiceName, Message: "Update", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) @@ -111,6 +115,7 @@ func ReqRunningJob(client pilot.PilotServiceClient) { Service: ServiceName, Message: "New", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) diff --git a/service/livestream/youtube/counter/counter.go b/service/livestream/youtube/counter/counter.go index 8db8d95d..9bbe474d 100644 --- a/service/livestream/youtube/counter/counter.go +++ b/service/livestream/youtube/counter/counter.go @@ -91,12 +91,16 @@ func ReqRunningJob(client pilot.PilotServiceClient) { YoutubeCounter := &checkYtJob{ CekCounter: make(map[string]bool), } + + hostname := engine.GetHostname() + for { res, err := client.RequestRunJobsOfService(context.Background(), &pilot.ServiceMessage{ Service: ServiceName, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { diff --git a/service/migrate/main.go b/service/migrate/main.go index ff75e986..cce8d82b 100755 --- a/service/migrate/main.go +++ b/service/migrate/main.go @@ -49,10 +49,12 @@ type NewVtuber struct { } func RequestPay(Message string) { + hostname := engine.GetHostname() res, err := gRCPconn.GetBotPayload(context.Background(), &pilot.ServiceMessage{ Message: Message, Service: "Migrate", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Fatalf("Error when request payload: %s", err) diff --git a/service/pilot/grpc/grpc.go b/service/pilot/grpc/grpc.go index 54c65961..02cad231 100755 --- a/service/pilot/grpc/grpc.go +++ b/service/pilot/grpc/grpc.go @@ -85,6 +85,8 @@ type UnitService struct { } type UnitMetadata struct { + UUID string + Hostname string Length int AgencyList []string } @@ -174,12 +176,10 @@ func (s *Service) RemapPayload() { for _, v := range s.Unit { AgencyCount += len(v.Payload) v.Metadata.Length = len(v.Payload) - for _, v2 := range v.Payload { - v.Metadata.AgencyList = append(v.Metadata.AgencyList, v2.GroupName) - } + v.Metadata.AgencyList = v.GetAgencyList() } - if AgencyCount < 29 { + if AgencyCount < 29 && AgencyCount != 0 { log.Fatal("Agency payload less than 29, len ", AgencyCount) } } @@ -233,7 +233,10 @@ func (s *Server) RequestRunJobsOfService(ctx context.Context, in *ServiceMessage in.ServiceUUID, 1, nil, - UnitMetadata{}, + UnitMetadata{ + Hostname: in.Hostname, + UUID: in.ServiceUUID, + }, }) v.RemapPayload() diff --git a/service/pilot/grpc/grpc.pb.go b/service/pilot/grpc/grpc.pb.go index ff419734..a264bbb0 100644 --- a/service/pilot/grpc/grpc.pb.go +++ b/service/pilot/grpc/grpc.pb.go @@ -122,6 +122,7 @@ type ServiceMessage struct { Message string `protobuf:"bytes,1,opt,name=Message,proto3" json:"Message,omitempty"` Service string `protobuf:"bytes,2,opt,name=Service,proto3" json:"Service,omitempty"` ServiceUUID string `protobuf:"bytes,3,opt,name=ServiceUUID,proto3" json:"ServiceUUID,omitempty"` + Hostname string `protobuf:"bytes,4,opt,name=Hostname,proto3" json:"Hostname,omitempty"` } func (x *ServiceMessage) Reset() { @@ -177,6 +178,13 @@ func (x *ServiceMessage) GetServiceUUID() string { return "" } +func (x *ServiceMessage) GetHostname() string { + if x != nil { + return x.Hostname + } + return "" +} + type Metric struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -359,54 +367,56 @@ var file_grpc_proto_rawDesc = []byte{ 0x6c, 0x65, 0x22, 0x35, 0x0a, 0x0d, 0x41, 0x67, 0x65, 0x6e, 0x63, 0x79, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x24, 0x0a, 0x0d, 0x41, 0x67, 0x65, 0x6e, 0x63, 0x79, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0d, 0x41, 0x67, 0x65, 0x6e, - 0x63, 0x79, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x73, 0x22, 0x66, 0x0a, 0x0e, 0x53, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x4d, - 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x4d, 0x65, - 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, - 0x20, 0x0a, 0x0b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x55, 0x55, 0x49, 0x44, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x55, 0x55, 0x49, - 0x44, 0x22, 0x3e, 0x0a, 0x06, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x12, 0x1e, 0x0a, 0x0a, 0x4d, - 0x65, 0x74, 0x72, 0x69, 0x63, 0x44, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, - 0x0a, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x44, 0x61, 0x74, 0x61, 0x12, 0x14, 0x0a, 0x05, 0x53, - 0x74, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x53, 0x74, 0x61, 0x74, - 0x65, 0x22, 0x07, 0x0a, 0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x9c, 0x01, 0x0a, 0x06, 0x52, - 0x75, 0x6e, 0x4a, 0x6f, 0x62, 0x12, 0x18, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, - 0x18, 0x0a, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x52, 0x75, 0x6e, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x52, 0x75, 0x6e, 0x12, 0x24, 0x0a, 0x0d, 0x56, - 0x74, 0x75, 0x62, 0x65, 0x72, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x04, 0x20, 0x01, - 0x28, 0x0c, 0x52, 0x0d, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, - 0x64, 0x12, 0x26, 0x0a, 0x0e, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x4d, 0x65, 0x74, 0x61, 0x64, - 0x61, 0x74, 0x61, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x56, 0x74, 0x75, 0x62, 0x65, - 0x72, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x32, 0xed, 0x02, 0x0a, 0x0c, 0x50, 0x69, - 0x6c, 0x6f, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3c, 0x0a, 0x0d, 0x47, 0x65, - 0x74, 0x42, 0x6f, 0x74, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x15, 0x2e, 0x70, 0x69, - 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x1a, 0x12, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x49, 0x6e, 0x69, 0x74, 0x22, 0x00, 0x12, 0x41, 0x0a, 0x17, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x52, 0x75, 0x6e, 0x4a, 0x6f, 0x62, 0x73, 0x4f, 0x66, 0x53, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x12, 0x15, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0d, 0x2e, 0x70, 0x69, 0x6c, - 0x6f, 0x74, 0x2e, 0x52, 0x75, 0x6e, 0x4a, 0x6f, 0x62, 0x22, 0x00, 0x12, 0x41, 0x0a, 0x10, 0x47, - 0x65, 0x74, 0x41, 0x67, 0x65, 0x6e, 0x63, 0x79, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, + 0x63, 0x79, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x73, 0x22, 0x82, 0x01, 0x0a, 0x0e, 0x53, 0x65, + 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x4d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x12, 0x20, 0x0a, 0x0b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x55, 0x55, 0x49, 0x44, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x55, 0x55, + 0x49, 0x44, 0x12, 0x1a, 0x0a, 0x08, 0x48, 0x6f, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x48, 0x6f, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, + 0x0a, 0x06, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x12, 0x1e, 0x0a, 0x0a, 0x4d, 0x65, 0x74, 0x72, + 0x69, 0x63, 0x44, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x4d, 0x65, + 0x74, 0x72, 0x69, 0x63, 0x44, 0x61, 0x74, 0x61, 0x12, 0x14, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x22, 0x07, + 0x0a, 0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x9c, 0x01, 0x0a, 0x06, 0x52, 0x75, 0x6e, 0x4a, + 0x6f, 0x62, 0x12, 0x18, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x52, 0x75, 0x6e, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x03, 0x52, 0x75, 0x6e, 0x12, 0x24, 0x0a, 0x0d, 0x56, 0x74, 0x75, 0x62, + 0x65, 0x72, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, + 0x0d, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x26, + 0x0a, 0x0e, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, + 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x56, 0x74, 0x75, 0x62, 0x65, 0x72, 0x4d, 0x65, + 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x32, 0xed, 0x02, 0x0a, 0x0c, 0x50, 0x69, 0x6c, 0x6f, 0x74, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3c, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x42, 0x6f, + 0x74, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x15, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, + 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, + 0x12, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, + 0x6e, 0x69, 0x74, 0x22, 0x00, 0x12, 0x41, 0x0a, 0x17, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x52, 0x75, 0x6e, 0x4a, 0x6f, 0x62, 0x73, 0x4f, 0x66, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x12, 0x15, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0d, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, + 0x52, 0x75, 0x6e, 0x4a, 0x6f, 0x62, 0x22, 0x00, 0x12, 0x41, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x41, + 0x67, 0x65, 0x6e, 0x63, 0x79, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x15, 0x2e, 0x70, + 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x1a, 0x14, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x41, 0x67, 0x65, 0x6e, + 0x63, 0x79, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x09, 0x48, + 0x65, 0x61, 0x72, 0x74, 0x42, 0x65, 0x61, 0x74, 0x12, 0x15, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, + 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, + 0x0c, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x30, + 0x01, 0x12, 0x2d, 0x0a, 0x0c, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52, 0x65, 0x70, 0x6f, 0x72, + 0x74, 0x12, 0x0d, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, + 0x1a, 0x0c, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, + 0x12, 0x34, 0x0a, 0x0b, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x15, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, - 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x14, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x41, - 0x67, 0x65, 0x6e, 0x63, 0x79, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x00, 0x12, 0x34, - 0x0a, 0x09, 0x48, 0x65, 0x61, 0x72, 0x74, 0x42, 0x65, 0x61, 0x74, 0x12, 0x15, 0x2e, 0x70, 0x69, - 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x1a, 0x0c, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x22, 0x00, 0x30, 0x01, 0x12, 0x2d, 0x0a, 0x0c, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52, 0x65, - 0x70, 0x6f, 0x72, 0x74, 0x12, 0x0d, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x4d, 0x65, 0x74, - 0x72, 0x69, 0x63, 0x1a, 0x0c, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, - 0x79, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x0b, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x45, 0x72, 0x72, - 0x6f, 0x72, 0x12, 0x15, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0c, 0x2e, 0x70, 0x69, 0x6c, 0x6f, - 0x74, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, 0x25, 0x5a, 0x23, 0x67, 0x69, 0x74, - 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6a, 0x75, 0x73, 0x74, 0x68, 0x75, 0x6d, 0x61, - 0x6e, 0x7a, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x69, 0x6c, 0x6f, 0x74, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0c, 0x2e, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x2e, 0x45, + 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, 0x25, 0x5a, 0x23, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6a, 0x75, 0x73, 0x74, 0x68, 0x75, 0x6d, 0x61, 0x6e, 0x7a, 0x2f, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x69, 0x6c, 0x6f, 0x74, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/service/pilot/grpc/grpc.proto b/service/pilot/grpc/grpc.proto index a994ed8a..eff9ecd4 100755 --- a/service/pilot/grpc/grpc.proto +++ b/service/pilot/grpc/grpc.proto @@ -14,6 +14,7 @@ message ServiceMessage { string Message = 1; string Service = 2; string ServiceUUID = 3; + string Hostname = 4; } diff --git a/service/rest-api/main.go b/service/rest-api/main.go index 8d17e30c..af25cd8e 100755 --- a/service/rest-api/main.go +++ b/service/rest-api/main.go @@ -11,6 +11,7 @@ import ( "github.com/JustHumanz/Go-Simp/pkg/config" "github.com/JustHumanz/Go-Simp/pkg/database" + "github.com/JustHumanz/Go-Simp/pkg/engine" "github.com/JustHumanz/Go-Simp/pkg/network" pilot "github.com/JustHumanz/Go-Simp/service/pilot/grpc" "github.com/google/uuid" @@ -51,6 +52,8 @@ func init() { log.Panic(err) } + hostname := engine.GetHostname() + RequestPayload := func() { var ( VtuberMembersTMP []MembersPayload @@ -60,6 +63,7 @@ func init() { Service: config.ResetApiService, Message: "Request", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Fatalf("Error when request payload: %s", err) diff --git a/service/subscriber/runner.go b/service/subscriber/runner.go index 6c760bd0..d958f3c1 100755 --- a/service/subscriber/runner.go +++ b/service/subscriber/runner.go @@ -103,11 +103,15 @@ func main() { } go pilot.RunHeartBeat(gRCPconn, ServiceName, ServiceUUID) + + hostname := engine.GetHostname() + go func() { tmp, err := gRCPconn.GetAgencyPayload(context.Background(), &pilot.ServiceMessage{ Service: ServiceName, Message: "Refresh payload", ServiceUUID: ServiceUUID, + Hostname: hostname, }) if err != nil { log.Error(err) From f5a33ff408c22ad8bd3d82326eed3a5dbff4d646 Mon Sep 17 00:00:00 2001 From: Humanz Date: Tue, 17 May 2022 20:12:28 +0700 Subject: [PATCH 6/8] HeartBeat add sleep --- service/pilot/grpc/grpc.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service/pilot/grpc/grpc.go b/service/pilot/grpc/grpc.go index 02cad231..30bbe06d 100755 --- a/service/pilot/grpc/grpc.go +++ b/service/pilot/grpc/grpc.go @@ -500,6 +500,7 @@ func (s *Server) HeartBeat(in *ServiceMessage, stream PilotService_HeartBeatServ } } } + time.Sleep(5 * time.Second) } } From edcff3319ea6b515663cd2190f801b9c9164ee76 Mon Sep 17 00:00:00 2001 From: Humanz Date: Tue, 17 May 2022 23:09:26 +0700 Subject: [PATCH 7/8] Move LowerCaseURI to engine --- service/rest-api/main.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/service/rest-api/main.go b/service/rest-api/main.go index af25cd8e..c8f61154 100755 --- a/service/rest-api/main.go +++ b/service/rest-api/main.go @@ -310,7 +310,7 @@ func main() { LiveBili.HandleFunc("/group/{groupID}/{status}", getBilibili).Methods("GET") LiveBili.HandleFunc("/member/{memberID}/{status}", getBilibili).Methods("GET") router.Use(muxlogrus.NewLogger().Middleware) - http.ListenAndServe(":2525", LowerCaseURI(router)) + http.ListenAndServe(":2525", engine.LowerCaseURI(router)) } func invalidPath(w http.ResponseWriter, r *http.Request) { @@ -815,12 +815,3 @@ type MessageError struct { Message string Date time.Time } - -func LowerCaseURI(h http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - r.URL.Path = strings.ToLower(r.URL.Path) - h.ServeHTTP(w, r) - } - - return http.HandlerFunc(fn) -} From dc4e736ec9733cbc2b7bcb91a71609b1cf254ce6 Mon Sep 17 00:00:00 2001 From: Humanz Date: Tue, 17 May 2022 23:27:18 +0700 Subject: [PATCH 8/8] Pilot update pilot command --- pkg/engine/engine.go | 9 +++++++++ service/pilot/runner.go | 31 ++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index b6fd2fb0..17317462 100755 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -769,3 +769,12 @@ func GetHostname() string { return hostname } + +func LowerCaseURI(h http.Handler) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + r.URL.Path = strings.ToLower(r.URL.Path) + h.ServeHTTP(w, r) + } + + return http.HandlerFunc(fn) +} diff --git a/service/pilot/runner.go b/service/pilot/runner.go index 579be391..bfd83938 100755 --- a/service/pilot/runner.go +++ b/service/pilot/runner.go @@ -6,10 +6,13 @@ import ( "net/http" "github.com/JustHumanz/Go-Simp/pkg/config" + "github.com/JustHumanz/Go-Simp/pkg/engine" "github.com/JustHumanz/Go-Simp/pkg/metric" network "github.com/JustHumanz/Go-Simp/pkg/network" pilot "github.com/JustHumanz/Go-Simp/service/pilot/grpc" + "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp" + muxlogrus "github.com/pytimer/mux-logrus" log "github.com/sirupsen/logrus" "google.golang.org/grpc" ) @@ -73,9 +76,30 @@ func main() { } grpcServer := grpc.NewServer() + router := mux.NewRouter() + + router.HandleFunc("/{service}/units/", func(w http.ResponseWriter, r *http.Request) { + Service := mux.Vars(r)["service"] + Data := []pilot.UnitMetadata{} + + if Service == "checker_youtube" { + Data = GetUnitsMetadata(s, config.YoutubeCheckerService) + } else if Service == "counter_youtube" { + Data = GetUnitsMetadata(s, config.YoutubeCounterService) + } else if Service == "space_bilibili" { + Data = GetUnitsMetadata(s, config.SpaceBiliBiliService) + } else if Service == "live_bilibili" { + Data = GetUnitsMetadata(s, config.LiveBiliBiliService) + } else if Service == "twitch" { + Data = GetUnitsMetadata(s, config.TwitchService) + } else if Service == "twitter" { + Data = GetUnitsMetadata(s, config.TwitterService) + } else if Service == "tbilibili" { + Data = GetUnitsMetadata(s, config.TBiliBiliService) + } else if Service == "pixiv" { + Data = GetUnitsMetadata(s, config.PixivService) + } - http.HandleFunc("/youtube/units/", func(w http.ResponseWriter, r *http.Request) { - Data := GetUnitsMetadata(s, config.YoutubeCheckerService) if Data != nil { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/json") @@ -98,7 +122,8 @@ func main() { } }) - go http.ListenAndServe(":8181", nil) + router.Use(muxlogrus.NewLogger().Middleware) + go http.ListenAndServe(":8181", engine.LowerCaseURI(router)) pilot.RegisterPilotServiceServer(grpcServer, &s)