From 275f71c4d6698b8ffb44841e21c191572090569f Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Wed, 27 Apr 2022 00:19:05 -0400 Subject: [PATCH] scheduler initial impl --- app/app.go | 48 +++++++++++++++++++++++++++------------- app/config.go | 6 ++--- app/download.go | 5 +++++ app/schedule.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ app/schedule_test.go | 28 ++++++++++++++++++++++++ go.mod | 6 +++++ go.sum | 8 +++++++ main.go | 22 ------------------- 8 files changed, 135 insertions(+), 40 deletions(-) create mode 100644 app/download.go create mode 100644 app/schedule.go create mode 100644 app/schedule_test.go diff --git a/app/app.go b/app/app.go index ca39644..a418109 100644 --- a/app/app.go +++ b/app/app.go @@ -1,20 +1,38 @@ package app -import ( - "fmt" - "log" - - "go.balki.me/tss/config" -) - func Run(configPath string) { - cfg, err := config.Parse(configPath) - if err != nil { - log.Fatal(err) - } + /* + cfg, err := ParseConfig(configPath) + if err != nil { + log.Fatal(err) + } - for _, feed := range c.Feeds { - log.Println("Processing feed", feed.Name) - } - fmt.Println(configPath) + for _, feed := range cfg.Feeds { + log.Println("Processing feed", feed.Name) + data, err := Download(feed.Url, feed.Proxy) + if err != nil { + log.Fatal(err) + } + } + + for _, feed := range c.Feeds { + log.Println("Processing feed", feed.Name) + + links, err := parseFeed(data) + if err != nil { + log.Fatal(err) + } + + for _, link := range links { + if alreadySent(link) { + continue + } + err := sendTelegram(link, feed.Channel, feed.Rhash) + if err != nil { + log.Fatal(err) + } + } + } + fmt.Println(configPath) + */ } diff --git a/app/config.go b/app/config.go index 6608ab1..dab6553 100644 --- a/app/config.go +++ b/app/config.go @@ -1,4 +1,4 @@ -package config +package app import ( "os" @@ -17,8 +17,8 @@ type Config struct { } `yaml:"feeds"` } -func Parse(configPath string) (*Config, error) { - cfg, err := os.ReadFile("tss.yaml") +func ParseConfig(configPath string) (*Config, error) { + cfg, err := os.ReadFile(configPath) if err != nil { return nil, err } diff --git a/app/download.go b/app/download.go new file mode 100644 index 0000000..2e6c30a --- /dev/null +++ b/app/download.go @@ -0,0 +1,5 @@ +package app + +func Download(url string, proxy string) ([]byte, error) { + return nil, nil +} diff --git a/app/schedule.go b/app/schedule.go new file mode 100644 index 0000000..9813999 --- /dev/null +++ b/app/schedule.go @@ -0,0 +1,52 @@ +package app + +import ( + "os" + "time" + + "github.com/robfig/cron/v3" + "gopkg.in/yaml.v3" +) + +type Scheduler struct { + filePath string + lastSuccessTime map[string]time.Time +} + +func NewScheduler(filePath string) (*Scheduler, error) { + s := Scheduler{filePath: filePath} + data, err := os.ReadFile(filePath) + if err != nil { + return nil, err + } + err = yaml.Unmarshal(data, &s.lastSuccessTime) + if err != nil { + return nil, err + } + return &s, nil +} + +func (s *Scheduler) Save() error { + data, err := yaml.Marshal(&s.lastSuccessTime) + if err != nil { + return err + } + return os.WriteFile(s.filePath, data, 0644) +} + +func (s *Scheduler) Good(name string) { + s.lastSuccessTime[name] = time.Now() +} + +func (s *Scheduler) ShouldDownload(name string, scheduleSpec string) (bool, error) { + 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 +} diff --git a/app/schedule_test.go b/app/schedule_test.go new file mode 100644 index 0000000..a966046 --- /dev/null +++ b/app/schedule_test.go @@ -0,0 +1,28 @@ +package app + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" +) + +func TestYamlMap(t *testing.T) { + yesterday := time.Now().Add(-24 * time.Hour) + m := map[string]time.Time{ + "Bala": time.Now(), + "Linus": yesterday, + } + out, err := yaml.Marshal(&m) + if err != nil { + t.Fatalf("got err: %v", err) + } + // assert.Equal(t, nil, fmt.Sprintf("%s", out)) + var m2 map[string]time.Time + err = yaml.Unmarshal(out, &m2) + assert.NoError(t, err) + + assert.Equal(t, "foobar", fmt.Sprint(m2["Bala"])) +} diff --git a/go.mod b/go.mod index 46f4248..1ef6605 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,11 @@ go 1.17 require ( github.com/robfig/cron/v3 v3.0.1 + github.com/stretchr/testify v1.7.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect +) diff --git a/go.sum b/go.sum index 2149a0c..4b7a289 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,14 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 6f23cde..d8ec336 100644 --- a/main.go +++ b/main.go @@ -9,28 +9,6 @@ import ( func main() { var err error - for _, feed := range c.Feeds { - log.Println("Processing feed", feed.Name) - data, err := download(feed.Url) - if err != nil { - log.Fatal(err) - } - - links, err := parseFeed(data) - if err != nil { - log.Fatal(err) - } - - for _, link := range links { - if alreadySent(link) { - continue - } - err := sendTelegram(link, feed.Channel, feed.Rhash) - if err != nil { - log.Fatal(err) - } - } - } } func download(url string) ([]byte, error) {