service.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "go-common/app/job/main/usersuit/conf"
  6. medalDao "go-common/app/job/main/usersuit/dao/medal"
  7. pendantDao "go-common/app/job/main/usersuit/dao/pendant"
  8. "go-common/library/log"
  9. "go-common/library/queue/databus"
  10. "github.com/robfig/cron"
  11. )
  12. // Service struct of service.
  13. type Service struct {
  14. pendantDao *pendantDao.Dao
  15. medalDao *medalDao.Dao
  16. // conf
  17. c *conf.Config
  18. accountNotifyPub *databus.Databus
  19. vipBinLogSub *databus.Databus
  20. notifych chan func()
  21. // wait group
  22. wg sync.WaitGroup
  23. }
  24. // New create service instance and return.
  25. func New(c *conf.Config) (s *Service) {
  26. s = &Service{
  27. c: c,
  28. pendantDao: pendantDao.New(c),
  29. medalDao: medalDao.New(c),
  30. accountNotifyPub: databus.New(c.Databus.AccountNotify),
  31. vipBinLogSub: databus.New(c.Databus.VipBinLog),
  32. notifych: make(chan func(), 10240),
  33. }
  34. // this is function
  35. go s.startexpireproc()
  36. s.wg.Add(1)
  37. go s.notifyproc()
  38. s.wg.Add(1)
  39. go s.vipconsumerproc()
  40. t := cron.New()
  41. if len(s.c.Properties.MedalCron) != 0 {
  42. t.AddFunc(s.c.Properties.MedalCron, s.cronUpNameplate)
  43. }
  44. t.Start()
  45. return
  46. }
  47. func (s *Service) addNotify(f func()) {
  48. select {
  49. case s.notifych <- f:
  50. default:
  51. log.Warn("addNotify chan full")
  52. }
  53. }
  54. // notifyproc nofity clear cache
  55. func (s *Service) notifyproc() {
  56. defer s.wg.Done()
  57. for {
  58. f := <-s.notifych
  59. f()
  60. }
  61. }
  62. // Close dao.
  63. func (s *Service) Close() {
  64. if s.pendantDao != nil {
  65. s.pendantDao.Close()
  66. s.medalDao.Close()
  67. }
  68. s.wg.Wait()
  69. }
  70. // Ping check server ok.
  71. func (s *Service) Ping(c context.Context) (err error) {
  72. if err = s.pendantDao.Ping(c); err != nil {
  73. return
  74. }
  75. return
  76. }
  77. // PDTStatStep PDT Stat step
  78. type PDTStatStep struct {
  79. Start, End, Step int64
  80. }
  81. // PDTGHisStep PDT G his
  82. type PDTGHisStep struct {
  83. Start, End, Step int64
  84. }
  85. // PDTOHisStep Order his
  86. type PDTOHisStep struct {
  87. Start, End, Step int64
  88. }