service.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/apm/conf"
  5. "go-common/app/admin/main/apm/dao"
  6. "go-common/app/admin/main/apm/model/tree"
  7. "go-common/app/admin/main/apm/model/ut"
  8. "go-common/app/tool/saga/service/gitlab"
  9. bm "go-common/library/net/http/blademaster"
  10. "sync"
  11. "github.com/jinzhu/gorm"
  12. "github.com/robfig/cron"
  13. )
  14. // Service is a service.
  15. type Service struct {
  16. c *conf.Config
  17. dao *dao.Dao
  18. DB *gorm.DB
  19. DBDatabus *gorm.DB
  20. DBCanal *gorm.DB
  21. client *bm.Client
  22. // tree cache
  23. treeCache map[string][]*tree.Node
  24. treeLock sync.RWMutex
  25. // cron cron
  26. cron *cron.Cron
  27. // discoveryID cache
  28. discoveryIDCache map[string]*tree.Resd
  29. discoveryIDLock sync.RWMutex
  30. ranksCache *ut.RanksCache
  31. appsCache *ut.AppsCache
  32. // dapper proxy
  33. dapperProxy *dapperProxy
  34. // gitlab api conf
  35. gitlab *gitlab.Gitlab
  36. }
  37. // New new a service
  38. func New(c *conf.Config) (s *Service) {
  39. s = &Service{
  40. c: c,
  41. dao: dao.New(c),
  42. client: bm.NewClient(c.HTTPClient),
  43. // tree cache
  44. treeCache: map[string][]*tree.Node{},
  45. // discoveryID cache
  46. discoveryIDCache: map[string]*tree.Resd{},
  47. // ranks cache
  48. ranksCache: &ut.RanksCache{},
  49. appsCache: &ut.AppsCache{},
  50. // cron cron
  51. cron: cron.New(),
  52. }
  53. s.gitlab = gitlab.New(conf.Conf.Gitlab.API, conf.Conf.Gitlab.Token)
  54. s.DB = s.dao.DB
  55. s.DBDatabus = s.dao.DBDatabus
  56. s.DBCanal = s.dao.DBCanal
  57. if err := s.cron.AddFunc(s.c.Cron.Crontab, s.taskAddMonitor); err != nil {
  58. panic(err)
  59. }
  60. if err := s.cron.AddFunc(s.c.Cron.Crontab, s.taskAddCache); err != nil {
  61. panic(err)
  62. }
  63. if err := s.cron.AddFunc(s.c.Cron.CrontabRepo, s.taskRankWechatReport); err != nil {
  64. panic(err)
  65. }
  66. if err := s.cron.AddFunc(s.c.Cron.CrontabRepo, s.taskWeeklyWechatReport); err != nil {
  67. panic(err)
  68. }
  69. dp, err := newDapperProxy(c.Host.DapperCo)
  70. if err != nil {
  71. panic(err)
  72. }
  73. s.dapperProxy = dp
  74. s.cron.Start()
  75. go s.taskAddCache()
  76. return
  77. }
  78. // Ping ping db,
  79. func (s *Service) Ping(c context.Context) (err error) {
  80. return
  81. }
  82. // Close close resource.
  83. func (s *Service) Close() {
  84. s.dao.Close()
  85. }