monitor.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 task() {
  34. procs, err := process.Processes()
  35. if err != nil {
  36. return
  37. }
  38. for _, p := range procs {
  39. var keysToDelete []int32
  40. for _, info := range StitchingServerPid {
  41. if info.Exists() {
  42. continue
  43. }
  44. // 读取文件并解析json
  45. err = Common.Micro.Deregister(&registry.Service{
  46. Name: "omat_stitching_server",
  47. Version: "latest",
  48. Nodes: []*registry.Node{
  49. {
  50. Id: info.IdName,
  51. },
  52. },
  53. })
  54. keysToDelete = append(keysToDelete, info.Pid)
  55. }
  56. for _, key := range keysToDelete {
  57. delete(StitchingServerPid, key)
  58. }
  59. if _, ok := StitchingServerPid[p.Pid]; ok {
  60. continue
  61. }
  62. name, err := p.Name()
  63. if err != nil {
  64. continue
  65. }
  66. cwd, err := p.Cwd()
  67. if err != nil {
  68. continue
  69. }
  70. if strings.Contains(name, "StitchingServer") {
  71. file, err := os.OpenFile(filepath.Join(cwd, "configure", "Server.json"), os.O_RDONLY, 0644)
  72. defer file.Close()
  73. all, err := io.ReadAll(file)
  74. if err != nil {
  75. continue
  76. }
  77. var jsondata StitchingServerServerJson
  78. err = json.Unmarshal(all, &jsondata)
  79. if err != nil {
  80. continue
  81. }
  82. err = Common.Micro.Register(&registry.Service{
  83. Name: "omat_stitching_server",
  84. Version: "latest",
  85. Nodes: []*registry.Node{
  86. {
  87. Id: Common.Machine + "-" + strings.Replace(fmt.Sprintf("%s-%d", jsondata.Server.Host, jsondata.Server.Port), ".", "-", -1),
  88. Address: fmt.Sprintf("%s:%d", "0.0.0.0", jsondata.Server.Port),
  89. Metadata: map[string]string{
  90. "protocol": "http",
  91. "ip": strings.Join(Common.Address, ","),
  92. },
  93. },
  94. },
  95. })
  96. if err != nil {
  97. continue
  98. }
  99. StitchingServerPid[p.Pid] = PidInfo{
  100. Pid: p.Pid,
  101. IdName: Common.Machine + "-" + strings.Replace(fmt.Sprintf("%s-%d", jsondata.Server.Host, jsondata.Server.Port), ".", "-", -1),
  102. }
  103. }
  104. }
  105. return
  106. }
  107. type StitchingServerServerJson struct {
  108. Server struct {
  109. Host string `json:"Host"`
  110. Port int `json:"Port"`
  111. } `json:"Server"`
  112. }
  113. func Start() {
  114. StitchingServerPid = make(map[int32]PidInfo)
  115. for {
  116. task()
  117. time.Sleep(time.Second * 5)
  118. }
  119. }