Ver código fonte

windows mdsn 优化

工作站 1 mês atrás
pai
commit
a652de727f
6 arquivos alterados com 149 adições e 103 exclusões
  1. 99 0
      Docker/monitor.go
  2. 0 54
      Docker/test.go
  3. 38 40
      Process/monitor.go
  4. 5 5
      cfg.toml
  5. 6 3
      main.go
  6. 1 1
      monitor.go

+ 99 - 0
Docker/monitor.go

@@ -0,0 +1,99 @@
+package Docker
+
+import (
+	"AudioPlayer/Common"
+	"context"
+	"fmt"
+	"github.com/docker/docker/api/types/container"
+	"github.com/docker/docker/client"
+	"go-micro.dev/v5/registry"
+	"hash/crc32"
+	"strings"
+	"time"
+)
+
+type DockerInfo struct {
+	IdName      string
+	ContainerId string
+}
+
+func (o *DockerInfo) Exists(dockerClient *client.Client) bool {
+	containerInfo, err := dockerClient.ContainerInspect(context.Background(), o.ContainerId)
+	if err != nil {
+		return false
+	}
+	return containerInfo.State.Running
+}
+
+var onlineContainer map[string]DockerInfo
+
+func task() {
+	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
+	if err != nil {
+		return
+	}
+	// 查看正在运行的容器
+	containers, err := cli.ContainerList(context.Background(), container.ListOptions{})
+	if err != nil {
+		return
+	}
+	for _, summary := range containers {
+		var keysToDelete []string
+		for _, info := range onlineContainer {
+			if info.Exists(cli) {
+				continue
+			}
+			// 读取文件并解析json
+			err = Common.Micro.Deregister(&registry.Service{
+				Name:    "omat_business",
+				Version: "latest",
+				Nodes: []*registry.Node{
+					{
+						Id: info.IdName,
+					},
+				},
+			})
+			keysToDelete = append(keysToDelete, info.ContainerId)
+		}
+		for _, key := range keysToDelete {
+			delete(onlineContainer, key)
+		}
+		if _, ok := onlineContainer[summary.ID]; ok {
+			continue
+		}
+
+		if strings.Contains(summary.Image, "business-go") {
+			// 读取文件并解析json
+			err = Common.Micro.Register(&registry.Service{
+				Name:    "omat_business",
+				Version: "latest",
+				Nodes: []*registry.Node{
+					{
+						Id:      fmt.Sprintf("%s-%v", Common.Machine, crc32.ChecksumIEEE([]byte(summary.Image))),
+						Address: "0.0.0.0:443",
+						Metadata: map[string]string{
+							"protocol": "https",
+							"ip":       strings.Join(Common.Address, ","),
+						},
+					},
+				},
+			})
+			if err != nil {
+				continue
+			}
+			onlineContainer[summary.ID] = DockerInfo{
+				IdName:      fmt.Sprintf("%s-%v", Common.Machine, crc32.ChecksumIEEE([]byte(summary.Image))),
+				ContainerId: summary.ID,
+			}
+
+		}
+	}
+}
+
+func Start() {
+	onlineContainer = make(map[string]DockerInfo)
+	for {
+		task()
+		time.Sleep(time.Second * 5)
+	}
+}

+ 0 - 54
Docker/test.go

@@ -1,54 +0,0 @@
-package Docker
-
-import (
-	"context"
-	"github.com/docker/docker/api/types/container"
-	"github.com/docker/docker/client"
-	"regexp"
-	"sync"
-	"time"
-)
-
-var OnlineContainerID map[string]container.Summary
-var OnlineContainerMux sync.RWMutex
-var isMonitoring bool
-var monitorTimer *time.Timer
-var debounceDuration = 5 * time.Second // 设置消抖时间为5秒
-
-func DockerOnlineContainer() {
-	if isMonitoring {
-		return
-	}
-	isMonitoring = true
-	ctx := context.Background()
-	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
-	if err != nil {
-		return
-	}
-	// 查看正在运行的容器
-	containers, err := cli.ContainerList(ctx, container.ListOptions{})
-	if err != nil {
-		return
-	}
-	OnlineContainerMux.Lock()
-	OnlineContainerID = make(map[string]container.Summary)
-	compile, err := regexp.Compile("business-go")
-	if err != nil {
-		return
-	}
-
-	for _, info := range containers {
-		if !compile.MatchString(info.Image) {
-			continue
-		}
-		OnlineContainerID[info.ID] = info
-	}
-	OnlineContainerMux.Unlock()
-	if monitorTimer != nil {
-		monitorTimer.Stop()
-	}
-	monitorTimer = time.AfterFunc(debounceDuration, func() {
-		isMonitoring = false
-	})
-	return
-}

+ 38 - 40
Process/monitor.go

@@ -36,28 +36,49 @@ func (p *PidInfo) Exists() bool {
 
 var StitchingServerPid map[int32]PidInfo
 
-func GetProcess() {
-	// 读取文件并解析json
-	err := Common.Micro.Register(&registry.Service{})
-
+func task() {
 	procs, err := process.Processes()
 	if err != nil {
 		return
 	}
 	for _, p := range procs {
+
+		var keysToDelete []int32
+		for _, info := range StitchingServerPid {
+			if info.Exists() {
+				continue
+			}
+			// 读取文件并解析json
+			err = Common.Micro.Deregister(&registry.Service{
+				Name:    "omat_stitching_server",
+				Version: "latest",
+				Nodes: []*registry.Node{
+					{
+						Id: info.IdName,
+					},
+				},
+			})
+			keysToDelete = append(keysToDelete, info.Pid)
+		}
+		for _, key := range keysToDelete {
+			delete(StitchingServerPid, key)
+		}
+
+		if _, ok := StitchingServerPid[p.Pid]; ok {
+			continue
+		}
+
 		name, err := p.Name()
 		if err != nil {
 			continue
 		}
+		cwd, err := p.Cwd()
+		if err != nil {
+			continue
+		}
+
 		if strings.Contains(name, "StitchingServer") {
-			if _, ok := StitchingServerPid[p.Pid]; ok {
-				continue
-			}
-			cwd, err := p.Cwd()
-			if err != nil {
-				continue
-			}
-			file, err := os.OpenFile(filepath.Join(cwd, "Server", "SplicingGuide.json"), os.O_RDONLY, 0644)
+			file, err := os.OpenFile(filepath.Join(cwd, "configure", "Server.json"), os.O_RDONLY, 0644)
 			defer file.Close()
 
 			all, err := io.ReadAll(file)
@@ -69,16 +90,16 @@ func GetProcess() {
 			if err != nil {
 				continue
 			}
-
 			err = Common.Micro.Register(&registry.Service{
 				Name:    "omat_stitching_server",
 				Version: "latest",
 				Nodes: []*registry.Node{
 					{
-						Id:      Common.Machine + "_" + strings.Replace(fmt.Sprintf("%s_%d", jsondata.Server.Host, jsondata.Server.Port), ".", "_", -1) + "_omat_stitching_server",
+						Id:      Common.Machine + "-" + strings.Replace(fmt.Sprintf("%s-%d", jsondata.Server.Host, jsondata.Server.Port), ".", "-", -1),
 						Address: fmt.Sprintf("%s:%d", "0.0.0.0", jsondata.Server.Port),
 						Metadata: map[string]string{
 							"protocol": "http",
+							"ip":       strings.Join(Common.Address, ","),
 						},
 					},
 				},
@@ -86,35 +107,11 @@ func GetProcess() {
 			if err != nil {
 				continue
 			}
-			var keysToDelete []int32
-			for _, info := range StitchingServerPid {
-				if info.Exists() {
-					continue
-				}
-				// 读取文件并解析json
-				err = Common.Micro.Deregister(&registry.Service{
-					Name:    "omat_stitching_server",
-					Version: "latest",
-					Nodes: []*registry.Node{
-						{
-							Id: info.IdName,
-						},
-					},
-				})
-				keysToDelete = append(keysToDelete, info.Pid)
-			}
-			for _, key := range keysToDelete {
-				delete(StitchingServerPid, key)
-			}
-
 			StitchingServerPid[p.Pid] = PidInfo{
 				Pid:    p.Pid,
-				IdName: Common.Machine + "_" + strings.Replace(fmt.Sprintf("%s_%d", jsondata.Server.Host, jsondata.Server.Port), ".", "_", -1) + "_omat_stitching_server",
+				IdName: Common.Machine + "-" + strings.Replace(fmt.Sprintf("%s-%d", jsondata.Server.Host, jsondata.Server.Port), ".", "-", -1),
 			}
 		}
-		for _, info := range StitchingServerPid {
-			info.Exists()
-		}
 	}
 	return
 }
@@ -127,8 +124,9 @@ type StitchingServerServerJson struct {
 }
 
 func Start() {
+	StitchingServerPid = make(map[int32]PidInfo)
 	for {
-		GetProcess()
+		task()
 		time.Sleep(time.Second * 5)
 	}
 }

+ 5 - 5
cfg.toml

@@ -11,10 +11,10 @@
 #  RecordingEquipment = "audio=Microphone in (Realtek(R) Audio)"
 #  Volume = 50
 
-UseNTP = true
+#UseNTP = true
 
-[Intercom]
-  Volume = 50
+#[Intercom]
+#  Volume = 50
 
-[Alarm]
-  Volume = 50
+#[Alarm]
+#  Volume = 50

+ 6 - 3
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"AudioPlayer/Common"
+	"AudioPlayer/Docker"
 	"AudioPlayer/Intercom"
 	"AudioPlayer/NTP"
 	ProcessMonitor "AudioPlayer/Process"
@@ -108,7 +109,6 @@ func (receiver *ServerInfo) Refresh() {
 
 func main() {
 	Common.Micro = registry.NewRegistry()
-	go ProcessMonitor.Start()
 
 	var err error
 	var cfg ServerInfo
@@ -270,7 +270,7 @@ func main() {
 			Version: "latest",
 			Nodes: []*registry.Node{
 				{
-					Id:      Common.Machine + "_tsukino_usagi",
+					Id:      Common.Machine + "_http",
 					Address: "0.0.0.0:4000",
 					Metadata: map[string]string{
 						"protocol": "http",
@@ -278,7 +278,7 @@ func main() {
 					},
 				},
 				{
-					Id:      Common.Machine + "_tsukino_usagi_grpc",
+					Id:      Common.Machine + "_grpc",
 					Address: "0.0.0.0:50051",
 					Metadata: map[string]string{
 						"protocol": "grpc",
@@ -293,6 +293,9 @@ func main() {
 		return
 	}
 
+	go ProcessMonitor.Start()
+	go Docker.Start()
+
 	r.Run(":4000")
 
 }

+ 1 - 1
monitor.go

@@ -26,7 +26,7 @@ type IOCountersStat struct {
 
 func shouldSkipInterface(name string) bool {
 	// 使用正则表达式匹配需要跳过的接口名称
-	skipPattern := regexp.MustCompile(`^lo|docker|br|蓝牙|Loopback|veth|vEth`)
+	skipPattern := regexp.MustCompile(`^lo|docker|br|蓝牙|Loopback|veth|vEth|VMware`)
 	return skipPattern.MatchString(name)
 }