Maxim Slipenko
6693ceabcc
Some checks failed
Format and Lint / format-check (push) Has been cancelled
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
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,
|
||
})
|
||
}
|
||
}
|