Use the new cron lib.

This commit is contained in:
lishuang 2020-07-11 16:30:28 +08:00
parent 02e0f10016
commit 192df21da4
4 changed files with 46 additions and 29 deletions

View File

@ -31,7 +31,8 @@ func (this *TaskService) Init() {
//init the clean footprint task. //init the clean footprint task.
func (this *TaskService) InitCleanFootprintTask() { func (this *TaskService) InitCleanFootprintTask() {
expression := "0 10 0 * * ?" //use standard cron expression. 5 fields. ()
expression := "10 0 * * *"
cronJob := cron.New() cronJob := cron.New()
entryId, err := cronJob.AddFunc(expression, this.footprintService.CleanOldData) entryId, err := cronJob.AddFunc(expression, this.footprintService.CleanOldData)
core.PanicError(err) core.PanicError(err)
@ -43,7 +44,7 @@ func (this *TaskService) InitCleanFootprintTask() {
//init the elt task. //init the elt task.
func (this *TaskService) InitEtlTask() { func (this *TaskService) InitEtlTask() {
expression := "0 5 0 * * ?" expression := "5 0 * * *"
cronJob := cron.New() cronJob := cron.New()
entryId, err := cronJob.AddFunc(expression, this.dashboardService.Etl) entryId, err := cronJob.AddFunc(expression, this.dashboardService.Etl)
core.PanicError(err) core.PanicError(err)
@ -55,7 +56,7 @@ func (this *TaskService) InitEtlTask() {
//init the scan task. //init the scan task.
func (this *TaskService) InitScanTask() { func (this *TaskService) InitScanTask() {
expression := "0 5 0 * * ?" expression := "15 0 * * *"
cronJob := cron.New() cronJob := cron.New()
entryId, err := cronJob.AddFunc(expression, this.dashboardService.Etl) entryId, err := cronJob.AddFunc(expression, this.dashboardService.Etl)
core.PanicError(err) core.PanicError(err)

View File

@ -25,7 +25,7 @@ func (this *TankLogger) Init() {
this.openFile() this.openFile()
expression := "0 0 0 * * ?" expression := "0 0 * * *"
cronJob := cron.New() cronJob := cron.New()
entryId, err := cronJob.AddFunc(expression, this.maintain) entryId, err := cronJob.AddFunc(expression, this.maintain)
core.PanicError(err) core.PanicError(err)

View File

@ -1,13 +1,53 @@
package test package test
import ( import (
"fmt"
"github.com/eyebluecn/tank/code/core"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"log"
"testing" "testing"
"time"
) )
func TestEveryOneSecondCron(t *testing.T) {
i := 0
customMethod := true
var c *cron.Cron
var spec string
if customMethod {
//use custom cron. every 1 second.
c = cron.New(cron.WithSeconds())
spec = "*/1 * * * * ?"
} else {
c = cron.New()
spec = "@every 1s"
}
entryId, err := c.AddFunc(spec, func() {
i++
log.Println("cron running:", i)
if i == 5 {
return
}
})
fmt.Printf("entryId = %d\n", entryId)
core.PanicError(err)
c.Start()
time.Sleep(3500 * time.Millisecond)
if i != 3 {
t.Errorf("should be 3\n")
}
fmt.Printf("i = %d", i)
}
func TestValidateCron(t *testing.T) { func TestValidateCron(t *testing.T) {
spec := "*/1 * * * * ?" spec := "@every 1s"
_, err := cron.ParseStandard(spec) _, err := cron.ParseStandard(spec)
if err != nil { if err != nil {
t.Error(err) t.Error(err)

View File

@ -2,10 +2,7 @@ package test
import ( import (
"fmt" "fmt"
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/util" "github.com/eyebluecn/tank/code/tool/util"
"github.com/robfig/cron/v3"
"log"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -23,27 +20,6 @@ func TestHello(t *testing.T) {
} }
func TestCron(t *testing.T) {
i := 0
c := cron.New()
spec := "*/1 * * * * ?"
entryId, err := c.AddFunc(spec, func() {
i++
log.Println("cron running:", i)
if i == 3 {
panic("intent to panic.")
}
})
fmt.Printf("entryId = %d", entryId)
core.PanicError(err)
c.Start()
time.Sleep(3 * time.Second)
}
func TestDayAgo(t *testing.T) { func TestDayAgo(t *testing.T) {
dayAgo := time.Now() dayAgo := time.Now()