From 4270b9ae24fb677a7b70916137fbd942f6240658 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Fri, 13 May 2022 12:55:45 -0400 Subject: [PATCH] concurrent download --- TODO.md | 4 ++-- app/app.go | 9 ++++++++- app/schedule.go | 8 ++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 0b730e4..408799f 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ -## MVP +## MVP DONE * ✓ Write TODO * ✓ Cron @@ -14,7 +14,7 @@ * ✓ Wrap content inside item ⚠️ atom calls it entry * ✓ Implement atom * ✓ Cleanup -* Show Feed name instead of Link +* ✓ Show Feed name instead of Link * ✓ Make Rhash optional ## Issues diff --git a/app/app.go b/app/app.go index df22099..3244e51 100644 --- a/app/app.go +++ b/app/app.go @@ -3,6 +3,7 @@ package app import ( "io" "net/http" + "sync" "time" "go.balki.me/tss/log" @@ -36,10 +37,16 @@ func Run(configPath string) { tgram := telegram.NewTelegramSender(tgramProxy, cfg.TelegramAuthToken) + wg := sync.WaitGroup{} for _, feed := range cfg.Feeds { log.Info("processing feed", "feed", feed.Name) - ProcessFeed(feed, scheduler, cfg.DbDir, tgram) + wg.Add(1) + go func() { + ProcessFeed(feed, scheduler, cfg.DbDir, tgram) + wg.Done() + }() } + wg.Wait() } func ProcessFeed(feed FeedCfg, scheduler Scheduler, dbDir string, tgram telegram.TelegramSender) { diff --git a/app/schedule.go b/app/schedule.go index 0f9be77..b9893aa 100644 --- a/app/schedule.go +++ b/app/schedule.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "sync" "time" "github.com/robfig/cron/v3" @@ -19,6 +20,7 @@ type Scheduler interface { type scheduler struct { filePath string + mutex sync.Mutex lastSuccessTime map[string]time.Time } @@ -45,6 +47,8 @@ func NewScheduler(filePath string) (Scheduler, error) { } func (s *scheduler) Save() error { + s.mutex.Lock() + defer s.mutex.Unlock() data, err := yaml.Marshal(&s.lastSuccessTime) if err != nil { return err @@ -53,10 +57,14 @@ func (s *scheduler) Save() error { } func (s *scheduler) Good(name string) { + s.mutex.Lock() + defer s.mutex.Unlock() s.lastSuccessTime[name] = time.Now() } func (s *scheduler) ShouldDownload(name string, scheduleSpec string) (bool, error) { + s.mutex.Lock() + defer s.mutex.Unlock() lastSuccess, ok := s.lastSuccessTime[name] if !ok { return true, nil