tss/schedule/schedule.go

85 lines
1.7 KiB
Go
Raw Normal View History

2022-06-12 18:46:16 -04:00
package schedule
2022-04-27 00:19:05 -04:00
import (
2022-04-28 19:24:21 -04:00
"errors"
"fmt"
2022-04-27 00:19:05 -04:00
"os"
2022-05-13 12:55:45 -04:00
"sync"
2022-04-27 00:19:05 -04:00
"time"
2022-06-12 18:46:16 -04:00
"github.com/go-logr/logr"
2022-04-27 00:19:05 -04:00
"github.com/robfig/cron/v3"
"gopkg.in/yaml.v3"
)
2022-06-12 18:46:16 -04:00
var log = logr.Discard()
func SetLogger(log logr.Logger) {
log = log
}
2022-05-02 20:20:48 -04:00
type Scheduler interface {
ShouldDownload(name string, scheduleSpec string) (bool, error)
Save() error
Good(name string)
}
type scheduler struct {
2022-04-27 00:19:05 -04:00
filePath string
2022-05-13 12:55:45 -04:00
mutex sync.Mutex
2022-04-27 00:19:05 -04:00
lastSuccessTime map[string]time.Time
}
2022-05-02 20:20:48 -04:00
func NewScheduler(filePath string) (Scheduler, error) {
2022-05-02 23:23:56 -04:00
s := scheduler{filePath: filePath, lastSuccessTime: map[string]time.Time{}}
2022-04-27 00:19:05 -04:00
data, err := os.ReadFile(filePath)
if err != nil {
2022-04-28 19:24:21 -04:00
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
f, err := os.Create(filePath)
if err != nil {
return nil, fmt.Errorf("path:%v does not exist and unable to create: err: %w", filePath, err)
}
f.Close()
log.Info("scheduler file does not exist, will be created", "path", filePath)
2022-04-28 19:24:21 -04:00
} else {
err = yaml.Unmarshal(data, &s.lastSuccessTime)
if err != nil {
return nil, err
}
2022-04-27 00:19:05 -04:00
}
return &s, nil
}
2022-05-02 20:20:48 -04:00
func (s *scheduler) Save() error {
2022-05-13 12:55:45 -04:00
s.mutex.Lock()
defer s.mutex.Unlock()
2022-04-27 00:19:05 -04:00
data, err := yaml.Marshal(&s.lastSuccessTime)
if err != nil {
return err
}
return os.WriteFile(s.filePath, data, 0644)
}
2022-05-02 20:20:48 -04:00
func (s *scheduler) Good(name string) {
2022-05-13 12:55:45 -04:00
s.mutex.Lock()
defer s.mutex.Unlock()
2022-04-27 00:19:05 -04:00
s.lastSuccessTime[name] = time.Now()
}
2022-05-02 20:20:48 -04:00
func (s *scheduler) ShouldDownload(name string, scheduleSpec string) (bool, error) {
2022-05-13 12:55:45 -04:00
s.mutex.Lock()
defer s.mutex.Unlock()
2022-04-27 00:19:05 -04:00
lastSuccess, ok := s.lastSuccessTime[name]
if !ok {
return true, nil
}
cron, err := cron.ParseStandard(scheduleSpec)
if err != nil {
return false, err
}
n := cron.Next(lastSuccess)
return n.Before(time.Now()), nil
}