2024-12-15 06:16:51 +00:00
|
|
|
|
package cronservice
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/go-co-op/gocron/v2"
|
2024-12-15 08:39:18 +00:00
|
|
|
|
|
2025-01-07 14:54:16 +00:00
|
|
|
|
"code.aides.space/aides-infra/aides-repo-api/internal/logger"
|
2024-12-15 06:16:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RepoService interface {
|
2025-01-02 09:52:17 +00:00
|
|
|
|
Update(bool) error
|
2024-12-15 06:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
|
repoService RepoService
|
|
|
|
|
|
|
|
|
|
scheduler gocron.Scheduler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(repo RepoService) *Service {
|
2024-12-24 19:40:37 +00:00
|
|
|
|
log := logger.GetLogger()
|
2024-12-15 09:03:11 +00:00
|
|
|
|
scheduler, err := gocron.NewScheduler()
|
2024-12-15 06:16:51 +00:00
|
|
|
|
if err != nil {
|
2024-12-24 19:40:37 +00:00
|
|
|
|
log.Fatal("Не удалось создать планировщик", map[string]interface{}{
|
|
|
|
|
"err": err,
|
|
|
|
|
})
|
2024-12-15 06:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
|
scheduler: scheduler,
|
|
|
|
|
repoService: repo,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) SetupCronJobs() {
|
2024-12-24 19:40:37 +00:00
|
|
|
|
log := logger.GetLogger()
|
2024-12-15 06:16:51 +00:00
|
|
|
|
_, err := s.scheduler.NewJob(
|
2024-12-15 09:03:11 +00:00
|
|
|
|
gocron.CronJob("0 4 * * *", false),
|
2024-12-15 06:16:51 +00:00
|
|
|
|
gocron.NewTask(func() {
|
2024-12-15 08:39:18 +00:00
|
|
|
|
log.Info(
|
2025-01-02 09:52:17 +00:00
|
|
|
|
"[cron] update is started",
|
2024-12-15 08:39:18 +00:00
|
|
|
|
)
|
2025-01-02 09:52:17 +00:00
|
|
|
|
err := s.repoService.Update(false)
|
2024-12-24 19:40:37 +00:00
|
|
|
|
if err != nil {
|
2025-01-02 09:52:17 +00:00
|
|
|
|
log.Error("[cron] update error", map[string]interface{}{
|
2024-12-24 19:40:37 +00:00
|
|
|
|
"err": err,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-12-15 06:16:51 +00:00
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2024-12-24 19:40:37 +00:00
|
|
|
|
log.Fatal("Не удалось создать задание cron", map[string]interface{}{
|
|
|
|
|
"err": err,
|
|
|
|
|
})
|
2024-12-15 06:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Start() {
|
|
|
|
|
s.scheduler.Start()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Shutdown() {
|
2024-12-24 19:40:37 +00:00
|
|
|
|
log := logger.GetLogger()
|
2024-12-15 06:16:51 +00:00
|
|
|
|
err := s.scheduler.Shutdown()
|
|
|
|
|
if err != nil {
|
2024-12-24 19:40:37 +00:00
|
|
|
|
log.Error("Не удалось корректно завершить работу планировщика", map[string]interface{}{
|
|
|
|
|
"err": err,
|
|
|
|
|
})
|
2024-12-15 06:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|