autoTask.go 1.0 KB

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