sync_video.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package ugc
  2. import (
  3. "context"
  4. "fmt"
  5. ugcmdl "go-common/app/job/main/tv/model/ugc"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "time"
  9. )
  10. const (
  11. _videoCond = " AND (v.transcoded = 1 OR v.cid <= %d) " +
  12. "AND v.retry < unix_timestamp(now()) " +
  13. "AND v.deleted = 0 "
  14. _parseVideos = "SELECT id,cid,index_order,eptitle,duration,description FROM ugc_video v " +
  15. "WHERE v.aid = ? AND v.submit = 1 " + _videoCond
  16. _postArc = "UPDATE ugc_video SET retry = ? WHERE cid = ? AND deleted = 0"
  17. _finishVideos = "UPDATE ugc_video SET submit = 0 WHERE cid = ? AND aid = ? AND deleted = 0"
  18. _finishArc = "UPDATE ugc_archive SET submit = 0 WHERE aid = ? AND deleted = 0"
  19. _parseArc = "SELECT id,aid,mid,typeid,videos,title,cover,content,duration,copyright,pubtime,ctime,mtime,state," +
  20. "manual,valid,submit,retry,result,deleted FROM ugc_archive WHERE aid = ?"
  21. _shouldAudit = "SELECT COUNT(1) as cnt FROM ugc_video v WHERE v.aid = ? AND v.submit = 1 " + _videoCond
  22. _videoSubmit = "SELECT cid FROM ugc_video v WHERE v.aid = ? AND v.submit = 0 " + _videoCond + " LIMIT 1"
  23. )
  24. // PpVideos postpones the archive's videos submit in 30 mins
  25. func (d *Dao) PpVideos(c context.Context, cids []int64) (err error) {
  26. var delay = time.Now().Unix() + int64(d.conf.UgcSync.Frequency.ErrorWait)
  27. for _, v := range cids {
  28. if _, err = d.DB.Exec(c, _postArc, delay, v); err != nil {
  29. log.Error("PostponeArc, failed to delay: (%v,%v), Error: %v", delay, v, err)
  30. return
  31. }
  32. }
  33. return
  34. }
  35. // FinishVideos updates the submit status from 1 to 0
  36. func (d *Dao) FinishVideos(c context.Context, videos []*ugcmdl.SimpleVideo, aid int64) (err error) {
  37. for _, v := range videos {
  38. if _, err = d.DB.Exec(c, _finishVideos, v.CID, aid); err != nil { // avoid updating the cid under another archive
  39. log.Error("FinishVideos Error: %v", v.CID, err)
  40. return
  41. }
  42. }
  43. if _, err = d.DB.Exec(c, _finishArc, aid); err != nil {
  44. log.Error("FinishVideos Error: %v", aid, err)
  45. }
  46. return
  47. }
  48. // ParseArc parses one archive data
  49. func (d *Dao) ParseArc(c context.Context, aid int64) (res *ugcmdl.Archive, err error) {
  50. res = &ugcmdl.Archive{}
  51. if err = d.DB.QueryRow(c, _parseArc, aid).Scan(&res.ID, &res.AID, &res.MID, &res.TypeID, &res.Videos, &res.Title,
  52. &res.Cover, &res.Content, &res.Duration, &res.Copyright, &res.Pubtime, &res.Ctime, &res.Mtime, &res.State,
  53. &res.Manual, &res.Valid, &res.Submit, &res.Retry, &res.Result, &res.Deleted); err != nil { // get the qualified aid to sync
  54. log.Warn("d.ParseArc.Query error(%v)", err)
  55. }
  56. return
  57. }
  58. // ShouldAudit distinguishes whether the archive should ask for audit or not
  59. func (d *Dao) ShouldAudit(c context.Context, aid int64) (res bool, err error) {
  60. var cnt int
  61. if err = d.DB.QueryRow(c, fmt.Sprintf(_shouldAudit, d.criCID), aid).Scan(&cnt); err != nil {
  62. log.Error("d.ShouldAudit Aid %d Err %v", aid, err)
  63. return
  64. }
  65. res = cnt > 0
  66. return
  67. }
  68. // VideoSubmit tells whether the archive already has some video submitted
  69. func (d *Dao) VideoSubmit(c context.Context, aid int64) (cid int64, err error) {
  70. if err = d.DB.QueryRow(c, fmt.Sprintf(_videoSubmit, d.criCID), aid).Scan(&cid); err != nil {
  71. log.Warn("d.videoSubmit Aid %d, Err %v", aid, err)
  72. }
  73. return
  74. }
  75. // ParseVideos picks 20 videos of one qualified archive
  76. func (d *Dao) ParseVideos(c context.Context, aid int64, ps int) (res [][]*ugcmdl.SimpleVideo, err error) {
  77. var (
  78. rows *sql.Rows
  79. videos []*ugcmdl.SimpleVideo
  80. )
  81. if rows, err = d.DB.Query(c, fmt.Sprintf(_parseVideos, d.criCID), aid); err != nil {
  82. log.Error("d._parseVideos.Query error(%v)", err)
  83. return
  84. }
  85. defer rows.Close()
  86. for rows.Next() {
  87. var r = ugcmdl.SimpleVideo{}
  88. if err = rows.Scan(&r.ID, &r.CID, &r.IndexOrder, &r.Eptitle, &r.Duration, &r.Description); err != nil {
  89. log.Error("ParseVideos row.Scan() error(%v)", err)
  90. return
  91. }
  92. videos = append(videos, &r)
  93. if len(videos) >= ps {
  94. var videoPce = append([]*ugcmdl.SimpleVideo{}, videos...)
  95. videos = []*ugcmdl.SimpleVideo{}
  96. res = append(res, videoPce)
  97. }
  98. }
  99. if err = rows.Err(); err != nil {
  100. log.Error("d._parseVideos.Query error(%v)", err)
  101. return
  102. }
  103. if len(videos) > 0 {
  104. res = append(res, videos)
  105. }
  106. return
  107. }