You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.2 KiB
65 lines
1.2 KiB
package cron
|
|
|
|
import (
|
|
"epur-pay/pkg/exchange"
|
|
"epur-pay/pkg/logger"
|
|
"epur-pay/pkg/mq"
|
|
"epur-pay/pkg/rds"
|
|
"epur-pay/pkg/utils"
|
|
"github.com/robfig/cron"
|
|
"gorm.io/gorm/clause"
|
|
"reflect"
|
|
"runtime"
|
|
)
|
|
|
|
var (
|
|
Cron *cron.Cron
|
|
)
|
|
|
|
func New() {
|
|
Cron = cron.New()
|
|
Cron.Start()
|
|
{
|
|
/*
|
|
每个月将任务表转移到历史数据表
|
|
*/
|
|
utils.Error(Cron.AddFunc("* * * * 7", MqStoreToHistory))
|
|
}
|
|
// 每天凌晨 12 点更新汇率
|
|
{
|
|
utils.Error(Cron.AddFunc("0 0 * * *", exchange.GetRate))
|
|
}
|
|
}
|
|
|
|
func MqStoreToHistory() {
|
|
|
|
defer func() {
|
|
err := recover()
|
|
if err != nil {
|
|
buf := make([]byte, 1<<16)
|
|
runtime.Stack(buf, true)
|
|
e := reflect.ValueOf(err)
|
|
logger.ErrorLogger.Errorln(e.String(), string(buf))
|
|
//p.E = errors.New(string(buf))
|
|
}
|
|
}()
|
|
|
|
rows := mq.AsyncTasks{}
|
|
|
|
utils.DbErrSkipRecordNotFound(rds.DB.Raw(`select * from async_task where create_time < ?`, utils.Time2StampSecond()-6*24*60*60).Scan(&rows).Error)
|
|
|
|
ids := []int64{}
|
|
|
|
for _, item := range rows {
|
|
ids = append(ids, item.Id)
|
|
}
|
|
|
|
if len(rows) > 0 {
|
|
utils.Error(rds.DB.Table("async_task_history").Clauses(clause.OnConflict{DoNothing: true}).Create(&rows).Error)
|
|
}
|
|
|
|
if len(ids) > 0 {
|
|
utils.Error(rds.DB.Exec(`delete from async_task where id in ?`, ids).Error)
|
|
}
|
|
}
|