service.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "go-common/app/interface/main/videoup/conf"
  7. "go-common/app/interface/main/videoup/dao/account"
  8. "go-common/app/interface/main/videoup/dao/archive"
  9. "go-common/app/interface/main/videoup/dao/bfs"
  10. "go-common/app/interface/main/videoup/dao/creative"
  11. "go-common/app/interface/main/videoup/dao/dynamic"
  12. "go-common/app/interface/main/videoup/dao/elec"
  13. "go-common/app/interface/main/videoup/dao/filter"
  14. "go-common/app/interface/main/videoup/dao/geetest"
  15. "go-common/app/interface/main/videoup/dao/mission"
  16. "go-common/app/interface/main/videoup/dao/order"
  17. "go-common/app/interface/main/videoup/dao/pay"
  18. "go-common/app/interface/main/videoup/dao/subtitle"
  19. "go-common/app/interface/main/videoup/dao/tag"
  20. arcmdl "go-common/app/interface/main/videoup/model/archive"
  21. missmdl "go-common/app/interface/main/videoup/model/mission"
  22. "go-common/app/interface/main/videoup/model/porder"
  23. "go-common/library/log"
  24. binfoc "go-common/library/log/infoc"
  25. "sync"
  26. )
  27. // Service is archive and videos service.
  28. type Service struct {
  29. c *conf.Config
  30. // dao
  31. arc *archive.Dao
  32. acc *account.Dao
  33. bfs *bfs.Dao
  34. miss *mission.Dao
  35. elec *elec.Dao
  36. order *order.Dao
  37. tag *tag.Dao
  38. creative *creative.Dao
  39. filter *filter.Dao
  40. gt *geetest.Dao
  41. dynamic *dynamic.Dao
  42. sub *subtitle.Dao
  43. pay *pay.Dao
  44. wg sync.WaitGroup
  45. asyncCh chan func() error
  46. // type cache
  47. typeCache map[int16]*arcmdl.Type
  48. orderUps map[int64]int64
  49. staffUps map[int64]int64
  50. missCache map[int]*missmdl.Mission
  51. missTagsCache map[string]int
  52. staffTypeCache map[int16]*arcmdl.StaffTypeConf
  53. staffGary bool
  54. descFmtsCache map[int16]map[int8][]*arcmdl.DescFormat
  55. exemptIDCheckUps map[int64]int64
  56. exemptZeroLevelAndAnswerUps map[int64]int64
  57. exemptUgcPayUps map[int64]int64
  58. exemptHalfMinUps map[int64]int64
  59. PorderCfgs map[int64]*porder.Config
  60. PorderGames map[int64]*porder.Game
  61. infoc *binfoc.Infoc
  62. }
  63. // New new a service.
  64. func New(c *conf.Config) (s *Service) {
  65. s = &Service{
  66. c: c,
  67. arc: archive.New(c),
  68. acc: account.New(c),
  69. bfs: bfs.New(c),
  70. miss: mission.New(c),
  71. elec: elec.New(c),
  72. order: order.New(c),
  73. tag: tag.New(c),
  74. creative: creative.New(c),
  75. filter: filter.New(c),
  76. gt: geetest.New(c),
  77. dynamic: dynamic.New(c),
  78. sub: subtitle.New(c),
  79. pay: pay.New(c),
  80. asyncCh: make(chan func() error, 100),
  81. infoc: binfoc.New(c.AppEditorInfoc),
  82. }
  83. s.loadType()
  84. s.loadMission()
  85. s.loadOrderUps()
  86. s.loadPorderCfgListAsMap()
  87. s.loadPorderGameListAsMap()
  88. s.loadExemptIDCheckUps()
  89. s.loadDescFormatCache()
  90. s.loadExemptZeroLevelAndAnswer()
  91. s.loadExemptUgcPayUps()
  92. s.loadStaffUps()
  93. s.loadStaffConfig()
  94. s.loadExemptHalfMinUps()
  95. go s.loadproc()
  96. go s.asyncproc()
  97. return s
  98. }
  99. func (s *Service) asyncproc() {
  100. s.wg.Add(1)
  101. defer s.wg.Done()
  102. for {
  103. f, ok := <-s.asyncCh
  104. if !ok {
  105. return
  106. }
  107. retry(3, 1*time.Second, f)
  108. }
  109. }
  110. func retry(attempts int, sleep time.Duration, callback func() error) (err error) {
  111. for i := 0; ; i++ {
  112. err = callback()
  113. if err == nil {
  114. return
  115. }
  116. if i >= (attempts - 1) {
  117. break
  118. }
  119. time.Sleep(sleep)
  120. log.Error("asyncproc retry retrying after error:(%+v)", err)
  121. }
  122. log.Error("asyncproc retry after %d attempts, last error: %s", attempts, err)
  123. return nil
  124. }
  125. func (s *Service) loadType() {
  126. tpm, err := s.arc.TypeMapping(context.TODO())
  127. if err != nil {
  128. log.Error("s.arc.TypeMapping error(%v)", err)
  129. return
  130. }
  131. s.typeCache = tpm
  132. }
  133. // s.missTagsCache: missionName or first tag
  134. func (s *Service) loadMission() {
  135. mm, err := s.miss.Missions(context.TODO())
  136. if err != nil {
  137. log.Error("s.miss.Mission error(%v)", err)
  138. return
  139. }
  140. s.missTagsCache = make(map[string]int)
  141. for _, m := range mm {
  142. if len(m.Tags) > 0 {
  143. splitedTags := strings.Split(m.Tags, ",")
  144. s.missTagsCache[splitedTags[0]] = m.ID
  145. } else {
  146. s.missTagsCache[m.Name] = m.ID
  147. }
  148. }
  149. s.missCache = mm
  150. }
  151. func (s *Service) loadOrderUps() {
  152. orderUps, err := s.order.Ups(context.TODO())
  153. if err != nil {
  154. return
  155. }
  156. s.orderUps = orderUps
  157. }
  158. // loadDescFormatCache
  159. func (s *Service) loadDescFormatCache() {
  160. fmts, err := s.arc.DescFormat(context.TODO())
  161. if err != nil {
  162. return
  163. }
  164. newFmts := make(map[int16]map[int8][]*arcmdl.DescFormat)
  165. for _, d := range fmts {
  166. if _, okTp := newFmts[d.TypeID]; !okTp {
  167. newFmts[d.TypeID] = make(map[int8][]*arcmdl.DescFormat)
  168. }
  169. if _, okCp := newFmts[d.TypeID][d.Copyright]; !okCp {
  170. newFmts[d.TypeID][d.Copyright] = []*arcmdl.DescFormat{}
  171. }
  172. newFmts[d.TypeID][d.Copyright] = append(newFmts[d.TypeID][d.Copyright], d)
  173. }
  174. log.Info("s.loadDescFormat: newFmts(%d), fmts(%d)", len(newFmts), len(fmts))
  175. s.descFmtsCache = newFmts
  176. }
  177. func (s *Service) loadStaffUps() {
  178. staffUps, err := s.arc.StaffUps(context.TODO())
  179. if err != nil {
  180. return
  181. }
  182. s.staffUps = staffUps
  183. }
  184. func (s *Service) loadproc() {
  185. for {
  186. time.Sleep(time.Duration(s.c.Tick))
  187. s.loadType()
  188. s.loadMission()
  189. s.loadExemptIDCheckUps()
  190. s.loadExemptZeroLevelAndAnswer()
  191. s.loadExemptUgcPayUps()
  192. s.loadExemptHalfMinUps()
  193. s.loadDescFormatCache()
  194. s.loadOrderUps()
  195. s.loadPorderCfgListAsMap()
  196. s.loadPorderGameListAsMap()
  197. s.loadStaffUps()
  198. s.loadStaffConfig()
  199. }
  200. }
  201. func (s *Service) loadPorderCfgListAsMap() {
  202. porderCfgList, err := s.arc.PorderCfgList(context.TODO())
  203. if err != nil {
  204. log.Error("s.arc.PorderCfgList error(%v)", err)
  205. return
  206. }
  207. s.PorderCfgs = porderCfgList
  208. }
  209. func (s *Service) loadPorderGameListAsMap() {
  210. list, err := s.arc.GameList(context.TODO())
  211. if err != nil {
  212. log.Error("GameList error(%v)", err)
  213. return
  214. }
  215. if len(list) == 0 || list == nil {
  216. log.Error("GameList empty error(%v)", err)
  217. return
  218. }
  219. s.PorderGames = list
  220. }
  221. // Ping ping success.
  222. func (s *Service) Ping(c context.Context) (err error) {
  223. return s.arc.Ping(c)
  224. }
  225. // Close close resource.
  226. func (s *Service) Close() {
  227. s.acc.Close()
  228. s.arc.Close()
  229. s.infoc.Close()
  230. close(s.asyncCh)
  231. s.wg.Wait()
  232. }
  233. // loadExemptIDCheckUps
  234. func (s *Service) loadExemptIDCheckUps() {
  235. ups, err := s.arc.UpSpecial(context.TODO(), 8)
  236. if err != nil {
  237. return
  238. }
  239. s.exemptIDCheckUps = ups
  240. }
  241. // loadExemptIDCheckUps
  242. func (s *Service) loadExemptZeroLevelAndAnswer() {
  243. ups, err := s.arc.UpSpecial(context.TODO(), 12)
  244. if err != nil {
  245. return
  246. }
  247. s.exemptZeroLevelAndAnswerUps = ups
  248. }
  249. // loadExemptUgcPayUps
  250. func (s *Service) loadExemptUgcPayUps() {
  251. ups, err := s.arc.UpSpecial(context.TODO(), 17)
  252. if err != nil {
  253. return
  254. }
  255. s.exemptUgcPayUps = ups
  256. }
  257. // loadStaffConfig 加载联合投稿分区配置
  258. func (s *Service) loadStaffConfig() {
  259. var err error
  260. if s.staffGary, s.staffTypeCache, err = s.arc.StaffTypeConfig(context.TODO()); err != nil {
  261. s.staffGary = true //怕权限被放出,没有获取到 creative配置的时候,默认开启了灰度策略
  262. }
  263. }
  264. // loadExemptHalfMinUps
  265. func (s *Service) loadExemptHalfMinUps() {
  266. ups, err := s.arc.UpSpecial(context.TODO(), 25)
  267. if err != nil {
  268. return
  269. }
  270. s.exemptHalfMinUps = ups
  271. }