First wait, then run

This commit is contained in:
ekzyis 2024-03-18 08:24:54 +01:00
parent 7bf9e6c53c
commit 1416550be9
1 changed files with 10 additions and 21 deletions

31
main.go
View File

@ -8,36 +8,22 @@ import (
"github.com/ekzyis/sn-goapi" "github.com/ekzyis/sn-goapi"
) )
func WaitUntilNextMinute() {
now := time.Now()
dur := now.Truncate(time.Minute).Add(time.Minute).Sub(now)
log.Println("sleeping for", dur.Round(time.Second))
time.Sleep(dur)
}
func WaitUntilNextRun() {
now := time.Now()
dur := now.Truncate(time.Minute).Add(15 * time.Minute).Sub(now)
log.Println("sleeping for", dur.Round(time.Second))
time.Sleep(dur)
}
func SyncStories() { func SyncStories() {
for { for {
now := time.Now()
dur := now.Truncate(time.Minute).Add(time.Minute).Sub(now)
log.Println("[hn] sleeping for", dur.Round(time.Second))
time.Sleep(dur)
stories, err := FetchHackerNewsTopStories() stories, err := FetchHackerNewsTopStories()
if err != nil { if err != nil {
SendErrorToDiscord(err) SendErrorToDiscord(err)
WaitUntilNextMinute()
continue continue
} }
if err := SaveStories(&stories); err != nil { if err := SaveStories(&stories); err != nil {
SendErrorToDiscord(err) SendErrorToDiscord(err)
WaitUntilNextMinute()
continue continue
} }
WaitUntilNextMinute()
} }
} }
@ -49,9 +35,13 @@ func main() {
err error err error
) )
now := time.Now()
dur := now.Truncate(time.Minute).Add(15 * time.Minute).Sub(now)
log.Println("[sn] sleeping for", dur.Round(time.Second))
time.Sleep(dur)
if filtered, err = CurateContentForStackerNews(); err != nil { if filtered, err = CurateContentForStackerNews(); err != nil {
SendErrorToDiscord(err) SendErrorToDiscord(err)
WaitUntilNextRun()
continue continue
} }
@ -73,6 +63,5 @@ func main() {
continue continue
} }
} }
WaitUntilNextRun()
} }
} }