service.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/merlin/conf"
  5. "go-common/app/admin/ep/merlin/dao"
  6. "go-common/library/sync/pipeline/fanout"
  7. "github.com/robfig/cron"
  8. )
  9. // Service struct
  10. type Service struct {
  11. c *conf.Config
  12. dao *dao.Dao
  13. cron *cron.Cron
  14. deviceChan *fanout.Fanout
  15. }
  16. // New init.
  17. func New(c *conf.Config) (s *Service) {
  18. s = &Service{
  19. c: c,
  20. dao: dao.New(c),
  21. deviceChan: fanout.New("deviceChan", fanout.Worker(1), fanout.Buffer(1024)),
  22. }
  23. scheduler := c.Scheduler
  24. if scheduler.Active {
  25. s.cron = cron.New()
  26. if err := s.cron.AddFunc(scheduler.GetExpiredMachinesTime, s.taskGetExpiredMachinesIntoTask); err != nil {
  27. panic(err)
  28. }
  29. if err := s.cron.AddFunc(scheduler.SendTaskMailMachinesWillExpiredTime, s.taskSendTaskMailMachinesWillExpired); err != nil {
  30. panic(err)
  31. }
  32. if err := s.cron.AddFunc(scheduler.DeleteExpiredMachinesInTask, s.taskDeleteExpiredMachines); err != nil {
  33. panic(err)
  34. }
  35. if err := s.cron.AddFunc(scheduler.CheckMachinesStatusInTask, s.taskMachineStatus); err != nil {
  36. panic(err)
  37. }
  38. if err := s.cron.AddFunc(scheduler.UpdateMobileDeviceInTask, s.taskSyncMobileDeviceList); err != nil {
  39. panic(err)
  40. }
  41. if err := s.cron.AddFunc(scheduler.UpdateSnapshotStatusInDoing, s.taskUpdateSnapshotStatusInDoing); err != nil {
  42. panic(err)
  43. }
  44. s.cron.Start()
  45. }
  46. return
  47. }
  48. // Close Service.
  49. func (s *Service) Close() {
  50. s.dao.Close()
  51. }
  52. // Ping check server ok.
  53. func (s *Service) Ping(c context.Context) (err error) {
  54. err = s.dao.Ping(c)
  55. return
  56. }
  57. // ConfVersion Conf Version.
  58. func (s *Service) ConfVersion(c context.Context) string {
  59. return conf.Conf.Version
  60. }