init.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package crontask
  2. import (
  3. "context"
  4. "wechat-api/internal/svc"
  5. "github.com/robfig/cron/v3"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. )
  8. type CronTask struct {
  9. logx.Logger
  10. ctx context.Context
  11. svcCtx *svc.ServiceContext
  12. }
  13. func NewCronTask(ctx context.Context, svcCtx *svc.ServiceContext) *CronTask {
  14. return &CronTask{
  15. Logger: logx.WithContext(ctx),
  16. ctx: ctx,
  17. svcCtx: svcCtx,
  18. }
  19. }
  20. func ScheduleRun(c *cron.Cron, serverCtx *svc.ServiceContext) {
  21. l := NewCronTask(context.Background(), serverCtx)
  22. c.AddFunc("* * * * *", func() {
  23. l.sendMsg()
  24. })
  25. sendWx := NewCronTask(context.Background(), serverCtx)
  26. c.AddFunc("* * * * *", func() {
  27. sendWx.sendWx()
  28. })
  29. sendWxOnTimeout := NewCronTask(context.Background(), serverCtx)
  30. c.AddFunc("* * * * *", func() {
  31. sendWxOnTimeout.sendWxOnTimeout()
  32. })
  33. computeStatistic := NewCronTask(context.Background(), serverCtx)
  34. c.AddFunc("0 * * * *", func() {
  35. computeStatistic.computeStatistic()
  36. })
  37. contactForm := NewCronTask(context.Background(), serverCtx)
  38. c.AddFunc("10 0 * * *", func() {
  39. contactForm.analyze(nil)
  40. })
  41. contactForm2 := NewCronTask(context.Background(), serverCtx)
  42. c.AddFunc("0 8 * * *", func() {
  43. bot_wxid1 := "wxid_mqq8zf6kdofj22"
  44. contactForm2.analyze(&bot_wxid1)
  45. bot_wxid2 := "wxid_4p6046tox6pd22"
  46. contactForm2.analyze(&bot_wxid2)
  47. bot_wxid3 := "wxid_s1bes9nbk37v12"
  48. contactForm2.analyze(&bot_wxid3)
  49. })
  50. contactForm3 := NewCronTask(context.Background(), serverCtx)
  51. c.AddFunc("30 11 * * *", func() {
  52. bot_wxid1 := "wxid_mqq8zf6kdofj22"
  53. contactForm3.analyze(&bot_wxid1)
  54. bot_wxid2 := "wxid_4p6046tox6pd22"
  55. contactForm3.analyze(&bot_wxid2)
  56. bot_wxid3 := "wxid_s1bes9nbk37v12"
  57. contactForm3.analyze(&bot_wxid3)
  58. })
  59. contactForm4 := NewCronTask(context.Background(), serverCtx)
  60. c.AddFunc("0 17 * * *", func() {
  61. bot_wxid1 := "wxid_mqq8zf6kdofj22"
  62. contactForm4.analyze(&bot_wxid1)
  63. bot_wxid2 := "wxid_4p6046tox6pd22"
  64. contactForm4.analyze(&bot_wxid2)
  65. bot_wxid3 := "wxid_s1bes9nbk37v12"
  66. contactForm4.analyze(&bot_wxid3)
  67. })
  68. l = NewCronTask(context.Background(), serverCtx)
  69. c.AddFunc("*/30 * * * *", func() {
  70. l.wxAddFriend()
  71. })
  72. //l = NewCronTask(context.Background(), serverCtx)
  73. //c.AddFunc("* * * * *", func() {
  74. // MaxWorker := 10
  75. // MaxChannel := 3
  76. // l.compApiCallback(MaxWorker, MaxChannel)
  77. //})
  78. }