Skip to content

Commit

Permalink
feat: use go routines to make it faster
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzFelix committed Sep 25, 2023
1 parent 9aa3dd6 commit f4f0efc
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions wbmalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,42 @@ type website struct {
func main() {
configuration := readConfiguration()
initializeWebsites(configuration)
goToSleep()
ch := make(chan struct{})

for {
for i := 0; i < len(websites); i++ {
checkWebsite(&websites[i])
go checkWebsite(&websites[i], ch)
}
for i := 0; i < len(websites); i++ {
<-ch
}
goToSleep()
}
}

func initializeWebsites(configuration configuration) {
ch := make(chan struct{})
websites = configuration.Websites
interval = configuration.Interval

for i := 0; i < len(websites); i++ {
createInitialSnapshot(&websites[i])
go createInitialSnapshot(&websites[i], ch)

}

for i := 0; i < len(websites); i++ {
<-ch
}
goToSleep()
}

func createInitialSnapshot(website *website) {
func createInitialSnapshot(website *website, ch chan struct{}) {
content, error := getWebsiteAsString(website)
if error == nil {
website.Snapshot = content
log.Println("Created initial snapshot for " + website.Name)
}
ch <- struct{}{}
}

func getWebsiteAsString(website *website) (string, error) {
Expand All @@ -76,18 +87,20 @@ func getWebsiteAsString(website *website) (string, error) {
return content, nil
}

func checkWebsite(website *website) {
func checkWebsite(website *website, ch chan struct{}) {
content, error := getWebsiteAsString(website)
if error != nil {
return
}
if website.Snapshot != content {
website.Snapshot = content
printContentChangeMsg(website)
playSound()
} else {
log.Println("No changes for " + website.Name)

if error == nil {
if website.Snapshot != content {
website.Snapshot = content
printContentChangeMsg(website)
playSound()
} else {
log.Println("No changes for " + website.Name)
}
}

ch <- struct{}{}
}

func printContentChangeMsg(website *website) {
Expand Down

0 comments on commit f4f0efc

Please sign in to comment.