fix: add new crontask module
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"ems.agt/src/framework/middleware"
|
||||
"ems.agt/src/framework/middleware/security"
|
||||
"ems.agt/src/modules/common"
|
||||
"ems.agt/src/modules/crontask"
|
||||
"ems.agt/src/modules/monitor"
|
||||
"ems.agt/src/modules/system"
|
||||
|
||||
@@ -116,4 +117,5 @@ func initModulesRoute(app *gin.Engine) {
|
||||
common.Setup(app)
|
||||
monitor.Setup(app)
|
||||
system.Setup(app)
|
||||
crontask.Setup(app)
|
||||
}
|
||||
|
||||
23
src/modules/crontask/crontask.go
Normal file
23
src/modules/crontask/crontask.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package crontask
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/cron"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/modules/crontask/tasks"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Setup 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> monitor 模块路由")
|
||||
|
||||
// 启动时需要的初始参数
|
||||
InitCronQueue()
|
||||
|
||||
}
|
||||
|
||||
// InitCronQueue 初始定时任务队列
|
||||
func InitCronQueue() {
|
||||
cron.CreateQueue("tasks", tasks.NewProcessor)
|
||||
}
|
||||
63
src/modules/crontask/tasks/tasks.go
Normal file
63
src/modules/crontask/tasks/tasks.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"ems.agt/lib/dborm"
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/src/framework/cron"
|
||||
)
|
||||
|
||||
var NewProcessor = &BarProcessor{
|
||||
progress: 0,
|
||||
count: 0,
|
||||
}
|
||||
|
||||
// bar 队列任务处理
|
||||
type BarProcessor struct {
|
||||
// 任务进度
|
||||
progress int
|
||||
// 执行次数
|
||||
count int
|
||||
}
|
||||
|
||||
func (s *BarProcessor) Execute(data any) (any, error) {
|
||||
log.Infof("执行 %d 次,上次进度: %d ", s.count, s.progress)
|
||||
s.count++
|
||||
|
||||
options := data.(cron.JobData)
|
||||
sysJob := options.SysJob
|
||||
log.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)
|
||||
|
||||
// // 实现任务处理逻辑
|
||||
// i := 0
|
||||
// s.progress = i
|
||||
// for i < 5 {
|
||||
// // 获取任务进度
|
||||
// progress := s.progress
|
||||
// log.Infof("jonId: %s => 任务进度:%d", sysJob.JobID, progress)
|
||||
// // 延迟响应
|
||||
// time.Sleep(time.Second * 2)
|
||||
// // 程序中途执行错误
|
||||
// if i == 3 {
|
||||
// // arr := [1]int{1}
|
||||
// // arr[i] = 3
|
||||
// // fmt.Println(arr)
|
||||
// // return "i = 3"
|
||||
// panic("程序中途执行错误")
|
||||
// }
|
||||
// i++
|
||||
// // 改变任务进度
|
||||
// s.progress = i
|
||||
// }
|
||||
where := "NOW()>ADDDATE(`create_time`,interval IFNULL((SELECT `value` FROM config WHERE config_tag='BackUpSaveTime'),30) day)"
|
||||
affected, err := dborm.XormDeleteDataByWhere(where, "ne_backup")
|
||||
if err != nil {
|
||||
// panic(fmt.Sprintf("Failed to XormDeleteDataByWhere:%v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回结果,用于记录执行结果
|
||||
return map[string]any{
|
||||
"err": err.Error(),
|
||||
"affected": affected,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user