package udptask

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"sync"
	"time"
	. "tools"
)

var udptaskmap = &sync.Map{}
var tomail string
var api string

func checkMapJob() {
	//阿里云内网无法发送邮件
	jkmail, _ := Config["jkmail"].(map[string]interface{})
	if jkmail != nil {
		tomail, _ = jkmail["to"].(string)
		api, _ = jkmail["api"].(string)
	}
	log.Println("start checkMapJob", tomail, jkmail)
	for {
		udptaskmap.Range(func(k, v interface{}) bool {
			now := time.Now().Unix()
			node, _ := v.(*UdpNode)
			if now-node.Timestamp > 120 {
				udptaskmap.Delete(k)
				res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, "分类-下节点-未响应，请相关人员检查", k.(string)))
				if err == nil {
					defer res.Body.Close()
					read, err := ioutil.ReadAll(res.Body)
					log.Println("邮件发发送：", string(read), err)
				}
			}
			return true
		})
		time.Sleep(60 * time.Second)
	}
}
