service.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/job/live/recommend-job/internal/conf"
  5. "go-common/app/job/live/recommend-job/internal/dao"
  6. "github.com/robfig/cron"
  7. )
  8. // Service struct
  9. type Service struct {
  10. c *conf.Config
  11. dao *dao.Dao
  12. cron *cron.Cron
  13. }
  14. // New init
  15. func New(c *conf.Config) (s *Service) {
  16. s = &Service{
  17. c: c,
  18. dao: dao.New(c),
  19. }
  20. return s
  21. }
  22. // RunCrontab ...
  23. func (s *Service) RunCrontab() {
  24. s.cron = cron.New()
  25. if s.c.ItemCFJob.Schedule == "" {
  26. panic("invalid schedule: " + s.c.ItemCFJob.Schedule)
  27. }
  28. if s.c.UserAreaJob.Schedule == "" {
  29. panic("invalid schedule: " + s.c.UserAreaJob.Schedule)
  30. }
  31. s.cron.AddJob(s.c.ItemCFJob.Schedule, &ItemCFJob{Conf: s.c.ItemCFJob, RedisConf: s.c.Redis, HadoopConf: s.c.Hadoop})
  32. s.cron.AddJob(s.c.UserAreaJob.Schedule, &UserAreaJob{JobConf: s.c.UserAreaJob, RedisConf: s.c.Redis, HadoopConf: s.c.Hadoop})
  33. s.cron.Start()
  34. }
  35. // Ping Service
  36. func (s *Service) Ping(ctx context.Context) (err error) {
  37. return s.dao.Ping(ctx)
  38. }
  39. // Close Service
  40. func (s *Service) Close() {
  41. s.dao.Close()
  42. }