service.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/credit-timer/conf"
  6. "go-common/app/job/main/credit-timer/dao"
  7. "go-common/library/log"
  8. )
  9. // Service struct of service.
  10. type Service struct {
  11. c *conf.Config
  12. dao *dao.Dao
  13. }
  14. // New create service instance and return.
  15. func New(c *conf.Config) (s *Service) {
  16. s = &Service{
  17. c: c,
  18. dao: dao.New(c),
  19. }
  20. go s.loadConfproc()
  21. go s.caseproc()
  22. go s.juryproc()
  23. go s.voteproc()
  24. go s.kpiproc()
  25. return
  26. }
  27. func (s *Service) loadConfproc() {
  28. for {
  29. s.loadConf(context.TODO())
  30. time.Sleep(time.Duration(s.c.Judge.ConfTimer))
  31. }
  32. }
  33. func (s *Service) caseproc() {
  34. for {
  35. s.caseProc(context.TODO())
  36. time.Sleep(time.Duration(s.c.Judge.CaseTimer))
  37. }
  38. }
  39. func (s *Service) juryproc() {
  40. for {
  41. s.juryProc(context.TODO())
  42. time.Sleep(time.Duration(s.c.Judge.JuryTimer))
  43. }
  44. }
  45. func (s *Service) voteproc() {
  46. for {
  47. s.voteProc(context.TODO())
  48. time.Sleep(time.Duration(s.c.Judge.VoteTimer))
  49. }
  50. }
  51. func (s *Service) kpiproc() {
  52. var err error
  53. for {
  54. d := time.Now().AddDate(0, 0, 1)
  55. ts := time.Until(time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 1, 0, time.Local))
  56. time.Sleep(ts)
  57. for {
  58. err = s.kpiPointProc(context.TODO())
  59. if err != nil {
  60. log.Error("kpiPointProc err(%v)", err)
  61. time.Sleep(time.Second * 5)
  62. continue
  63. }
  64. break
  65. }
  66. log.Info("KPIPointproc err(%v)", err)
  67. for {
  68. err = s.KPIProc(context.TODO())
  69. if err != nil {
  70. log.Error("kpiProc err(%v)", err)
  71. time.Sleep(time.Second * 5)
  72. continue
  73. }
  74. break
  75. }
  76. log.Info("kpiproc err(%v)", err)
  77. }
  78. }
  79. // Close kafka consumer close.
  80. func (s *Service) Close() (err error) {
  81. return
  82. }
  83. // Ping check service health.
  84. func (s *Service) Ping(c context.Context) error {
  85. return s.dao.Ping(c)
  86. }