并发+超时示例

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")
}

 

转载于:https://my.oschina.net/zhangxc73912/blog/2987884

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值