Skip to content

Commit

Permalink
refactored main + writers
Browse files Browse the repository at this point in the history
  • Loading branch information
jakopako committed Jun 9, 2023
1 parent aa50778 commit 7f4edf0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (

var version = "dev"

func runScraper(s scraper.Scraper, itemsChannel chan map[string]interface{}, globalConfig *scraper.GlobalConfig, wg *sync.WaitGroup) {
defer wg.Done()
func runScraper(s *scraper.Scraper, itemsChannel chan map[string]interface{}, globalConfig *scraper.GlobalConfig) {
log.Printf("scraping %s\n", s.Name)
// This could probably be improved. We could pass the channel to
// GetItems instead of waiting for the scraper to finish.
Expand Down Expand Up @@ -134,11 +133,17 @@ func main() {
for _, s := range config.Scrapers {
if *singleScraper == "" || *singleScraper == s.Name {
scraperWg.Add(1)
go runScraper(s, itemsChannel, &config.Global, &scraperWg)
go func(scr scraper.Scraper) {
defer scraperWg.Done()
runScraper(&scr, itemsChannel, &config.Global)
}(s)
}
}
writerWg.Add(1)
go writer.Write(itemsChannel, &writerWg)
go func() {
defer writerWg.Done()
writer.Write(itemsChannel)
}()
scraperWg.Wait()
close(itemsChannel)
writerWg.Wait()
Expand Down
4 changes: 1 addition & 3 deletions output/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"net/http"
"net/url"
"sync"
"time"
)

Expand All @@ -25,8 +24,7 @@ func NewAPIWriter(wc *WriterConfig) *APIWriter {
}
}

func (f *APIWriter) Write(items chan map[string]interface{}, wg *sync.WaitGroup) {
defer wg.Done()
func (f *APIWriter) Write(items chan map[string]interface{}) {
client := &http.Client{
Timeout: time.Second * 10,
}
Expand Down
4 changes: 1 addition & 3 deletions output/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"log"
"os"
"sync"
)

type FileWriter struct {
Expand All @@ -18,8 +17,7 @@ func NewFileWriter(wc *WriterConfig) *FileWriter {
writerConfig: wc,
}
}
func (fr *FileWriter) Write(items chan map[string]interface{}, wg *sync.WaitGroup) {
defer wg.Done()
func (fr *FileWriter) Write(items chan map[string]interface{}) {
f, err := os.Create(fr.writerConfig.FilePath)
if err != nil {
log.Fatalf("FileWriter ERROR while trying to open file: %v", err)
Expand Down
4 changes: 1 addition & 3 deletions output/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"encoding/json"
"fmt"
"log"
"sync"
)

type StdoutWriter struct{}

func (s *StdoutWriter) Write(items chan map[string]interface{}, wg *sync.WaitGroup) {
defer wg.Done()
func (s *StdoutWriter) Write(items chan map[string]interface{}) {
for item := range items {
// We cannot use the following line of code because it automatically replaces certain html characters
// with the corresponding Unicode replacement rune.
Expand Down
4 changes: 1 addition & 3 deletions output/writer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package output

import "sync"

type Writer interface {
// if a writer encounters a fatal error it should call log.Fatalf
// to prevent the crawler from uselessly continuing to run.
Write(itemsList chan map[string]interface{}, wg *sync.WaitGroup)
Write(itemsList chan map[string]interface{})
}

// .WriterConfig defines the necessary paramters to make a new writer
Expand Down

0 comments on commit 7f4edf0

Please sign in to comment.