aides-repo-api/internal/services/cronservice/service.go
Maxim Slipenko e4f9743dea
Some checks failed
Format and Lint / format-check (push) Failing after 2s
Format and Lint / test (push) Successful in 52s
chore: change code.alt-gnome to code.aides.space
2025-01-07 17:54:16 +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.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,
})
}
}