Finish the cron things.

This commit is contained in:
zicla
2019-04-27 14:56:27 +08:00
parent 9d35088cce
commit 14b6a74666
4 changed files with 81 additions and 33 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/eyebluecn/tank/code/config"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/robfig/cron"
"net/http"
"time"
)
@ -88,3 +89,31 @@ func (this *FootprintService) Trace(writer http.ResponseWriter, request *http.Re
this.logger.Info("Ip:%s Host:%s Uri:%s Params:%s Cost:%d", footprint.Ip, footprint.Host, footprint.Uri, paramsString, int64(duration/time.Millisecond))
}
//系统启动,数据库配置完毕后会调用该方法
func (this *FootprintService) Bootstrap() {
//每日00:10 删除8日之前的访问数据
expression := "0 10 0 * * ?"
cronJob := cron.New()
err := cronJob.AddFunc(expression, this.cleanOldData)
util.PanicError(err)
cronJob.Start()
this.logger.Info("[cron job] 每日00:10 删除8日之前的访问数据")
//立即执行一次数据清洗任务
go util.SafeMethod(this.cleanOldData)
}
//定期删除8日前的数据。
func (this *FootprintService) cleanOldData() {
day8Ago := time.Now()
day8Ago = day8Ago.AddDate(0, 0, -8)
day8Ago = util.FirstSecondOfDay(day8Ago)
this.logger.Info("删除%s之前的访问数据", util.ConvertTimeToDateTimeString(day8Ago))
this.footprintDao.DeleteByCreateTimeBefore(day8Ago)
}