service.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/member-cache/conf"
  6. "go-common/app/job/main/member-cache/dao"
  7. "go-common/library/queue/databus"
  8. )
  9. // Service struct
  10. type Service struct {
  11. c *conf.Config
  12. dao *dao.Dao
  13. memberBinLog *databus.Databus
  14. blockBinLog *databus.Databus
  15. accountNotify *databus.Databus
  16. }
  17. // New init
  18. func New(c *conf.Config) (s *Service) {
  19. s = &Service{
  20. c: c,
  21. dao: dao.New(c),
  22. memberBinLog: databus.New(c.MemberBinLog),
  23. blockBinLog: databus.New(c.BlockBinLog),
  24. accountNotify: databus.New(c.AccountNotify),
  25. }
  26. s.Start(context.Background())
  27. return s
  28. }
  29. // Start to handle requests
  30. func (s *Service) Start(ctx context.Context) {
  31. for i := 0; i < 10; i++ {
  32. go s.memberBinLogproc(context.Background())
  33. }
  34. for i := 0; i < 10; i++ {
  35. go s.blockBinLogproc(context.Background())
  36. }
  37. }
  38. // Ping Service
  39. func (s *Service) Ping(c context.Context) (err error) {
  40. return s.dao.Ping(c)
  41. }
  42. // Close Service
  43. func (s *Service) Close() {
  44. s.dao.Close()
  45. }
  46. // BeautifyMessage is
  47. func BeautifyMessage(msg *databus.Message) string {
  48. pmsg := struct {
  49. Key string `json:"key"`
  50. Value string `json:"value"`
  51. Topic string `json:"topic"`
  52. Partition int32 `json:"partition"`
  53. Offset int64 `json:"offset"`
  54. Timestamp int64 `json:"timestamp"`
  55. }{
  56. Key: msg.Key,
  57. Value: string(msg.Value),
  58. Topic: msg.Topic,
  59. Partition: msg.Partition,
  60. Offset: msg.Offset,
  61. Timestamp: msg.Timestamp,
  62. }
  63. return fmt.Sprintf("%+v", pmsg)
  64. }