task.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "time"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/app/admin/main/videoup/model/manager"
  10. xsql "go-common/library/database/sql"
  11. "go-common/library/log"
  12. )
  13. // TaskTooksByHalfHour get task books by ctime
  14. func (s *Service) TaskTooksByHalfHour(c context.Context, stime, etime time.Time) (tooks []*archive.TaskTook, err error) {
  15. if tooks, err = s.arc.TaskTooksByHalfHour(c, stime, etime); err != nil {
  16. log.Error("s.arc.TaskTooksByHalfHour(%v,%v)", stime, etime)
  17. return
  18. }
  19. return
  20. }
  21. //lockVideo Lock specified category videos
  22. func (s *Service) lockVideo() {
  23. //TODO It's a temporary function to lock videos. When no longer needed, remove it.
  24. var (
  25. c = context.TODO()
  26. adminID int64 = 399 //Temporary task admin id for lock video.
  27. uname = "videoupjob" //Temporary task admin name for lock video.
  28. reason = "版权原因,该视频不予审核通过"
  29. reasonID int64 = 197
  30. rCateID int64 = 76
  31. tagID int64 = 7 //版权tag
  32. note = "自动锁定分区视频【欧美电影】,【日本电影】,【其他国家】,【港台剧】,【海外剧】"
  33. ctime = time.Now()
  34. mtime = ctime
  35. err error
  36. tx *xsql.Tx
  37. )
  38. //Note: check if another instance is locking video
  39. locking, err := s.arc.IsLockingVideo(c)
  40. if err != nil {
  41. log.Error("s.lockVideo() s.arc.IsLockingVideo() err(%v)", err)
  42. return
  43. }
  44. if locking {
  45. log.Info("s.lockVideo() another instance is locking video")
  46. return
  47. }
  48. //Set locking video redis
  49. if err = s.arc.LockingVideo(c, 1); err != nil {
  50. log.Error("s.lockVideo() s.arc.LockingVideo() err(%v)", err)
  51. return
  52. }
  53. defer func() {
  54. //Unlock locking video redis
  55. if err = s.arc.LockingVideo(c, 0); err != nil {
  56. log.Error("s.lockVideo() s.arc.LockingVideo() err(%v)", err)
  57. }
  58. }()
  59. if _, err = s.arc.TaskUserCheckIn(c, adminID); err != nil {
  60. log.Error("s.lockVideo() s.arc.TaskUserCheckIn(%d) error(%v)", adminID, err)
  61. return
  62. }
  63. tasks, err := s.arc.UserUndoneSpecTask(c, adminID)
  64. if err != nil {
  65. log.Error("s.lockVideo() error(%v)", err)
  66. return
  67. }
  68. if len(tasks) == 0 {
  69. log.Info("s.lockVideo() no task.")
  70. return
  71. }
  72. var vps = []*archive.VideoParam{}
  73. for _, t := range tasks {
  74. if t.State == archive.TypeFinished {
  75. continue
  76. }
  77. v, err := s.arc.VideoByCID(c, t.Cid)
  78. if err != nil {
  79. log.Error("s.lockVideo() s.arc.VideoByCID(%d) error(%v)", t.Cid, err)
  80. continue
  81. }
  82. arc, err := s.arc.Archive(c, t.Aid)
  83. if err != nil {
  84. log.Error("s.lockVideo() s.arc.Archive(%d) error(%v)", v.Aid, err)
  85. continue
  86. }
  87. //If archive's mid in white list, release task and continue
  88. if s.PGCWhite(arc.Mid) {
  89. log.Info("s.lockVideo() mid in white list, release task(%d)", t.ID)
  90. //Begin update task state and add task history
  91. if tx, err = s.arc.BeginTran(c); err != nil {
  92. log.Error("s.arc.BeginTran error(%v)", err)
  93. continue
  94. }
  95. if _, err = s.arc.TxReleaseByID(tx, t.ID); err != nil {
  96. log.Error("s.lockVideo() s.arc.TxReleaseByID(%d) error(%v)", t.ID, err)
  97. tx.Rollback()
  98. continue
  99. }
  100. if _, err = s.arc.TxAddTaskHis(tx, 0, archive.ActionRelease /*action*/, t.ID /*task_id*/, t.Cid /*cid*/, 0, 0, 0, "lockVideo release" /*reason*/); err != nil {
  101. log.Error("s.lockVideo() s.arc.TxAddTaskHis error(%v)", err)
  102. tx.Rollback()
  103. continue
  104. }
  105. if err = tx.Commit(); err != nil {
  106. log.Error("tx.Commit error(%v)", err)
  107. }
  108. continue
  109. }
  110. //Get archive's top type id
  111. rTp, err := s.TypeTopParent(arc.TypeID)
  112. if err != nil {
  113. log.Error("s.lockVideo() s.arc.TypeTopParent(%d) error(%v)", arc.TypeID, err)
  114. continue
  115. }
  116. //Add video lock reason log
  117. if _, err = s.mng.AddReasonLog(c, v.Cid, manager.ReasonLogTypeVideo, rCateID, reasonID, adminID, arc.TypeID, ctime, mtime); err != nil {
  118. log.Error("s.lockVideo() s.arc.AddReasonLog(%d,%d,%d,%d,%d,%d,%v,%v) error(%v)", v.Cid, manager.ReasonLogTypeVideo, rCateID, reasonID, adminID, arc.TypeID, ctime, mtime, err)
  119. }
  120. //Begin update task state and add task history
  121. if tx, err = s.arc.BeginTran(c); err != nil {
  122. log.Error("s.arc.BeginTran error(%v)", err)
  123. continue
  124. }
  125. if _, err = s.arc.TxUpTaskByID(tx, t.ID, map[string]interface{}{"state": archive.TypeFinished, "utime": 0}); err != nil {
  126. log.Error("s.lockVideo() s.arc.TxUpTaskByID(%d) error(%v)", t.ID, err)
  127. tx.Rollback()
  128. continue
  129. }
  130. if _, err = s.arc.TxAddTaskHis(tx, archive.PoolForFirst, archive.TypeFinished, t.ID, t.Cid, adminID, 0, archive.VideoStatusLock, reason); err != nil {
  131. log.Error("s.lockVideo() s.arc.SubmitTask(%d) error(%v)", t.ID, err)
  132. tx.Rollback()
  133. continue
  134. }
  135. if err = tx.Commit(); err != nil {
  136. log.Error("tx.Commit error(%v)", err)
  137. continue
  138. }
  139. //Set video param
  140. vp := &archive.VideoParam{}
  141. vp.ID = v.ID
  142. vp.Aid = v.Aid
  143. vp.Cid = v.Cid
  144. vp.Filename = v.Filename
  145. vp.RegionID = rTp.ID
  146. vp.Status = archive.VideoStatusLock
  147. vp.UID = adminID
  148. vp.Oname = uname
  149. vp.Note = note + " [任务ID]task:" + strconv.Itoa(int(t.ID))
  150. vp.Reason = reason
  151. vp.TagID = tagID
  152. vp.Encoding = 0
  153. vps = append(vps, vp)
  154. log.Info("s.lockVideo() add video. cid(%d)", v.Cid)
  155. }
  156. if len(vps) == 0 {
  157. log.Info("s.lockVideo() no belongs to 399 task.")
  158. return
  159. }
  160. //Add videos to batch list
  161. s.BatchVideo(c, vps, archive.ActionVideoSubmit)
  162. }
  163. /* 批量查询,批量转换
  164. * list []*struct{}
  165. * multrans 转化器,根据ID查出其他值
  166. * ID id字段名称,id字段类型必须是int64
  167. * Names 查出来的各个字段名称
  168. */
  169. func (s *Service) mulIDtoName(c context.Context, list interface{}, multrans func(context.Context, []int64) (map[int64][]interface{}, error), ID string, Names ...string) (err error) {
  170. var (
  171. lV, itemI, itemIE, idFiled, nameFiled, valueField reflect.Value
  172. id int64
  173. ids []int64
  174. hashIDName = make(map[int64][]interface{})
  175. )
  176. if lV = reflect.ValueOf(list); !lV.IsValid() || lV.IsNil() || lV.Kind() != reflect.Slice {
  177. return fmt.Errorf("invalid list")
  178. }
  179. count := lV.Len()
  180. for i := 0; i < count; i++ {
  181. if itemI = lV.Index(i); !itemI.IsValid() || itemI.IsNil() || itemI.Kind() != reflect.Ptr {
  182. return fmt.Errorf("invalid itemI")
  183. }
  184. if itemIE = itemI.Elem(); !itemIE.IsValid() || itemIE.Kind() != reflect.Struct {
  185. return fmt.Errorf("invalid itemIE")
  186. }
  187. if idFiled = itemIE.FieldByName(ID); !idFiled.IsValid() || idFiled.Kind() != reflect.Int64 {
  188. return fmt.Errorf("invalid idFiled")
  189. }
  190. for _, name := range Names {
  191. if nameFiled = itemIE.FieldByName(name); !nameFiled.IsValid() || !nameFiled.CanSet() {
  192. return fmt.Errorf("invalid nameFiled")
  193. }
  194. }
  195. if id = idFiled.Int(); id != 0 {
  196. if _, ok := hashIDName[id]; !ok {
  197. hashIDName[id] = []interface{}{}
  198. ids = append(ids, id)
  199. }
  200. }
  201. }
  202. if hashIDName, err = multrans(c, ids); err != nil {
  203. log.Error("multrans error(%v)", ids)
  204. return
  205. }
  206. for i := 0; i < count; i++ {
  207. itemIE = lV.Index(i).Elem()
  208. id = itemIE.FieldByName(ID).Int()
  209. if names, ok := hashIDName[id]; ok && len(names) == len(Names) {
  210. for i, name := range names {
  211. nameFiled = itemIE.FieldByName(Names[i])
  212. valueField = reflect.ValueOf(name)
  213. if nameFiled.Kind() != valueField.Kind() {
  214. log.Error("multrans return %v while need ", ids)
  215. continue
  216. }
  217. itemIE.FieldByName(Names[i]).Set(reflect.ValueOf(name))
  218. }
  219. }
  220. }
  221. return
  222. }
  223. // 每个ID单独查询 strict严格模式下一次错误,直接返回
  224. func (s *Service) singleIDtoName(c context.Context, list interface{}, singletrans func(context.Context, int64) ([]interface{}, error), strict bool, ID string, Names ...string) (err error) {
  225. var (
  226. lV, itemI, itemIE, idFiled, nameFiled, valueField reflect.Value
  227. id int64
  228. values []interface{}
  229. )
  230. if lV = reflect.ValueOf(list); !lV.IsValid() || lV.IsNil() || lV.Kind() != reflect.Slice {
  231. return fmt.Errorf("invalid list")
  232. }
  233. count := lV.Len()
  234. for i := 0; i < count; i++ {
  235. if itemI = lV.Index(i); !itemI.IsValid() || itemI.IsNil() || itemI.Kind() != reflect.Ptr {
  236. return fmt.Errorf("invalid itemI")
  237. }
  238. if itemIE = itemI.Elem(); !itemIE.IsValid() || itemIE.Kind() != reflect.Struct {
  239. return fmt.Errorf("invalid itemIE")
  240. }
  241. if idFiled = itemIE.FieldByName(ID); !idFiled.IsValid() || idFiled.Kind() != reflect.Int64 {
  242. return fmt.Errorf("invalid idFiled")
  243. }
  244. for _, Name := range Names {
  245. if nameFiled = itemIE.FieldByName(Name); !nameFiled.IsValid() || !nameFiled.CanSet() {
  246. return fmt.Errorf("invalid nameFiled")
  247. }
  248. }
  249. if id = idFiled.Int(); id != 0 {
  250. if values, err = singletrans(c, id); err != nil || len(values) != len(Names) {
  251. log.Error("s.sigleIDtoName error(%v) len(values)=%d len(Names)=%d", err, len(values), len(Names))
  252. if strict {
  253. return
  254. }
  255. err = nil
  256. continue
  257. }
  258. for i, value := range values {
  259. nameFiled = itemIE.FieldByName(Names[i])
  260. valueField = reflect.ValueOf(value)
  261. if nameFiled.Kind() != valueField.Kind() {
  262. log.Error("singletrans return %s while need %s", valueField.Kind().String(), nameFiled.Kind().String())
  263. continue
  264. }
  265. nameFiled.Set(valueField)
  266. }
  267. }
  268. }
  269. return
  270. }
  271. // GetUID 获取uid,有时候cookie没有uid
  272. func (s *Service) GetUID(c context.Context, name string) (uid int64, err error) {
  273. return s.mng.GetUIDByName(c, name)
  274. }