aides-repo-api/internal/services/cronservice/service.go
Maxim Slipenko 37007956ab
All checks were successful
Format and Lint / format-check (push) Successful in 2m57s
Format and Lint / test (push) Successful in 1m21s
fix: do not update repo if no changes
2025-01-02 12:52:17 +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 {
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,
})
}
}