package tomail import ( "fmt" "io/ioutil" "log" "net/http" "net/smtp" "qfw/util" mgdb "qfw/util/mongodb" sp "spiderutil" "strconv" "strings" "time" "github.com/cron" "gopkg.in/mgo.v2/bson" . "gopkg.in/mgo.v2/bson" ) var Mail map[string]interface{} func TimeTask() { defer util.Catch() c := cron.New() c.Start() c.AddFunc("0 20 9 ? * MON-FRI", CheckCreateTask) c.AddFunc("0 0 */1 ? * *", CheckLuaMove) c.Start() } //监测爬虫由历史转增量时未成功的 func CheckLuaMove() { defer util.Catch() util.Debug("开始检测爬虫节点移动...") query := map[string]interface{}{ "comeintime": map[string]interface{}{ "$gte": time.Now().Add(-(time.Hour * 1)).Unix(), "$lte": time.Now().Unix(), }, "ok": false, } util.Debug("query:", query) list := *mgdb.Find("luamovelog", query, nil, nil, false, -1, -1) text := "" if len(list) > 0 { for _, l := range list { stype := util.ObjToString(l["type"]) code := util.ObjToString(l["code"]) text += code + ":" + stype + ";" } } if text != "" { for i := 1; i <= 3; i++ { res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", sp.Config.JkMail["api"], sp.Config.JkMail["to"], "lua-move-fail", text)) if err == nil { res.Body.Close() read, err := ioutil.ReadAll(res.Body) util.Debug("邮件发送:", string(read), err) break } } } } //检测创建任务失败的爬虫 func CheckCreateTask() { defer util.Catch() util.Debug("开始检测任务创建...") query := map[string]interface{}{ "comeintime": map[string]interface{}{ "$gte": GetTime(0), }, } codes := []string{} list := *mgdb.Find("luacreatetaskerr", query, nil, nil, false, -1, -1) if len(list) > 0 { for _, l := range list { code := util.ObjToString(l["code"]) codes = append(codes, code) } } if len(codes) > 0 { for i := 1; i <= 3; i++ { res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", sp.Config.JkMail["api"], sp.Config.JkMail["to"], "lua-createtask-err", "爬虫:"+strings.Join(codes, ";"))) if err == nil { res.Body.Close() read, err := ioutil.ReadAll(res.Body) util.Debug("邮件发送:", string(read), err) break } } } } func SendToMail() { mailInfo := *(util.ObjToMap(Mail["smtp"])) host := mailInfo["host"].(string) from := mailInfo["from"].(string) pwd := mailInfo["password"].(string) subject := mailInfo["subject"].(string) hour := time.Now().Hour() if hour == 8 { //定时查询数据库 查询需要发邮件的人和相关信息 timeStr := time.Now().Format("2006-01-02") the_time, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local) time_zero := the_time.Unix() //当日凌晨的时间戳 time_twentyFour := time_zero + 86399 //当日24时的时间戳 //聚合查询数据 //mgdb.InitMongodbPool(5, "192.168.3.207:27080", "editor") sess := mgdb.GetMgoConn() defer mgdb.DestoryMongoConn(sess) var res []M sess.DB("editor").C("task").Pipe([]M{M{"$match": M{"l_complete": M{"$gte": time_zero, "$lte": time_twentyFour}, "i_state": M{"$gte": 1, "$lte": 2}}}, M{"$group": M{"_id": "$s_modifyid", "count": M{"$sum": 1}}}}).All(&res) //遍历数据进行发邮件 for _, v := range res { _id, ok := v["_id"].(string) if ok { query := bson.M{ "_id": bson.ObjectIdHex(_id), } user := *mgdb.FindOne("user", query) if user["s_email"] == nil { continue } num := strconv.Itoa(v["count"].(int)) body := `

你有` + num + `条任务需要今天完成

` hp := strings.Split(host, ":") auth := smtp.PlainAuth("", from, pwd, hp[0]) content_type := "Content-Type: text/html; charset=UTF-8" msg := []byte("To: " + user["s_email"].(string) + "\r\nFrom: " + from + "\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body) send_to := strings.Split(user["s_email"].(string), ";") err := smtp.SendMail(host, auth, from, send_to, msg) if err == nil { log.Println(user["s_email"].(string), " sendMail success") } else { log.Println(user["s_email"].(string), " sendMail fail") } } } time.Sleep(1 * time.Hour) } //time.AfterFunc(30*time.Minute, func() { SendToMail(to, num) }) time.AfterFunc(30*time.Minute, SendToMail) } //获取第day天凌晨的时间戳 func GetTime(day int) int64 { defer util.Catch() nowTime := time.Now().AddDate(0, 0, day) timeStr := util.FormatDate(&nowTime, util.Date_Short_Layout) t, _ := time.ParseInLocation(util.Date_Short_Layout, timeStr, time.Local) return t.Unix() }