service.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "go-common/app/admin/main/credit/conf"
  7. accdao "go-common/app/admin/main/credit/dao/account"
  8. blockedao "go-common/app/admin/main/credit/dao/blocked"
  9. managerdao "go-common/app/admin/main/credit/dao/manager"
  10. msgdao "go-common/app/admin/main/credit/dao/msg"
  11. relationdao "go-common/app/admin/main/credit/dao/relation"
  12. searchdao "go-common/app/admin/main/credit/dao/search"
  13. uploaddao "go-common/app/admin/main/credit/dao/upload"
  14. blkmodel "go-common/app/admin/main/credit/model/blocked"
  15. coinclient "go-common/app/service/main/coin/api"
  16. "go-common/library/log"
  17. "github.com/jinzhu/gorm"
  18. )
  19. // Service struct of service.
  20. type Service struct {
  21. // conf
  22. c *conf.Config
  23. accDao *accdao.Dao
  24. blockedDao *blockedao.Dao
  25. managerDao *managerdao.Dao
  26. searchDao *searchdao.Dao
  27. uploadDao *uploaddao.Dao
  28. msgDao *msgdao.Dao
  29. RelationDao *relationdao.Dao
  30. DB *gorm.DB
  31. ReadDB *gorm.DB
  32. Search *searchdao.Dao
  33. caseConfCache map[string]string
  34. Managers map[int64]string
  35. caseReasons map[int]string
  36. MsgCh chan *blkmodel.SysMsg
  37. notifych chan func()
  38. stop chan struct{}
  39. // wait
  40. wg sync.WaitGroup
  41. coinClient coinclient.CoinClient
  42. }
  43. // New create service instance and return.
  44. func New(c *conf.Config) (s *Service) {
  45. s = &Service{
  46. c: c,
  47. blockedDao: blockedao.New(c),
  48. managerDao: managerdao.New(c),
  49. accDao: accdao.New(c),
  50. searchDao: searchdao.New(c),
  51. uploadDao: uploaddao.New(c),
  52. RelationDao: relationdao.New(c),
  53. msgDao: msgdao.New(c),
  54. MsgCh: make(chan *blkmodel.SysMsg, c.ChanSize.SysMsg),
  55. notifych: make(chan func(), c.ChanSize.SysMsg),
  56. stop: make(chan struct{}),
  57. }
  58. s.DB = s.blockedDao.DB
  59. s.ReadDB = s.blockedDao.ReadDB
  60. s.Search = s.searchDao
  61. var err error
  62. if s.coinClient, err = coinclient.NewClient(c.CoinClient); err != nil {
  63. panic(err)
  64. }
  65. s.loadConfig()
  66. s.loadManager()
  67. go s.cacheproc()
  68. go s.loadManagerproc()
  69. go s.msgproc()
  70. s.wg.Add(1)
  71. go s.notifyproc()
  72. return s
  73. }
  74. func (s *Service) cacheproc() {
  75. for {
  76. time.Sleep(3 * time.Minute)
  77. s.loadConfig()
  78. }
  79. }
  80. func (s *Service) loadManagerproc() {
  81. for {
  82. time.Sleep(1 * time.Hour)
  83. s.loadManager()
  84. }
  85. }
  86. func (s *Service) loadConfig() {
  87. cc, err := s.CaseConf(context.TODO())
  88. if err != nil {
  89. log.Error("s.CaseConf error(%v)", err)
  90. return
  91. }
  92. s.caseConfCache = cc
  93. cr, err := s.CaseReason(context.TODO())
  94. if err != nil {
  95. log.Error("s.CaseReason error(%v)", err)
  96. return
  97. }
  98. s.caseReasons = cr
  99. }
  100. func (s *Service) loadManager() {
  101. managers, err := s.managerDao.Managers(context.TODO())
  102. if err != nil {
  103. log.Error("s.Managers error(%v)", err)
  104. return
  105. }
  106. s.Managers = managers
  107. }
  108. // AddNotify .
  109. func (s *Service) AddNotify(f func()) {
  110. select {
  111. case s.notifych <- f:
  112. default:
  113. log.Warn("addNotify chan full")
  114. }
  115. }
  116. // notifyproc nofity clear cache
  117. func (s *Service) notifyproc() {
  118. defer s.wg.Done()
  119. for {
  120. f, ok := <-s.notifych
  121. if !ok {
  122. log.Warn("s.notifych chan is close")
  123. return
  124. }
  125. f()
  126. }
  127. }
  128. // Ping check server ok.
  129. func (s *Service) Ping(c context.Context) error {
  130. return s.blockedDao.Ping(c)
  131. }
  132. // Close dao.
  133. func (s *Service) Close() {
  134. s.blockedDao.Close()
  135. close(s.MsgCh)
  136. close(s.notifych)
  137. close(s.stop)
  138. time.Sleep(1 * time.Second)
  139. s.wg.Wait()
  140. }