-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.go
75 lines (57 loc) · 1.71 KB
/
fetch.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
package cadastre
import "time"
import "sync"
import "log"
// Represents the fetcher, which runs continuously, sucking up data from the specified hosts
// and persisting it to the storage engine for later retrieval.
type Fetcher struct {
Configuration *Configuration
running bool
fetchGuard sync.WaitGroup
}
func (me *Fetcher) Start() error {
me.running = false
processListFetcher := func(server Server) {
// Increment the fetcher guard.
me.fetchGuard.Add(1)
for {
// See if we should still be running.
if !me.running {
break
}
// Wait our fetch interval before we proceed.
time.Sleep(me.Configuration.FetchInterval)
// Check again to see if we should be running, as we could have just sat through
// a moderately-long fetch interval and have the process waiting on us to stop.
if !me.running {
break
}
snapshot := &Snapshot{}
if err := snapshot.TakeSnapshot(server); err != nil {
log.Printf("error: failed to take snapshot for host '%s'! %s", server.InternalName, err)
} else {
// Persist it to our datastore.
me.Configuration.Storage.Persist(server.InternalName, snapshot)
}
}
// All done, so decrement the fetcher guard.
me.fetchGuard.Done()
}
// Set ourselves as running.
me.running = true
var fetcherCount uint64
// Create a fetcher routine for each server we want to monitor.
for _, server := range me.Configuration.Servers {
fetcherCount++
go processListFetcher(server)
}
log.Printf("Launched %d fetcher(s) to poll databases...", fetcherCount)
return nil
}
func (me *Fetcher) Stop() error {
// Alert all goroutines that we're done running.
me.running = false
// Wait for all fetchers to stop.
me.fetchGuard.Wait()
return nil
}