service.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/job/bbq/recall/internal/conf"
  5. "go-common/app/job/bbq/recall/internal/dao"
  6. "go-common/library/log"
  7. "github.com/robfig/cron"
  8. )
  9. // Service struct
  10. type Service struct {
  11. c *conf.Config
  12. dao *dao.Dao
  13. sche *cron.Cron
  14. }
  15. // New init
  16. func New(c *conf.Config) (s *Service) {
  17. s = &Service{
  18. c: c,
  19. dao: dao.New(c),
  20. sche: cron.New(),
  21. }
  22. return s
  23. }
  24. // InitCron .
  25. func (s *Service) InitCron() {
  26. s.sche.AddFunc("@every 3s", s.HeartBeat)
  27. s.sche.AddFunc(s.c.Job.ForwardIndex.Schedule, s.GenForwardIndex)
  28. s.sche.AddFunc(s.c.Job.BloomFilter.Schedule, s.GenBloomFilter)
  29. s.sche.Start()
  30. }
  31. // RunSrv .
  32. func (s *Service) RunSrv(name string) {
  33. log.Info("run job{%s}", name)
  34. switch name {
  35. case s.c.Job.ForwardIndex.JobName:
  36. s.GenForwardIndex()
  37. case s.c.Job.BloomFilter.JobName:
  38. s.GenBloomFilter()
  39. default:
  40. s.HeartBeat()
  41. }
  42. }
  43. // HeartBeat .
  44. func (s *Service) HeartBeat() {
  45. log.Info("alive...")
  46. }
  47. // Ping Service
  48. func (s *Service) Ping(ctx context.Context) (err error) {
  49. return s.dao.Ping(ctx)
  50. }
  51. // Close Service
  52. func (s *Service) Close() {
  53. s.dao.Close()
  54. }