service.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package service
  2. import (
  3. "context"
  4. "runtime"
  5. "go-common/app/job/main/mcn/conf"
  6. "go-common/app/job/main/mcn/dao"
  7. accgrpc "go-common/app/service/main/account/api"
  8. "go-common/library/sync/pipeline/fanout"
  9. "github.com/pkg/errors"
  10. "github.com/robfig/cron"
  11. )
  12. // Service struct
  13. type Service struct {
  14. c *conf.Config
  15. dao *dao.Dao
  16. // rpc
  17. accGRPC accgrpc.AccountClient
  18. worker *fanout.Fanout
  19. }
  20. // New init
  21. func New(c *conf.Config) (s *Service) {
  22. s = &Service{
  23. c: c,
  24. dao: dao.New(c),
  25. worker: fanout.New("cache", fanout.Worker(runtime.NumCPU()), fanout.Buffer(1024)),
  26. }
  27. var err error
  28. if s.accGRPC, err = accgrpc.NewClient(c.GRPCClient.Account); err != nil {
  29. panic(errors.WithMessage(err, "Failed to dial account service"))
  30. }
  31. if err := s.initEmailTemplate(); err != nil {
  32. panic(err)
  33. }
  34. t := cron.New()
  35. t.AddFunc(c.Property.UpMcnSignStateCron, s.UpMcnSignStateCron)
  36. t.AddFunc(c.Property.UpMcnUpStateCron, s.UpMcnUpStateCron)
  37. t.AddFunc(c.Property.UpExpirePayCron, s.UpExpirePayCron)
  38. //t.AddFunc(c.Property.UpMcnDataSummaryCron, s.UpMcnDataSummaryCron)
  39. t.AddFunc(c.Property.McnRecommendCron, s.McnRecommendCron)
  40. t.AddFunc(c.Property.DealFailRecommendCron, s.DealFailRecommendCron)
  41. t.AddFunc(c.Property.CheckMcnSignUpDueCron, s.CheckDateDueCron)
  42. t.Start()
  43. return s
  44. }
  45. // Ping Service
  46. func (s *Service) Ping(c context.Context) (err error) {
  47. return s.dao.Ping(c)
  48. }
  49. // Close Service
  50. func (s *Service) Close() {
  51. s.dao.Close()
  52. s.worker.Close()
  53. }