58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package cronservice
|
||
|
||
import (
|
||
"log"
|
||
|
||
"github.com/go-co-op/gocron/v2"
|
||
|
||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
|
||
)
|
||
|
||
type RepoService interface {
|
||
ForceUpdate()
|
||
}
|
||
|
||
type Service struct {
|
||
repoService RepoService
|
||
|
||
scheduler gocron.Scheduler
|
||
}
|
||
|
||
func New(repo RepoService) *Service {
|
||
scheduler, err := gocron.NewScheduler()
|
||
if err != nil {
|
||
log.Fatalf("Не удалось создать планировщик: %v", err)
|
||
}
|
||
|
||
return &Service{
|
||
scheduler: scheduler,
|
||
repoService: repo,
|
||
}
|
||
}
|
||
|
||
func (s *Service) SetupCronJobs() {
|
||
_, err := s.scheduler.NewJob(
|
||
gocron.CronJob("0 4 * * *", false),
|
||
gocron.NewTask(func() {
|
||
log := logger.GetLogger()
|
||
log.Info(
|
||
"[cron] force update is started",
|
||
)
|
||
s.repoService.ForceUpdate()
|
||
}),
|
||
)
|
||
if err != nil {
|
||
log.Printf("Не удалось создать задание cron: %v", err)
|
||
}
|
||
}
|
||
|
||
func (s *Service) Start() {
|
||
s.scheduler.Start()
|
||
}
|
||
|
||
func (s *Service) Shutdown() {
|
||
err := s.scheduler.Shutdown()
|
||
if err != nil {
|
||
log.Printf("Не удалось корректно завершить работу планировщика: %v", err)
|
||
}
|
||
}
|