jobs.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package jobs
  2. import (
  3. sysModel "cold-delivery/app/admin/model"
  4. "cold-delivery/common/global"
  5. "cold-delivery/db"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  9. "time"
  10. )
  11. // 需要将定义的struct 添加到字典中;
  12. // 字典 key 可以配置到 自动任务 调用目标 中;
  13. func InitJob() {
  14. jobList = map[string]JobsExec{
  15. //"ExamplesOne": ExamplesOne{},
  16. "CountIceRaftRecordFreezeDuration": CountIceRaftRecordFreezeDuration{},
  17. // ...
  18. }
  19. }
  20. // 新添加的job 必须按照以下格式定义,并实现Exec函数
  21. type ExamplesOne struct {
  22. }
  23. func (t ExamplesOne) Exec(arg interface{}) error {
  24. str := time.Now().Format(timeFormat) + " [INFO] JobCore ExamplesOne exec success"
  25. // TODO: 这里需要注意 Examples 传入参数是 string 所以 arg.(string);请根据对应的类型进行转化;
  26. switch arg.(type) {
  27. case string:
  28. if arg.(string) != "" {
  29. fmt.Println("string", arg.(string))
  30. fmt.Println(str, arg.(string))
  31. } else {
  32. fmt.Println("arg is nil")
  33. fmt.Println(str, "arg is nil")
  34. }
  35. break
  36. }
  37. return nil
  38. }
  39. // 统计冰排冷冻时长
  40. type CountIceRaftRecordFreezeDuration struct {
  41. }
  42. // 统计冰排冷冻时长
  43. func (t CountIceRaftRecordFreezeDuration) Exec(arg interface{}) error {
  44. iceRaftRecordList := make([]sysModel.IceRaftRecord, 0)
  45. // GetOrm 获取orm连接
  46. orm, _ := db.GetOrm(&gin.Context{})
  47. err := orm.Where("status = ? or status = ? ", sysModel.IceRaftRecordStatusFreezing, sysModel.IceRaftRecordStatusWaitUse).
  48. Find(&iceRaftRecordList).Error
  49. if err != nil {
  50. log.Errorf("db error: %s", err)
  51. return global.GetFailedErr
  52. }
  53. for _, record := range iceRaftRecordList {
  54. inTime := record.InStorageTime.Local()
  55. //if inTime.Add(time.Hour * time.Duration(record.FreezeClaim)).After(time.Now()) {
  56. if inTime.Add(time.Duration(float64(time.Hour) * float64(record.FreezeClaim))).After(time.Now()) {
  57. // 未达到冷冻时长 不修改状态 只修改时间
  58. record.FreezeDuration = int(time.Now().Sub(inTime).Minutes())
  59. } else {
  60. record.FreezeDuration = int(time.Now().Sub(inTime).Minutes())
  61. record.Status = sysModel.IceRaftRecordStatusWaitUse
  62. }
  63. if err := orm.Save(&record).Error; err != nil {
  64. log.Errorf("db error: %s", err)
  65. return global.UpdateFailedErr
  66. }
  67. }
  68. return nil
  69. }