package jobs import ( sysModel "cold-delivery/app/admin/model" "cold-delivery/common/global" "cold-delivery/db" "fmt" "github.com/gin-gonic/gin" log "gogs.baozhida.cn/zoie/OAuth-core/logger" "time" ) // 需要将定义的struct 添加到字典中; // 字典 key 可以配置到 自动任务 调用目标 中; func InitJob() { jobList = map[string]JobsExec{ //"ExamplesOne": ExamplesOne{}, "CountIceRaftRecordFreezeDuration": CountIceRaftRecordFreezeDuration{}, // ... } } // 新添加的job 必须按照以下格式定义,并实现Exec函数 type ExamplesOne struct { } func (t ExamplesOne) Exec(arg interface{}) error { str := time.Now().Format(timeFormat) + " [INFO] JobCore ExamplesOne exec success" // TODO: 这里需要注意 Examples 传入参数是 string 所以 arg.(string);请根据对应的类型进行转化; switch arg.(type) { case string: if arg.(string) != "" { fmt.Println("string", arg.(string)) fmt.Println(str, arg.(string)) } else { fmt.Println("arg is nil") fmt.Println(str, "arg is nil") } break } return nil } // 统计冰排冷冻时长 type CountIceRaftRecordFreezeDuration struct { } // 统计冰排冷冻时长 func (t CountIceRaftRecordFreezeDuration) Exec(arg interface{}) error { iceRaftRecordList := make([]sysModel.IceRaftRecord, 0) // GetOrm 获取orm连接 orm, _ := db.GetOrm(&gin.Context{}) err := orm.Where("status = ? or status = ? ", sysModel.IceRaftRecordStatusFreezing, sysModel.IceRaftRecordStatusWaitUse). Find(&iceRaftRecordList).Error if err != nil { log.Errorf("db error: %s", err) return global.GetFailedErr } for _, record := range iceRaftRecordList { inTime := record.InStorageTime.Local() //if inTime.Add(time.Hour * time.Duration(record.FreezeClaim)).After(time.Now()) { if inTime.Add(time.Duration(float64(time.Hour) * float64(record.FreezeClaim))).After(time.Now()) { // 未达到冷冻时长 不修改状态 只修改时间 record.FreezeDuration = int(time.Now().Sub(inTime).Minutes()) } else { record.FreezeDuration = int(time.Now().Sub(inTime).Minutes()) record.Status = sysModel.IceRaftRecordStatusWaitUse } if err := orm.Save(&record).Error; err != nil { log.Errorf("db error: %s", err) return global.UpdateFailedErr } } return nil }