service.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package resource
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/web-show/conf"
  6. "go-common/app/interface/main/web-show/dao/ad"
  7. "go-common/app/interface/main/web-show/dao/bangumi"
  8. "go-common/app/interface/main/web-show/dao/data"
  9. resdao "go-common/app/interface/main/web-show/dao/resource"
  10. rsmdl "go-common/app/interface/main/web-show/model/resource"
  11. accrc "go-common/app/service/main/account/rpc/client"
  12. arcrpc "go-common/app/service/main/archive/api/gorpc"
  13. locrpc "go-common/app/service/main/location/rpc/client"
  14. recrpc "go-common/app/service/main/resource/rpc/client"
  15. "go-common/library/log"
  16. )
  17. // Service define web-show service
  18. type Service struct {
  19. resdao *resdao.Dao
  20. bangumiDao *bangumi.Dao
  21. accRPC *accrc.Service3
  22. arcRPC *arcrpc.Service2
  23. recrpc *recrpc.Service
  24. adDao *ad.Dao
  25. dataDao *data.Dao
  26. locRPC *locrpc.Service
  27. // cache
  28. asgCache map[int][]*rsmdl.Assignment // resID => assignments
  29. urlMonitor map[int]map[string]string // pf=>map[rs.name=>url]
  30. videoCache map[int64][][]*rsmdl.VideoAD
  31. posCache map[string]*rsmdl.Position // resID=>srcIDs
  32. defBannerCache *rsmdl.Assignment
  33. adsCache map[int]*rsmdl.Assignment
  34. }
  35. // New return service object
  36. func New(c *conf.Config) *Service {
  37. s := &Service{
  38. adDao: ad.New(c),
  39. resdao: resdao.New(c),
  40. bangumiDao: bangumi.New(c),
  41. dataDao: data.New(c),
  42. asgCache: make(map[int][]*rsmdl.Assignment),
  43. videoCache: make(map[int64][][]*rsmdl.VideoAD),
  44. posCache: make(map[string]*rsmdl.Position),
  45. // crm
  46. adsCache: make(map[int]*rsmdl.Assignment),
  47. }
  48. s.arcRPC = arcrpc.New2(c.RPCClient2.Archive)
  49. s.accRPC = accrc.New3(c.RPCClient2.Account)
  50. s.recrpc = recrpc.New(c.RPCClient2.Resource)
  51. s.locRPC = locrpc.New(c.LocationRPC)
  52. s.init()
  53. return s
  54. }
  55. func (s *Service) init() (err error) {
  56. if err = s.loadRes(); err != nil {
  57. log.Error("adService.Load, err (%v)", err)
  58. }
  59. if err = s.loadVideoAd(); err != nil {
  60. log.Error("adService.LoadVideo, err (%v)", err)
  61. }
  62. // if err = s.loadAds(); err != nil {
  63. // log.Error("s.loadAds err(%v)", err)
  64. // }
  65. go s.checkproc()
  66. go s.loadproc()
  67. return
  68. }
  69. // loadproc is a routine load ads to cache
  70. func (s *Service) loadproc() {
  71. for {
  72. s.loadRes()
  73. s.loadVideoAd()
  74. //s.loadAds()
  75. time.Sleep(time.Duration(conf.Conf.Reload.Ad))
  76. }
  77. }
  78. // checkpro a routine check diff
  79. func (s *Service) checkproc() {
  80. for {
  81. s.checkDiff()
  82. time.Sleep(time.Duration(conf.Conf.Reload.Ad))
  83. }
  84. }
  85. // Close close service
  86. func (s *Service) Close() {
  87. s.resdao.Close()
  88. }
  89. // Ping ping service
  90. func (s *Service) Ping(c context.Context) (err error) {
  91. if err = s.resdao.Ping(c); err != nil {
  92. log.Error("s.resDap.Ping err(%v)", err)
  93. return
  94. }
  95. if err = s.adDao.Ping(c); err != nil {
  96. log.Error("s.adDao.Ping err(%v)", err)
  97. }
  98. return
  99. }