package monitor import ( "basic.com/valib/logger.git" "bee-config/config" "bee-config/models" "bee-config/utils" "context" "os" "strings" "sync" "time" ) func Init(ctx context.Context, wg *sync.WaitGroup) { wg.Add(1) go monitorLoop(ctx, wg) } func monitorLoop(ctx context.Context, wg *sync.WaitGroup) { t := time.NewTicker(time.Second * 2) for { select { case <-ctx.Done(): t.Stop() wg.Done() return case <- t.C: syncStatus() } } } func syncStatus() { var node models.BeeNode arr, err := node.FindAll() if nil != err { logger.Error("syncStatus failed to read all data, err:", err) return } nodes := make([]*models.BeeNode, len(arr)) for _, v := range arr { nodes = append(nodes, &v) id := uint8(v.Id) dir := config.GetNodeDir(id) if _, err := os.Stat(dir); os.IsNotExist(err) { continue } output, err := utils.ShellExecute(dir, "docker-compose", "ps", "-a") if nil != err { logger.Error("syncStatus failed to invoke docker-compose, err:", err) continue } containers := parseDockerCompose(output) if len(containers) < 2 { logger.Error("syncStatus no enough containers") continue } var beeStatus, clefStatus string for _, v := range containers { words := strings.Split(v, " ") if len(words) < 5 { continue } if strings.Contains(words[0], "bee-1") { beeStatus = words[3] } if strings.Contains(words[0], "clef-1") { clefStatus = words[3] } } if len(beeStatus) < 1 { logger.Error("syncStatus failed to find bee container") continue } if len(clefStatus) < 1 { logger.Error("syncStatus failed to find clefStatus container") continue } } } func parseDockerCompose(output string) []string { lines := strings.Split(output, "\n") for i, v := range lines { if strings.HasPrefix(v, "------") { index := i + 1 return lines[index:] } } return []string{} }