Remove unnecessary goroutines
This commit is contained in:
parent
0285c5a53a
commit
7bf9e6c53c
38
main.go
38
main.go
|
@ -8,13 +8,6 @@ import (
|
||||||
"github.com/ekzyis/sn-goapi"
|
"github.com/ekzyis/sn-goapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WaitUntilNextHour() {
|
|
||||||
now := time.Now()
|
|
||||||
dur := now.Truncate(time.Hour).Add(time.Hour).Sub(now)
|
|
||||||
log.Println("sleeping for", dur.Round(time.Second))
|
|
||||||
time.Sleep(dur)
|
|
||||||
}
|
|
||||||
|
|
||||||
func WaitUntilNextMinute() {
|
func WaitUntilNextMinute() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
dur := now.Truncate(time.Minute).Add(time.Minute).Sub(now)
|
dur := now.Truncate(time.Minute).Add(time.Minute).Sub(now)
|
||||||
|
@ -29,35 +22,6 @@ func WaitUntilNextRun() {
|
||||||
time.Sleep(dur)
|
time.Sleep(dur)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckNotifications() {
|
|
||||||
var prevHasNewNotes bool
|
|
||||||
for {
|
|
||||||
log.Println("Checking notifications ...")
|
|
||||||
hasNewNotes, err := sn.CheckNotifications()
|
|
||||||
if err != nil {
|
|
||||||
SendErrorToDiscord(err)
|
|
||||||
} else {
|
|
||||||
if !prevHasNewNotes && hasNewNotes {
|
|
||||||
// only send embed on "rising edge"
|
|
||||||
SendNotificationsEmbedToDiscord()
|
|
||||||
log.Println("Forwarded notifications to monitoring")
|
|
||||||
} else if hasNewNotes {
|
|
||||||
log.Println("Notifications already forwarded")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
prevHasNewNotes = hasNewNotes
|
|
||||||
WaitUntilNextRun()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SessionKeepAlive() {
|
|
||||||
for {
|
|
||||||
log.Println("Refresh session using GET /api/auth/session ...")
|
|
||||||
sn.RefreshSession()
|
|
||||||
WaitUntilNextHour()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SyncStories() {
|
func SyncStories() {
|
||||||
for {
|
for {
|
||||||
stories, err := FetchHackerNewsTopStories()
|
stories, err := FetchHackerNewsTopStories()
|
||||||
|
@ -78,8 +42,6 @@ func SyncStories() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go CheckNotifications()
|
|
||||||
go SessionKeepAlive()
|
|
||||||
go SyncStories()
|
go SyncStories()
|
||||||
for {
|
for {
|
||||||
var (
|
var (
|
||||||
|
|
Loading…
Reference in New Issue