autoTask.go 1.9 KB

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