Add: delete expired record for crontask

This commit is contained in:
2023-10-24 20:19:50 +08:00
parent 22e53ea20f
commit d9b1852a3b

View File

@@ -0,0 +1,81 @@
package delExpiredNeBackup
import (
"encoding/json"
"fmt"
"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
}
type BarParams struct {
Duration int `json:"duration"`
ColName string `json:"colName"`
TableName string `json:"tableName"`
}
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
var params BarParams
duration := 60
err := json.Unmarshal([]byte(sysJob.TargetParams), &params)
if err != nil {
return nil, err
}
//duration = params.Duration
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 := fmt.Sprintf("NOW()>ADDDATE(`%s`,interval %d day)", params.ColName, params.Duration)
affected, err := dborm.XormDeleteDataByWhere(where, params.TableName)
if err != nil {
// panic(fmt.Sprintf("Failed to XormDeleteDataByWhere:%v", err))
return nil, err
}
// 返回结果,用于记录执行结果
return map[string]any{
"msg": "sucess",
"affected": affected,
}, nil
}