并发+超时示例

原创
2018/12/13 12:34
阅读数 75
func installMantisAgent() {
	log.Println("begin auto repair mantis agent")
	num := 0
	succNum := 0
	failNum := 0
	var Q *queue.Queue

	switch g.Config().RepairType {
	case "mysql":
		Q = g.SshNoSQLMantisAgentQueue
	case "nosql":
		Q = g.SshDBMantisAgentQueue
	default:
		Q = g.SshMantisAgentQueue
	}
	log.Println("需要安装mantis agent机器数:", Q.Len())
	for !Q.Empty() {
		tmpMachineList, err := Q.Get(g.Config().ConcurrentInstallNum)
		if err != nil {
			fmt.Println("get data from SshQueue,happend err", err)
			continue
		}
		num += len(tmpMachineList)
		var wg sync.WaitGroup
		count := make(chan string, len(tmpMachineList))
		for _, ip := range tmpMachineList {
			wg.Add(1)
			//time.After用于超时处理
			go func(ip string) {
				defer wg.Done()
				timeout := make(chan string, 1)
				go func(ip string) {
					var msg string
					//ret, err := InstallMantisAgent(ip)
					ret := true
					err := fmt.Errorf("test")
					if ret {
						msg = fmt.Sprintf("ip:%s start mantis agent successfully", ip)
						//更新db状态并将err写入db
						alive, version := checkMachine.HttpGet(ip)
						checkMachine.UpdateAgentStatus(alive, version, ip, msg)
						count <- "success"
					} else {
						msg = fmt.Sprintf("ip:%s start mantis agent failed,err:%v", ip, err)
						//更新db状态并将err写入db
						checkMachine.UpdateAgentStatus(false, "", ip, msg)
						count <- "fail"
					}

					timeout <- msg
				}(ip)
				select {
				case res := <-timeout:
					log.Println(res)
				case <-time.After(time.Second * 120):
					log.Printf("ip:%s start mantis agent failed,err:%s", ip, "远程命令执行超时")
					//更新db状态并将err写入db
					checkMachine.UpdateAgentStatus(false, "", ip, fmt.Sprintf("start mantis agent failed,err:%s", "远程命令执行超时"))
					count <- "fail"
				}
			}(ip.(string))

		}
		wg.Wait()
		for i := 0; i < len(tmpMachineList); i++ {
			rest := <-count
			if rest == "success" {
				succNum += 1
			}
			if rest == "fail" {
				failNum += 1
			}
		}
		log.Printf("Mantis 当前已完成任务:%v,成功任务:%v,失败任务:%v,当前剩余任务:%v", num, succNum, failNum, g.SshMantisAgentQueue.Len())
	}
	log.Println("end   auto repair mantis agent")
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部