2022-04-27 00:19:05 -04:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-04-28 19:24:21 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-04-27 00:19:05 -04:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
2022-05-02 00:36:27 -04:00
|
|
|
"go.balki.me/tss/log"
|
2022-04-27 00:19:05 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
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()
|
2022-05-02 00:36:27 -04:00
|
|
|
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-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-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-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
|
|
|
|
}
|