service.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/point/conf"
  6. "go-common/app/admin/main/point/dao"
  7. "go-common/app/admin/main/point/model"
  8. pointrpc "go-common/app/service/main/point/rpc/client"
  9. "go-common/library/log"
  10. )
  11. // Service struct
  12. type Service struct {
  13. c *conf.Config
  14. dao *dao.Dao
  15. pointRPC *pointrpc.Service
  16. appMap map[int64]string
  17. }
  18. // New init
  19. func New(c *conf.Config) (s *Service) {
  20. s = &Service{
  21. c: c,
  22. dao: dao.New(c),
  23. pointRPC: pointrpc.New(c.RPC.Point),
  24. appMap: make(map[int64]string),
  25. }
  26. go s.loadappinfoproc()
  27. return s
  28. }
  29. // Ping Service
  30. func (s *Service) Ping(c context.Context) (err error) {
  31. return s.dao.Ping(c)
  32. }
  33. // Close Service
  34. func (s *Service) Close() {
  35. s.dao.Close()
  36. }
  37. func (s *Service) loadappinfoproc() {
  38. for {
  39. s.loadAppInfo()
  40. time.Sleep(time.Minute * 2)
  41. }
  42. }
  43. func (s *Service) loadAppInfo() {
  44. var (
  45. res []*model.AppInfo
  46. err error
  47. )
  48. if res, err = s.dao.AllAppInfo(context.TODO()); err != nil {
  49. log.Error("loadAppInfo AllAppInfo error(%v)", err)
  50. return
  51. }
  52. aMap := make(map[int64]string, len(res))
  53. for _, v := range res {
  54. aMap[v.ID] = v.Name
  55. }
  56. s.appMap = aMap
  57. }