delay.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/videoup/model/archive"
  7. "go-common/app/job/main/videoup/model/message"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. )
  12. func (s *Service) delayproc() {
  13. defer s.wg.Done()
  14. for {
  15. if s.closed {
  16. return
  17. }
  18. s.pubDelay()
  19. time.Sleep(1 * time.Minute)
  20. }
  21. }
  22. func (s *Service) pubDelay() {
  23. var (
  24. c = context.TODO()
  25. delays []*archive.Delay
  26. err error
  27. now = time.Now()
  28. )
  29. if delays, err = s.arc.NowDelays(c, now); err != nil {
  30. log.Error("s.arc.NowDelays err(%v)", err)
  31. return
  32. }
  33. if len(delays) == 0 {
  34. log.Info("%s nothing need delay pub", now.Format("2006-01-02 15:04:05"))
  35. return
  36. }
  37. var delayIds = make([]int64, 0, len(delays))
  38. for _, v := range delays {
  39. log.Info("archive(%d) type(%d) begin delay pub", v.Aid, v.Type)
  40. var (
  41. a *archive.Archive
  42. tx *sql.Tx
  43. )
  44. delayIds = append(delayIds, v.ID)
  45. if a, err = s.arc.Archive(c, v.Aid); err != nil || a == nil {
  46. log.Error("s.arc.Archive(%d) error(%v)", v.Aid, err)
  47. continue
  48. }
  49. //定时发布稿件 到临界点必须 state=-40
  50. if v.Type == archive.DelayTypeForUser && a.State != archive.StateForbidUserDelay {
  51. log.Info("archive(%d) type(%d) state(%d) delay failed", v.Aid, v.Type, a.State)
  52. now := now.Unix()
  53. s.msg.Send(c, "1_7_9", "您的稿件定时发布失败", fmt.Sprintf(`您的稿件《%s》(av%d)未能成功发布。原因:未能及时通过审核 您可以在稿件编辑页面重新编辑定时发布的时间,为此带来的麻烦,深感抱歉。#{点击进入编辑>>}{"http://member.bilibili.com/v/video/submit.html?type=edit&aid=%d"}`,
  54. a.Title, a.Aid, a.Aid), a.Mid, now)
  55. s.msg.Send(c, "113_1_1", "您的视频定时发布失败", fmt.Sprintf(`您的视频《%s》(av%d)定时发布失败。原因:未能及时通过审核。您可在稿件编辑页重新编辑定时发布时间。为此带来的麻烦,深感抱歉。#{点击进行编辑>>}{"https://member.bilibili.com/v/video/submit.html?type=edit&aid=%d"}`,
  56. a.Title, a.Aid, a.Aid), a.Mid, now)
  57. continue
  58. }
  59. if v.Type == archive.DelayTypeForUser && a.State == archive.StateForbidUserDelay {
  60. a.State = archive.StateOpen
  61. var addit *archive.Addit
  62. if addit, err = s.arc.Addit(c, a.Aid); err != nil && addit != nil && addit.MissionID > 0 {
  63. a.State = archive.StateOpen
  64. } else if !s.isWhite(a.Mid) && !s.isBlack(a.Mid) {
  65. if pfl, _ := s.profile(c, a.Mid); pfl != nil && pfl.Follower < int64(s.fansCache) && s.isRoundType(a.TypeID) {
  66. a.State = archive.StateOrange // NOTE: auto open must
  67. }
  68. }
  69. } else {
  70. log.Info("archive(%d) type(%d) state(%d) delay failed", v.Aid, v.Type, v.State)
  71. continue
  72. }
  73. if tx, err = s.arc.BeginTran(c); err != nil {
  74. log.Error("s.arc.BeginTran error(%v)", err)
  75. continue
  76. }
  77. firstPass := false
  78. if firstPass, err = s.txUpArcState(c, tx, a.Aid, a.State); err != nil {
  79. tx.Rollback()
  80. continue
  81. }
  82. log.Info("archive(%d) type(%d) delay upState(%d)", v.Aid, v.Type, a.State)
  83. if firstPass {
  84. a.PTime = xtime.Time(now.Unix())
  85. if _, err = s.arc.TxUpPTime(tx, a.Aid, now); err != nil {
  86. tx.Rollback()
  87. continue
  88. }
  89. log.Info("archive(%d) type(%d) delay upPTime(%d)", v.Aid, v.Type, a.PTime)
  90. }
  91. if archive.NormalState(a.State) {
  92. var round int8
  93. if round, err = s.tranRound(c, tx, a); err != nil {
  94. tx.Rollback()
  95. continue
  96. }
  97. a.Round = round
  98. if err = s.tranArchiveOper(tx, a); err != nil {
  99. tx.Rollback()
  100. continue
  101. }
  102. } else {
  103. if err = s.tranArchiveOper(tx, a); err != nil {
  104. tx.Rollback()
  105. return
  106. }
  107. }
  108. if err = tx.Commit(); err != nil {
  109. log.Error("tx.commit error(%v)", err)
  110. continue
  111. }
  112. if archive.NormalState(a.State) {
  113. s.syncBVC(c, a)
  114. s.changeMission(c, a, 0)
  115. now := now.Unix()
  116. s.msg.Send(c, "1_7_10", "您的稿件已经发布", fmt.Sprintf(`您的稿件《%s》(av%d)已经发布成功,#{点击查看>>}{"http://www.bilibili.com/video/av%d/"} `,
  117. a.Title, a.Aid, a.Aid), a.Mid, now)
  118. s.msg.Send(c, "113_1_1", "您的视频定时发布成功", fmt.Sprintf(`您的视频《%s》(av%d)已经发布成功,#{点击查看>>}{"http://www.bilibili.com/video/av%d/"} `,
  119. a.Title, a.Aid, a.Aid), a.Mid, now)
  120. s.sendAuditMsg(c, message.RouteDelayOpen, a.Aid)
  121. if is, _ := s.IsUpperFirstPass(c, a.Mid, a.Aid); is {
  122. go s.sendNewUpperMsg(c, a.Mid, a.Aid)
  123. }
  124. }
  125. log.Info("archive(%d) type(%d) end delay pub", v.Aid, v.Type)
  126. }
  127. s.arc.DelDelayByIds(c, delayIds)
  128. }