Add the task service. Ready to tidy the go modules.
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/tool/result"
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
@ -143,11 +144,21 @@ func (this *PreferenceController) EditPreviewConfig(writer http.ResponseWriter,
|
||||
|
||||
func (this *PreferenceController) EditScanConfig(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||
|
||||
scanConfig := request.FormValue("scanConfig")
|
||||
scanConfigStr := request.FormValue("scanConfig")
|
||||
|
||||
preference := this.preferenceDao.Fetch()
|
||||
|
||||
preference.ScanConfig = scanConfig
|
||||
scanConfig := &ScanConfig{}
|
||||
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(scanConfigStr), &scanConfig)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//validate the scan config.
|
||||
if scanConfig.Enable {
|
||||
//validate cron.
|
||||
|
||||
}
|
||||
|
||||
preference = this.preferenceService.Save(preference)
|
||||
|
||||
|
@ -64,14 +64,3 @@ func (this *Preference) FetchScanConfig() *ScanConfig {
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
//set the scan config
|
||||
func (this *Preference) SetScanConfig(scanConfigJson string) {
|
||||
|
||||
b, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(scanConfigJson)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
this.ScanConfig = string(b)
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package rest
|
||||
|
||||
import (
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/robfig/cron"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
// system tasks service
|
||||
@ -31,37 +31,37 @@ func (this *TaskService) Init() {
|
||||
//init the clean footprint task.
|
||||
func (this *TaskService) InitCleanFootprintTask() {
|
||||
|
||||
this.logger.Info("[cron job] Every day 00:10 delete Footprint data of 8 days ago.")
|
||||
expression := "0 10 0 * * ?"
|
||||
cronJob := cron.New()
|
||||
err := cronJob.AddFunc(expression, this.footprintService.CleanOldData)
|
||||
entryId, err := cronJob.AddFunc(expression, this.footprintService.CleanOldData)
|
||||
core.PanicError(err)
|
||||
cronJob.Start()
|
||||
|
||||
this.logger.Info("[cron job] Every day 00:10 delete Footprint data of 8 days ago. entryId = %d", entryId)
|
||||
}
|
||||
|
||||
//init the elt task.
|
||||
func (this *TaskService) InitEtlTask() {
|
||||
|
||||
this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data.")
|
||||
expression := "0 5 0 * * ?"
|
||||
cronJob := cron.New()
|
||||
err := cronJob.AddFunc(expression, this.dashboardService.Etl)
|
||||
entryId, err := cronJob.AddFunc(expression, this.dashboardService.Etl)
|
||||
core.PanicError(err)
|
||||
cronJob.Start()
|
||||
|
||||
this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data. entryId = %d", entryId)
|
||||
}
|
||||
|
||||
//init the scan task.
|
||||
func (this *TaskService) InitScanTask() {
|
||||
|
||||
this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data.")
|
||||
expression := "0 5 0 * * ?"
|
||||
cronJob := cron.New()
|
||||
err := cronJob.AddFunc(expression, this.dashboardService.Etl)
|
||||
entryId, err := cronJob.AddFunc(expression, this.dashboardService.Etl)
|
||||
core.PanicError(err)
|
||||
cronJob.Start()
|
||||
|
||||
this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data. entryId = %d", entryId)
|
||||
}
|
||||
|
||||
func (this *TaskService) Bootstrap() {
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/tool/util"
|
||||
"github.com/robfig/cron"
|
||||
"github.com/robfig/cron/v3"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
@ -25,13 +25,13 @@ func (this *TankLogger) Init() {
|
||||
|
||||
this.openFile()
|
||||
|
||||
this.Info("[cron job] Every day 00:00 maintain log file.")
|
||||
expression := "0 0 0 * * ?"
|
||||
cronJob := cron.New()
|
||||
err := cronJob.AddFunc(expression, this.maintain)
|
||||
entryId, err := cronJob.AddFunc(expression, this.maintain)
|
||||
core.PanicError(err)
|
||||
cronJob.Start()
|
||||
|
||||
this.Info("[cron job] Every day 00:00 maintain log file. entryId = %d", entryId)
|
||||
}
|
||||
|
||||
func (this *TankLogger) Destroy() {
|
||||
|
18
code/test/cron_test.go
Normal file
18
code/test/cron_test.go
Normal file
@ -0,0 +1,18 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateCron(t *testing.T) {
|
||||
|
||||
spec := "*/1 * * * * ?"
|
||||
_, err := cron.Parse(spec)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("%s passed\n", spec)
|
||||
}
|
||||
|
||||
}
|
@ -31,7 +31,7 @@ func TestCron(t *testing.T) {
|
||||
err := c.AddFunc(spec, func() {
|
||||
i++
|
||||
log.Println("cron running:", i)
|
||||
if i == 2 {
|
||||
if i == 3 {
|
||||
panic("intent to panic.")
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user