autoTask.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package service
  2. import (
  3. "github.com/robfig/cron/v3"
  4. log "github.com/sirupsen/logrus"
  5. "time"
  6. "youngee_b_api/db"
  7. )
  8. func AutoTask() error {
  9. c := cron.New(cron.WithSeconds())
  10. spec := "0 * */10 * * ?" //cron表达式,每半小时执行一次
  11. //spec := "0 */1 * * * ?" //cron表达式,每1分钟一次
  12. //spec := "*/10 * * * * ?" //cron表达式,每10秒一次
  13. _, err1 := c.AddFunc(spec, AutoTaskUpdateStatus)
  14. if err1 != nil {
  15. log.Println("service [AutoTaskUpdateStatus] error:", err1)
  16. return err1
  17. }
  18. _, err2 := c.AddFunc("@midnight", AutoTaskUpdateApplyTimes)
  19. if err2 != nil {
  20. log.Println("service [AutoTaskUpdateApplyTimes] error:", err2)
  21. return err2
  22. }
  23. _, err3 := c.AddFunc(spec, AutoTaskCompleteSelection)
  24. if err3 != nil {
  25. log.Println("service [AutoTaskCompleteSecTask] error:", err2)
  26. return err3
  27. }
  28. c.Start()
  29. return nil
  30. }
  31. func AutoTaskUpdateStatus() {
  32. err := db.AutoUpdateStatus()
  33. log.Println("AutoTaskUpdateStatus is running ,Time :", time.Now())
  34. if err != nil {
  35. log.Println("AutoTaskUpdateStatus error : ", err)
  36. }
  37. }
  38. func AutoTaskUpdateApplyTimes() {
  39. err := db.AutoUpdateApplyTimes()
  40. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  41. if err != nil {
  42. log.Println("AutoUpdateApplyTimes error : ", err)
  43. }
  44. }
  45. func AutoTaskCompleteSelection() {
  46. err := db.AutoCompleteSelection()
  47. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  48. if err != nil {
  49. log.Println("AutoUpdateApplyTimes error : ", err)
  50. }
  51. }