autoTask.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 */30 * * * ?" //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. c.Start()
  24. return nil
  25. }
  26. func AutoTaskUpdateStatus() {
  27. err := db.AutoUpdateStatus()
  28. log.Println("AutoTaskUpdateStatus is running ,Time :", time.Now())
  29. if err != nil {
  30. log.Println("AutoTaskUpdateStatus error : ", err)
  31. }
  32. }
  33. func AutoTaskUpdateApplyTimes() {
  34. err := db.AutoUpdateApplyTimes()
  35. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  36. if err != nil {
  37. log.Println("AutoUpdateApplyTimes error : ", err)
  38. }
  39. }