package cronservice import ( "github.com/go-co-op/gocron/v2" "code.aides.space/aides-infra/aides-repo-api/internal/logger" ) type RepoService interface { Update(bool) error } type Service struct { repoService RepoService scheduler gocron.Scheduler } func New(repo RepoService) *Service { log := logger.GetLogger() scheduler, err := gocron.NewScheduler() if err != nil { log.Fatal("Не удалось создать планировщик", map[string]interface{}{ "err": err, }) } return &Service{ scheduler: scheduler, repoService: repo, } } func (s *Service) SetupCronJobs() { log := logger.GetLogger() _, err := s.scheduler.NewJob( gocron.CronJob("0 4 * * *", false), gocron.NewTask(func() { log.Info( "[cron] update is started", ) err := s.repoService.Update(false) if err != nil { log.Error("[cron] update error", map[string]interface{}{ "err": err, }) } }), ) if err != nil { log.Fatal("Не удалось создать задание cron", map[string]interface{}{ "err": err, }) } } func (s *Service) Start() { s.scheduler.Start() } func (s *Service) Shutdown() { log := logger.GetLogger() err := s.scheduler.Shutdown() if err != nil { log.Error("Не удалось корректно завершить работу планировщика", map[string]interface{}{ "err": err, }) } }