forked from nyaruka/rp-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
137 lines (111 loc) · 2.84 KB
/
daemon.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package indexer
import (
"database/sql"
"log/slog"
"sync"
"time"
"github.com/nyaruka/gocommon/analytics"
"github.com/nyaruka/rp-indexer/v8/indexers"
)
type Daemon struct {
cfg *Config
db *sql.DB
wg *sync.WaitGroup
quit chan bool
indexers []indexers.Indexer
poll time.Duration
prevStats map[indexers.Indexer]indexers.Stats
}
// NewDaemon creates a new daemon to run the given indexers
func NewDaemon(cfg *Config, db *sql.DB, ixs []indexers.Indexer, poll time.Duration) *Daemon {
return &Daemon{
cfg: cfg,
db: db,
wg: &sync.WaitGroup{},
quit: make(chan bool),
indexers: ixs,
poll: poll,
prevStats: make(map[indexers.Indexer]indexers.Stats, len(ixs)),
}
}
// Start starts this daemon
func (d *Daemon) Start() {
// if we have a librato token, configure it
if d.cfg.LibratoToken != "" {
analytics.RegisterBackend(analytics.NewLibrato(d.cfg.LibratoUsername, d.cfg.LibratoToken, d.cfg.InstanceName, time.Second, d.wg))
}
analytics.Start()
for _, i := range d.indexers {
d.startIndexer(i)
}
d.startStatsReporter(time.Minute)
}
func (d *Daemon) startIndexer(indexer indexers.Indexer) {
d.wg.Add(1) // add ourselves to the wait group
log := slog.With("indexer", indexer.Name())
go func() {
defer func() {
log.Info("indexer exiting")
d.wg.Done()
}()
for {
select {
case <-d.quit:
return
case <-time.After(d.poll):
_, err := indexer.Index(d.db, d.cfg.Rebuild, d.cfg.Cleanup)
if err != nil {
log.Error("error during indexing", "error", err)
}
}
}
}()
}
func (d *Daemon) startStatsReporter(interval time.Duration) {
d.wg.Add(1) // add ourselves to the wait group
go func() {
defer func() {
slog.Info("analytics exiting")
d.wg.Done()
}()
for {
select {
case <-d.quit:
return
case <-time.After(interval):
d.reportStats()
}
}
}()
}
func (d *Daemon) reportStats() {
metrics := make(map[string]float64, len(d.indexers)*2)
for _, ix := range d.indexers {
stats := ix.Stats()
prev := d.prevStats[ix]
indexedInPeriod := stats.Indexed - prev.Indexed
deletedInPeriod := stats.Deleted - prev.Deleted
elapsedInPeriod := stats.Elapsed - prev.Elapsed
rateInPeriod := float64(0)
if indexedInPeriod > 0 && elapsedInPeriod > 0 {
rateInPeriod = float64(indexedInPeriod) / (float64(elapsedInPeriod) / float64(time.Second))
}
metrics[ix.Name()+"_indexed"] = float64(indexedInPeriod)
metrics[ix.Name()+"_deleted"] = float64(deletedInPeriod)
metrics[ix.Name()+"_rate"] = rateInPeriod
d.prevStats[ix] = stats
}
log := slog.New(slog.Default().Handler())
for k, v := range metrics {
analytics.Gauge("indexer."+k, v)
log = log.With(k, v)
}
log.Info("stats reported")
}
// Stop stops this daemon
func (d *Daemon) Stop() {
slog.Info("daemon stopping")
analytics.Stop()
close(d.quit)
d.wg.Wait()
}