Finish the cron things.
This commit is contained in:
parent
9d35088cce
commit
14b6a74666
@ -14,8 +14,6 @@ type DashboardService struct {
|
||||
matterDao *MatterDao
|
||||
imageCacheDao *ImageCacheDao
|
||||
userDao *UserDao
|
||||
//每天凌晨定时整理器
|
||||
maintainTimer *time.Timer
|
||||
}
|
||||
|
||||
//初始化方法
|
||||
@ -53,43 +51,23 @@ func (this *DashboardService) Init() {
|
||||
//系统启动,数据库配置完毕后会调用该方法
|
||||
func (this *DashboardService) Bootstrap() {
|
||||
|
||||
//立即执行数据清洗任务
|
||||
go util.SafeMethod(this.maintain)
|
||||
//每日00:05分清洗离线数据
|
||||
expression := "0 5 0 * * ?"
|
||||
cronJob := cron.New()
|
||||
err := cronJob.AddFunc(expression, this.etl)
|
||||
util.PanicError(err)
|
||||
cronJob.Start()
|
||||
this.logger.Info("[cron job] 每日00:05清洗离线数据")
|
||||
|
||||
//每天00:05执行数据清洗任务
|
||||
i := 0
|
||||
c := cron.New()
|
||||
//立即执行一次数据清洗任务
|
||||
go util.SafeMethod(this.etl)
|
||||
|
||||
//AddFunc
|
||||
spec := "*/5 * * * * ?"
|
||||
err := c.AddFunc(spec, func() {
|
||||
i++
|
||||
this.logger.Info("cron running: %d", i)
|
||||
})
|
||||
this.PanicError(err)
|
||||
|
||||
//AddJob方法
|
||||
//c.AddJob(spec, TestJob{})
|
||||
//c.AddJob(spec, Test2Job{})
|
||||
|
||||
//启动计划任务
|
||||
c.Start()
|
||||
|
||||
//关闭着计划任务, 但是不能关闭已经在执行中的任务.
|
||||
defer c.Stop()
|
||||
}
|
||||
|
||||
//每日清洗离线数据表。
|
||||
func (this *DashboardService) maintain() {
|
||||
func (this *DashboardService) etl() {
|
||||
|
||||
//准备好下次维护日志的时间。
|
||||
now := time.Now()
|
||||
nextTime := util.FirstMinuteOfDay(util.Tomorrow())
|
||||
duration := nextTime.Sub(now)
|
||||
this.logger.Info("每日数据汇总,下次时间:%s ", util.ConvertTimeToDateTimeString(nextTime))
|
||||
this.maintainTimer = time.AfterFunc(duration, func() {
|
||||
go util.SafeMethod(this.maintain)
|
||||
})
|
||||
this.logger.Info("每日定时数据清洗")
|
||||
|
||||
//准备日期开始结尾
|
||||
startTime := util.FirstSecondOfDay(util.Yesterday())
|
||||
|
@ -119,6 +119,12 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T
|
||||
return int64(cost)
|
||||
}
|
||||
|
||||
//删除某个时刻之前的记录
|
||||
func (this *FootprintDao) DeleteByCreateTimeBefore(createTime time.Time) {
|
||||
db := CONTEXT.DB.Where("create_time < ?", createTime).Delete(Footprint{})
|
||||
this.PanicError(db.Error)
|
||||
}
|
||||
|
||||
//执行清理操作
|
||||
func (this *FootprintDao) Cleanup() {
|
||||
this.logger.Info("[FootprintDao]执行清理:清除数据库中所有Footprint记录。")
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -2,8 +2,12 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
"github.com/robfig/cron"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
@ -12,3 +16,34 @@ func TestHello(t *testing.T) {
|
||||
fmt.Printf("%v", split)
|
||||
|
||||
}
|
||||
|
||||
//测试cron表达式
|
||||
func TestCron(t *testing.T) {
|
||||
|
||||
i := 0
|
||||
c := cron.New()
|
||||
spec := "*/1 * * * * ?"
|
||||
err := c.AddFunc(spec, func() {
|
||||
i++
|
||||
log.Println("cron running:", i)
|
||||
})
|
||||
util.PanicError(err)
|
||||
|
||||
c.Start()
|
||||
|
||||
//当前线程阻塞 20s
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
}
|
||||
|
||||
//测试 时间
|
||||
func TestDayAgo(t *testing.T) {
|
||||
|
||||
dayAgo := time.Now()
|
||||
dayAgo = dayAgo.AddDate(0, 0, -8)
|
||||
|
||||
thenDay := util.FirstSecondOfDay(dayAgo)
|
||||
|
||||
fmt.Printf("%s\n", util.ConvertTimeToDateTimeString(thenDay))
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user