package main

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

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

type udpNode struct {
	timestamp int64
}

func checkMailJob() {

	//阿里云内网无法发送邮件
	jkmail, _ := Config["jkmail"].(map[string]interface{})
	if jkmail != nil {
		tomail, _ = jkmail["to"].(string)
		api, _ = jkmail["api"].(string)
	}
	log.Println("start check mail Job", tomail, Config["jkmail"])
	for {
		udptaskmap.Range(func(k, v interface{}) bool {
			now := time.Now().Unix()
			node, _ := v.(*udpNode)
			if now-node.timestamp >= 600 {
				udptaskmap.Delete(k)
				res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, "extract_control-warning",k.(string)))
				if err == nil {
					defer res.Body.Close()
					read, err := ioutil.ReadAll(res.Body)
					log.Println("控制中心-邮件发送成功：", string(read), err)
				}else {
					log.Println("控制中心-邮件发送异常：", err)
				}
			}
			return true
		})
		time.Sleep(60 * time.Second)
	}
}
