service.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package service
  2. import (
  3. "context"
  4. "go-common/library/net/trace"
  5. "go-common/library/log"
  6. "github.com/robfig/cron"
  7. "go-common/app/job/live/dao-anchor-job/internal/conf"
  8. "go-common/app/job/live/dao-anchor-job/internal/dao"
  9. )
  10. // Service struct
  11. type Service struct {
  12. c *conf.Config
  13. dao *dao.Dao
  14. cron *cron.Cron
  15. }
  16. // New init
  17. func New(c *conf.Config) (s *Service) {
  18. s = &Service{
  19. c: c,
  20. dao: dao.New(c),
  21. cron: cron.New(),
  22. }
  23. if err := s.cron.AddFunc(s.c.CoverControl.CoverCron, s.updateKeyFrame); err != nil {
  24. panic(err)
  25. }
  26. if err := s.cron.AddFunc(s.c.Minute3Control.Minute3Cron, s.minuteDataToDB); err != nil {
  27. panic(err)
  28. }
  29. if err := s.cron.AddFunc(s.c.MinuteControl.MinuteCron, s.minuteDataToCacheList); err != nil {
  30. panic(err)
  31. }
  32. s.cron.Start()
  33. return s
  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. log.Info("Crontab Closed!")
  42. s.cron.Stop()
  43. log.Info("Physical Dao Closed!")
  44. s.dao.Close()
  45. log.Info("tv-job has been closed.")
  46. }
  47. func GetTraceLogCtx(ctx context.Context, title string) (ctxNew context.Context) {
  48. t := trace.New(title)
  49. ctxNew = trace.NewContext(ctx, t)
  50. return
  51. }