upper.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package ugc
  2. import (
  3. "context"
  4. "time"
  5. appDao "go-common/app/job/main/tv/dao/app"
  6. ugcMdl "go-common/app/job/main/tv/model/ugc"
  7. account "go-common/app/service/main/account/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _arcPiece = 20
  13. _upName = 1
  14. _upFace = 2
  15. )
  16. // refreshUpproc refreshes the upper info regularly
  17. func (s *Service) refreshUpproc() {
  18. var c = context.Background()
  19. for {
  20. time.Sleep(time.Duration(s.c.UgcSync.Frequency.UpperRefresh))
  21. s.refreshUp(c, true)
  22. log.Info("RefreshUpper Succ")
  23. }
  24. }
  25. // refreshUp picks all the upper in our DB and compare them with RPC result to refresh if different
  26. func (s *Service) refreshUp(ctx context.Context, refresh bool) {
  27. var (
  28. count int64
  29. nbPiece int
  30. err error
  31. maxID int64
  32. activeUps = make(map[int64]int)
  33. )
  34. if count, err = s.upDao.CountUP(ctx); err != nil {
  35. log.Error("[refreshUpper] CountUP error [%v]", err)
  36. return
  37. }
  38. nbPiece = appDao.NumPce(int(count), _arcPiece)
  39. log.Info("[refreshUpper] Numero Piece %d", nbPiece)
  40. for i := 0; i < nbPiece; i++ {
  41. res, newMaxID, err := s.upDao.PickUppers(ctx, maxID, _arcPiece)
  42. if err != nil {
  43. log.Error("[refreshUpper] Pick Piece %d Error, Ignore it", i)
  44. continue
  45. }
  46. if newMaxID <= maxID {
  47. log.Error("MaxID is not increasing! [%d,%d]", newMaxID, maxID)
  48. return
  49. }
  50. maxID = newMaxID
  51. for _, v := range res { // travel the 50 mids
  52. activeUps[v] = 1
  53. if refresh {
  54. s.upCheck(ctx, v)
  55. }
  56. }
  57. }
  58. if len(activeUps) > 0 {
  59. s.activeUps = activeUps
  60. log.Info("[refreshUpper] ActiveUps Len %d", len(activeUps))
  61. }
  62. }
  63. // upCheck checks the upper remote and local, modify if different
  64. func (s *Service) upCheck(ctx context.Context, v int64) (err error) {
  65. var (
  66. upCMS *ugcMdl.Upper
  67. upRPC *account.Card
  68. )
  69. if upCMS, err = s.upDao.LoadUpMeta(ctx, v); err != nil { // load local upper data
  70. log.Warn("[refreshUpper] LoadUpMeta Mid %d, Err %d", v, err)
  71. return
  72. }
  73. if upRPC, err = s.upDao.Card3(ctx, v); err != nil { // load remote upper data
  74. log.Warn("[refreshUpper] Card3 Mid %d, Err %d", v, err)
  75. return
  76. }
  77. if err = s.upModify(ctx, upRPC, upCMS); err != nil {
  78. log.Warn("[refreshUpper] upModify Mid %d, Err %d", v, err)
  79. }
  80. return
  81. }
  82. // upModify checks whether the upper's info has been modified or not, if yes, refresh it in DB and MC
  83. func (s *Service) upModify(ctx context.Context, upRPC *account.Card, upCMS *ugcMdl.Upper) (err error) {
  84. fs, ns := upCMS.IsSame(upRPC.Name, upRPC.Face)
  85. if fs && ns { // compare, if same, just jump to the next upper
  86. return
  87. }
  88. if !fs {
  89. log.Info("Mid %d Face Modified, Old: %s, New %s", upRPC.Mid, upCMS.OriFace, upRPC.Face)
  90. req := &ugcMdl.ReqSetUp{
  91. Value: upRPC.Face,
  92. MID: upRPC.Mid,
  93. UpType: _upFace,
  94. }
  95. if err = s.upDao.RefreshUp(ctx, req); err != nil {
  96. log.Error("RefreshUp Req %v, Err %v", req, err)
  97. return
  98. }
  99. }
  100. if !ns {
  101. log.Info("Mid %d Name Modified, Old: %s, New %s", upRPC.Mid, upCMS.OriName, upRPC.Name)
  102. req := &ugcMdl.ReqSetUp{
  103. Value: upRPC.Name,
  104. MID: upRPC.Mid,
  105. UpType: _upName,
  106. }
  107. if err = s.upDao.RefreshUp(ctx, req); err != nil {
  108. log.Error("RefreshUp Req %v, Err %v", req, err)
  109. return
  110. }
  111. }
  112. err = s.upDao.SendUpper(ctx, upRPC.Mid) // only update once if both face and name are modified
  113. return
  114. }
  115. // delete Upper treatment proc
  116. func (s *Service) delUpproc() {
  117. defer s.waiter.Done()
  118. for {
  119. if s.daoClosed {
  120. log.Info("delUpproc DB closed!")
  121. return
  122. }
  123. // build the skeleton, arc + video data
  124. cMid, err := s.dao.DeletedUp(ctx)
  125. if err != nil && err != sql.ErrNoRows {
  126. log.Error("DeletedUp Error %v", err)
  127. appDao.PromError("DelUp:Err")
  128. time.Sleep(time.Duration(s.c.UgcSync.Frequency.SyncFre))
  129. continue
  130. }
  131. if err == sql.ErrNoRows || cMid == 0 {
  132. log.Info("SyncDelAid No Data to Sync")
  133. time.Sleep(time.Duration(s.c.UgcSync.Frequency.SyncFre))
  134. continue
  135. }
  136. if err = s.delUp(cMid); err != nil {
  137. appDao.PromError("DelUp:Err")
  138. log.Error("delUp error %v, mid %d", err, cMid)
  139. time.Sleep(time.Duration(s.c.UgcSync.Frequency.SyncFre))
  140. continue
  141. }
  142. appDao.PromInfo("DelUp:Succ")
  143. }
  144. }
  145. // delete Upper treatment
  146. func (s *Service) delUp(mid int64) (err error) {
  147. var (
  148. aids []int64
  149. count int64
  150. page int
  151. )
  152. if count, err = s.dao.CountUpArcs(ctx, mid); err != nil {
  153. s.delUpErr(mid, "CountUpArcs Mid %d, Err %v", err)
  154. return
  155. }
  156. page = appDao.NumPce(int(count), _arcPiece)
  157. log.Info("Ready to Remove Mid %d Arcs, Count: %d, Page: %d", mid, count, page)
  158. for i := 0; i < page; i++ {
  159. log.Info("Operating DelUp Mid %d Page %d", mid, i+1)
  160. if aids, err = s.dao.UpArcs(ctx, mid); err != nil && err != sql.ErrNoRows {
  161. return
  162. }
  163. if err == sql.ErrNoRows || len(aids) == 0 { // means end of the loup
  164. break
  165. }
  166. for _, v := range aids {
  167. if err = s.delArc(v); err != nil {
  168. s.delUpErr(mid, "delArc Mid %d, Err %v", err)
  169. return
  170. }
  171. }
  172. }
  173. // change the upper's status to tell it's finish
  174. if err = s.dao.FinishDelUp(ctx, mid); err != nil {
  175. s.delUpErr(mid, "delArc Mid %d, Err %v", err)
  176. return
  177. }
  178. log.Info("DelUp Mid %d Succ, Count %d, Page %d", mid, count, page)
  179. return
  180. }
  181. // delUpErr: it logs the error and postpone the videos for the next submit
  182. func (s *Service) delUpErr(mid int64, fmt string, err error) {
  183. s.dao.PpDelUp(ctx, mid)
  184. log.Error(fmt, mid, err)
  185. }
  186. // syncUpproc picks all the submit=1 uppers and updates their archives' submit to 1
  187. func (s *Service) syncUpproc() {
  188. defer s.waiter.Done()
  189. for {
  190. if s.daoClosed {
  191. log.Info("syncUpproc DB closed!")
  192. return
  193. }
  194. mids, err := s.upDao.TosyncUps(ctx) // pick to sync mids
  195. if err != nil {
  196. log.Error("syncUpproc Error %v", err)
  197. time.Sleep(time.Duration(s.c.UgcSync.Frequency.SyncFre))
  198. continue
  199. }
  200. if len(mids) == 0 {
  201. log.Info("No Upper to Sync")
  202. time.Sleep(time.Duration(s.c.UgcSync.Frequency.SyncFre))
  203. continue
  204. }
  205. log.Info("syncUpproc treats %d uppers", len(mids))
  206. for _, v := range mids { // updates these uppers' archives' submit to 1
  207. if err = s.submitUps(ctx, v); err != nil {
  208. log.Error("syncUpproc updates Archive error %v, mid %d", err, v)
  209. continue
  210. }
  211. if err = s.upDao.FinsyncUps(ctx, v); err != nil {
  212. log.Error("syncUpproc finish upper error %v, mid %d", err, v)
  213. }
  214. }
  215. }
  216. }
  217. func (s *Service) submitUps(ctx context.Context, mid int64) (err error) {
  218. var (
  219. count int64
  220. )
  221. if count, err = s.dao.CountUpArcs(ctx, mid); err != nil {
  222. s.delUpErr(mid, "CountUpArcs Mid %d, Err %v", err)
  223. return
  224. }
  225. page := appDao.NumPce(int(count), _arcPiece)
  226. log.Info("Ready to Submit Mid %d Arcs, Count: %d, Page: %d", mid, count, page)
  227. for i := 0; i < page; i++ {
  228. log.Info("Operating DelUp Mid %d Page %d", mid, i+1)
  229. var aids []int64
  230. if aids, err = s.dao.UpArcs(ctx, mid); err != nil && err != sql.ErrNoRows {
  231. return
  232. }
  233. if err == sql.ErrNoRows || len(aids) == 0 { // means end of the loup
  234. break
  235. }
  236. s.modArcCh <- aids // put them into the channel
  237. time.Sleep(50 * time.Millisecond)
  238. }
  239. return
  240. }