service.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/spy/conf"
  6. spydao "go-common/app/admin/main/spy/dao"
  7. "go-common/app/admin/main/spy/model"
  8. "go-common/library/log"
  9. )
  10. // Service biz service def.
  11. type Service struct {
  12. c *conf.Config
  13. spyDao *spydao.Dao
  14. allEventName map[int64]string
  15. loadEventTick time.Duration
  16. }
  17. // New new a Service and return.
  18. func New(c *conf.Config) (s *Service) {
  19. s = &Service{
  20. c: c,
  21. spyDao: spydao.New(c),
  22. allEventName: make(map[int64]string),
  23. loadEventTick: time.Duration(c.Property.LoadEventTick),
  24. }
  25. s.loadeventname()
  26. go s.loadeventproc()
  27. return
  28. }
  29. // Ping check dao health.
  30. func (s *Service) Ping(c context.Context) (err error) {
  31. return s.spyDao.Ping(c)
  32. }
  33. // Wait wait all closed.
  34. func (s *Service) Wait() {
  35. }
  36. // Close close all dao.
  37. func (s *Service) Close() {
  38. s.spyDao.Close()
  39. }
  40. func (s *Service) loadeventname() (err error) {
  41. var (
  42. c = context.TODO()
  43. es []*model.Event
  44. )
  45. es, err = s.spyDao.AllEvent(c)
  46. if err != nil {
  47. log.Error("loadeventname allevent error(%v)", err)
  48. return
  49. }
  50. tmp := make(map[int64]string, len(es))
  51. for _, e := range es {
  52. tmp[e.ID] = e.NickName
  53. }
  54. s.allEventName = tmp
  55. log.V(2).Info("loadeventname (%v) load success", tmp)
  56. return
  57. }
  58. func (s *Service) loadeventproc() {
  59. for {
  60. time.Sleep(s.loadEventTick)
  61. s.loadeventname()
  62. }
  63. }