scheduler initial impl
This commit is contained in:
parent
a2085625aa
commit
275f71c4d6
48
app/app.go
48
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)
|
||||
*/
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
5
app/download.go
Normal file
5
app/download.go
Normal file
@ -0,0 +1,5 @@
|
||||
package app
|
||||
|
||||
func Download(url string, proxy string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
52
app/schedule.go
Normal file
52
app/schedule.go
Normal file
@ -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
|
||||
}
|
28
app/schedule_test.go
Normal file
28
app/schedule_test.go
Normal file
@ -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"]))
|
||||
}
|
6
go.mod
6
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
|
||||
)
|
||||
|
8
go.sum
8
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=
|
||||
|
22
main.go
22
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user