monitor.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package ProcessMonitor
  2. import (
  3. "AudioPlayer/Common"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/shirou/gopsutil/v3/process"
  7. "go-micro.dev/v5/registry"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. )
  14. // 辅助函数:检查特定 PID 的进程是否存在
  15. func processExistsByPid(pid int32) bool {
  16. _, err := process.NewProcess(pid)
  17. if err != nil {
  18. if err == process.ErrorProcessNotRunning {
  19. return false
  20. }
  21. return false
  22. }
  23. return true
  24. }
  25. type PidInfo struct {
  26. Pid int32
  27. IdName string
  28. }
  29. func (p *PidInfo) Exists() bool {
  30. return processExistsByPid(p.Pid)
  31. }
  32. var StitchingServerPid map[int32]PidInfo
  33. func GetProcess() {
  34. // 读取文件并解析json
  35. err := Common.Micro.Register(&registry.Service{})
  36. procs, err := process.Processes()
  37. if err != nil {
  38. return
  39. }
  40. for _, p := range procs {
  41. name, err := p.Name()
  42. if err != nil {
  43. continue
  44. }
  45. if strings.Contains(name, "StitchingServer") {
  46. if _, ok := StitchingServerPid[p.Pid]; ok {
  47. continue
  48. }
  49. cwd, err := p.Cwd()
  50. if err != nil {
  51. continue
  52. }
  53. file, err := os.OpenFile(filepath.Join(cwd, "Server", "SplicingGuide.json"), os.O_RDONLY, 0644)
  54. defer file.Close()
  55. all, err := io.ReadAll(file)
  56. if err != nil {
  57. continue
  58. }
  59. var jsondata StitchingServerServerJson
  60. err = json.Unmarshal(all, &jsondata)
  61. if err != nil {
  62. continue
  63. }
  64. err = Common.Micro.Register(&registry.Service{
  65. Name: "omat_stitching_server",
  66. Version: "latest",
  67. Nodes: []*registry.Node{
  68. {
  69. Id: Common.Machine + "_" + strings.Replace(fmt.Sprintf("%s_%d", jsondata.Server.Host, jsondata.Server.Port), ".", "_", -1) + "_omat_stitching_server",
  70. Address: fmt.Sprintf("%s:%d", "0.0.0.0", jsondata.Server.Port),
  71. Metadata: map[string]string{
  72. "protocol": "http",
  73. },
  74. },
  75. },
  76. })
  77. if err != nil {
  78. continue
  79. }
  80. var keysToDelete []int32
  81. for _, info := range StitchingServerPid {
  82. if info.Exists() {
  83. continue
  84. }
  85. // 读取文件并解析json
  86. err = Common.Micro.Deregister(&registry.Service{
  87. Name: "omat_stitching_server",
  88. Version: "latest",
  89. Nodes: []*registry.Node{
  90. {
  91. Id: info.IdName,
  92. },
  93. },
  94. })
  95. keysToDelete = append(keysToDelete, info.Pid)
  96. }
  97. for _, key := range keysToDelete {
  98. delete(StitchingServerPid, key)
  99. }
  100. StitchingServerPid[p.Pid] = PidInfo{
  101. Pid: p.Pid,
  102. IdName: Common.Machine + "_" + strings.Replace(fmt.Sprintf("%s_%d", jsondata.Server.Host, jsondata.Server.Port), ".", "_", -1) + "_omat_stitching_server",
  103. }
  104. }
  105. for _, info := range StitchingServerPid {
  106. info.Exists()
  107. }
  108. }
  109. return
  110. }
  111. type StitchingServerServerJson struct {
  112. Server struct {
  113. Host string `json:"Host"`
  114. Port int `json:"Port"`
  115. } `json:"Server"`
  116. }
  117. func Start() {
  118. for {
  119. GetProcess()
  120. time.Sleep(time.Second * 5)
  121. }
  122. }