aides-repo-api/internal/services/cronservice/service.go
Maxim Slipenko 6693ceabcc
Some checks failed
Format and Lint / format-check (push) Has been cancelled
refactor
2024-12-24 22:40:37 +03:00

69 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cronservice
import (
"github.com/go-co-op/gocron/v2"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
)
type RepoService interface {
ForceUpdate() 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] force update is started",
)
err := s.repoService.ForceUpdate()
if err != nil {
log.Error("[cron] force 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,
})
}
}