2024-12-15 06:16:51 +00:00
|
|
|
|
package cronservice
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-co-op/gocron/v2"
|
2024-12-15 08:39:18 +00:00
|
|
|
|
|
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
|
2024-12-15 06:16:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RepoService interface {
|
|
|
|
|
ForceUpdate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
|
repoService RepoService
|
|
|
|
|
|
|
|
|
|
scheduler gocron.Scheduler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(repo RepoService) *Service {
|
|
|
|
|
scheduler, err := gocron.NewScheduler(gocron.WithLocation(time.UTC))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Не удалось создать планировщик: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
|
scheduler: scheduler,
|
|
|
|
|
repoService: repo,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) SetupCronJobs() {
|
|
|
|
|
_, err := s.scheduler.NewJob(
|
|
|
|
|
gocron.CronJob("* * * * *", false),
|
|
|
|
|
gocron.NewTask(func() {
|
2024-12-15 08:39:18 +00:00
|
|
|
|
log := logger.GetLogger()
|
|
|
|
|
log.Info(
|
|
|
|
|
"Cron run!",
|
|
|
|
|
)
|
2024-12-15 06:16:51 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|