service.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strconv"
  6. "sync"
  7. "time"
  8. "go-common/app/job/main/credit/conf"
  9. "go-common/app/job/main/credit/dao"
  10. archive "go-common/app/service/main/archive/api/gorpc"
  11. memrpc "go-common/app/service/main/member/api/gorpc"
  12. "go-common/library/log"
  13. "go-common/library/queue/databus"
  14. )
  15. // Service struct of service.
  16. type Service struct {
  17. c *conf.Config
  18. dao *dao.Dao
  19. credbSub *databus.Databus
  20. replyAllSub *databus.Databus
  21. labourSub *databus.Databus
  22. arcRPC *archive.Service2
  23. memRPC *memrpc.Service
  24. // wait group
  25. wg sync.WaitGroup
  26. }
  27. // New create service instance and return.
  28. func New(c *conf.Config) (s *Service) {
  29. s = &Service{
  30. c: c,
  31. dao: dao.New(c),
  32. credbSub: databus.New(c.DataBus.CreditDBSub),
  33. replyAllSub: databus.New(c.DataBus.ReplyAllSub),
  34. labourSub: databus.New(c.DataBus.LabourSub),
  35. arcRPC: archive.New2(c.RPCClient.Archive),
  36. memRPC: memrpc.New(c.RPCClient.Member),
  37. }
  38. s.loadConf()
  39. s.loadCase()
  40. s.loadDealWrongCase()
  41. s.wg.Add(1)
  42. go s.replyAllConsumer()
  43. go s.creditConsumer()
  44. go s.loadConfproc()
  45. return
  46. }
  47. // Ping check service health.
  48. func (s *Service) Ping(c context.Context) error {
  49. return s.dao.Ping(c)
  50. }
  51. func (s *Service) loadConfproc() {
  52. for {
  53. time.Sleep(time.Duration(s.c.Judge.ConfTimer))
  54. s.loadConf()
  55. s.loadCase()
  56. s.loadDealWrongCase()
  57. }
  58. }
  59. func (s *Service) loadConf() {
  60. m, err := s.dao.LoadConf(context.TODO())
  61. if err != nil {
  62. log.Error("loadConf error(%v)", err)
  63. return
  64. }
  65. if s.c.Judge.CaseGiveHours, err = strconv.ParseInt(m["case_give_hours"], 10, 64); err != nil {
  66. log.Error("loadConf CaseGiveHours error(%v)", err)
  67. }
  68. if s.c.Judge.CaseCheckTime, err = strconv.ParseInt(m["case_check_hours"], 10, 64); err != nil {
  69. log.Error("loadConf CaseCheckTime error(%v)", err)
  70. }
  71. if s.c.Judge.JuryRatio, err = strconv.ParseInt(m["jury_vote_radio"], 10, 64); err != nil {
  72. log.Error("loadConf JuryRatio error(%v)", err)
  73. }
  74. if s.c.Judge.JudgeRadio, err = strconv.ParseInt(m["case_judge_radio"], 10, 64); err != nil {
  75. log.Error("loadConf JudgeRadio error(%v)", err)
  76. }
  77. if s.c.Judge.CaseVoteMin, err = strconv.ParseInt(m["case_vote_min"], 10, 64); err != nil {
  78. log.Error("loadConf CaseVoteMin error(%v)", err)
  79. }
  80. if s.c.Judge.CaseObtainMax, err = strconv.ParseInt(m["case_obtain_max"], 10, 64); err != nil {
  81. log.Error("loadConf CaseObtainMax error(%v)", err)
  82. }
  83. if s.c.Judge.CaseVoteMax, err = strconv.ParseInt(m["case_vote_max"], 10, 64); err != nil {
  84. log.Error("loadConf CaseVoteMax error(%v)", err)
  85. }
  86. if s.c.Judge.JuryApplyMax, err = strconv.ParseInt(m["jury_apply_max"], 10, 64); err != nil {
  87. log.Error("loadConf JuryApplyMax error(%v)", err)
  88. }
  89. if s.c.Judge.CaseLoadMax, err = strconv.Atoi(m["case_load_max"]); err != nil {
  90. log.Error("loadConf CaseLoadMax error(%v)", err)
  91. }
  92. var caseLoadSwitch int64
  93. if caseLoadSwitch, err = strconv.ParseInt(m["case_load_switch"], 10, 64); err != nil {
  94. log.Error("loadConf CaseLoadSwitch error(%v)", err)
  95. }
  96. s.c.Judge.CaseLoadSwitch = int8(caseLoadSwitch)
  97. if s.c.Judge.CaseVoteMaxPercent, err = strconv.Atoi(m["case_vote_max_percent"]); err != nil {
  98. log.Error("loadConf CaseVoteMaxPercent error(%v)", err)
  99. }
  100. if _, ok := m["vote_num"]; !ok {
  101. s.c.Judge.VoteNum.RateS = 1
  102. s.c.Judge.VoteNum.RateA = 1
  103. s.c.Judge.VoteNum.RateB = 1
  104. s.c.Judge.VoteNum.RateC = 1
  105. s.c.Judge.VoteNum.RateD = 1
  106. return
  107. }
  108. if err = json.Unmarshal([]byte(m["vote_num"]), &s.c.Judge.VoteNum); err != nil {
  109. log.Error("loadConf vote_num error(%v)", err)
  110. }
  111. }
  112. // Close kafka consumer close.
  113. func (s *Service) Close() {
  114. if s == nil {
  115. return
  116. }
  117. if s.dao != nil {
  118. s.dao.Close()
  119. }
  120. if s.credbSub != nil {
  121. s.credbSub.Close()
  122. }
  123. if s.replyAllSub != nil {
  124. s.replyAllSub.Close()
  125. }
  126. s.wg.Wait()
  127. }