Finish the cron things.
This commit is contained in:
parent
9d35088cce
commit
14b6a74666
@ -14,8 +14,6 @@ type DashboardService struct {
|
|||||||
matterDao *MatterDao
|
matterDao *MatterDao
|
||||||
imageCacheDao *ImageCacheDao
|
imageCacheDao *ImageCacheDao
|
||||||
userDao *UserDao
|
userDao *UserDao
|
||||||
//每天凌晨定时整理器
|
|
||||||
maintainTimer *time.Timer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//初始化方法
|
//初始化方法
|
||||||
@ -53,43 +51,23 @@ func (this *DashboardService) Init() {
|
|||||||
//系统启动,数据库配置完毕后会调用该方法
|
//系统启动,数据库配置完毕后会调用该方法
|
||||||
func (this *DashboardService) Bootstrap() {
|
func (this *DashboardService) Bootstrap() {
|
||||||
|
|
||||||
//立即执行数据清洗任务
|
//每日00:05分清洗离线数据
|
||||||
go util.SafeMethod(this.maintain)
|
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
|
go util.SafeMethod(this.etl)
|
||||||
c := cron.New()
|
|
||||||
|
|
||||||
//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() {
|
||||||
|
|
||||||
//准备好下次维护日志的时间。
|
this.logger.Info("每日定时数据清洗")
|
||||||
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)
|
|
||||||
})
|
|
||||||
|
|
||||||
//准备日期开始结尾
|
//准备日期开始结尾
|
||||||
startTime := util.FirstSecondOfDay(util.Yesterday())
|
startTime := util.FirstSecondOfDay(util.Yesterday())
|
||||||
|
@ -119,6 +119,12 @@ func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.T
|
|||||||
return int64(cost)
|
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() {
|
func (this *FootprintDao) Cleanup() {
|
||||||
this.logger.Info("[FootprintDao]执行清理:清除数据库中所有Footprint记录。")
|
this.logger.Info("[FootprintDao]执行清理:清除数据库中所有Footprint记录。")
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/eyebluecn/tank/code/config"
|
"github.com/eyebluecn/tank/code/config"
|
||||||
"github.com/eyebluecn/tank/code/tool/util"
|
"github.com/eyebluecn/tank/code/tool/util"
|
||||||
|
"github.com/robfig/cron"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"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))
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/eyebluecn/tank/code/tool/util"
|
||||||
|
"github.com/robfig/cron"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHello(t *testing.T) {
|
func TestHello(t *testing.T) {
|
||||||
@ -12,3 +16,34 @@ func TestHello(t *testing.T) {
|
|||||||
fmt.Printf("%v", split)
|
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