-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcurrent.go
84 lines (68 loc) · 1.64 KB
/
concurrent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// +build ignore
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
poller "github.com/SunSince90/website-poller"
"gopkg.in/yaml.v2"
)
// This example shows how to start multiple pollers and manage their life
// cycles correctly.
func main() {
// -- Load the file
filePath := "./path/to/pages.yaml"
yamlFile, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("error while reading file:", err)
os.Exit(1)
}
// -- Set up the pollers
var pages []poller.Page
var pollers []poller.Poller
err = yaml.Unmarshal(yamlFile, &pages)
if err != nil {
fmt.Println("error while unmarshalling file:", err)
os.Exit(1)
}
for _, page := range pages {
p, err := poller.New(&page)
if err != nil {
fmt.Println("a page contains errors:", err, ", skipping...")
continue
}
p.SetHandlerFunc(handleResponse)
pollers = append(pollers, p)
}
// -- Start the pollers
ctx, canc := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(len(pollers))
for _, p := range pollers {
go func(currentPoller poller.Poller) {
defer wg.Done()
currentPoller.Start(ctx, true)
}(p)
}
// -- Graceful shutdown
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
fmt.Println("exit requested")
canc() // Cancel all the pollers
wg.Wait() // Wait for all the pollers to finish before exiting!
fmt.Println("goodbye!")
}
func handleResponse(id string, resp *http.Response, err error) {
// Scrape the website...
}