package main

import (
	"fmt"
	"io/ioutil"
	"luatask"
	"os"
	qu "qfw/util"
	"time"
	"timetask"
	"util"

	"github.com/donnie4w/go-logger/logger"

	"github.com/cron"
)

func init() {
	qu.ReadConfig(&util.Config)
	qu.ReadConfig("./worktime.json", &util.Workfig)
	qu.ReadConfig("./user.json", &timetask.LuaUserMap)
	util.InitWorkfig()
	timetask.GetLuaUserInfo()
	//
	util.InitMgo()
	util.InitOther() //
	//msgclient
	util.InitMsgClient(
		qu.ObjToString(util.CommServers["addr"]),
		qu.ObjToString(util.BidServers["addr"]),
		qu.ObjToString(util.CommServers["name"]),
		qu.ObjToString(util.BidServers["name"]),
	)
	//logs
	logger.SetRollingDaily("./logs", "task.log")
}

func main() {
	go clearLogs()
	c := cron.New()
	c.Start()
	c.AddFunc(util.RandomDataPushCron, timetask.GetSpiderWarnData) //数据维护平台-爬虫数据维护数据数据统计
	c.AddFunc(util.QyworkRemindCron, timetask.SendInfoToWxWork)    //企业微信日常警告
	c.AddFunc(util.StartTaskCron, luatask.StartTask)               //开始任务
	//c.AddFunc(CodeSummaryCron, SummaryCode)    //上架爬虫信息汇总
	ch := make(chan bool, 1)
	<-ch
}

func clearLogs() {
	fmt.Println("=======clearLogs========")
	timeInt := time.Now().AddDate(0, 0, -30).Unix()
	dirs, err := ioutil.ReadDir("./logs")
	if err == nil {
		for _, f := range dirs {
			fname := f.Name()
			logTimeStr := util.TimeReg.FindString(fname)
			if logTimeStr == "" {
				continue
			}
			logTimeInt, _ := time.ParseInLocation("2006-01-02", logTimeStr, time.Local)
			if logTimeInt.Unix() < timeInt {
				os.Remove("./logs/" + fname)
			}
		}
	}
	time.AfterFunc(24*time.Hour, clearLogs)
}
