forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.go
98 lines (83 loc) · 3.19 KB
/
cron.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
package analytics
import (
"context"
"sync"
"time"
"github.com/nyaruka/librato"
"github.com/nyaruka/mailroom"
"github.com/nyaruka/mailroom/core/queue"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/utils/cron"
"github.com/sirupsen/logrus"
)
func init() {
mailroom.AddInitFunction(StartAnalyticsCron)
}
// StartAnalyticsCron starts our cron job of posting stats every minute
func StartAnalyticsCron(rt *runtime.Runtime, wg *sync.WaitGroup, quit chan bool) error {
cron.Start(quit, rt, "stats", time.Second*60, true,
func() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
return reportAnalytics(ctx, rt)
},
)
return nil
}
var (
// both sqlx and redis provide wait stats which are cummulative that we need to make into increments
dbWaitDuration time.Duration
dbWaitCount int64
redisWaitDuration time.Duration
redisWaitCount int64
)
// calculates a bunch of stats every minute and both logs them and sends them to librato
func reportAnalytics(ctx context.Context, rt *runtime.Runtime) error {
// We wait 15 seconds since we fire at the top of the minute, the same as expirations.
// That way any metrics related to the size of our queue are a bit more accurate (all expirations can
// usually be handled in 15 seconds). Something more complicated would take into account the age of
// the items in our queues.
time.Sleep(time.Second * 15)
// get our DB status
dbStats := rt.DB.Stats()
redisStats := rt.RP.Stats()
rc := rt.RP.Get()
defer rc.Close()
// calculate size of batch queue
batchSize, err := queue.Size(rc, queue.BatchQueue)
if err != nil {
logrus.WithError(err).Error("error calculating batch queue size")
}
// and size of handler queue
handlerSize, err := queue.Size(rc, queue.HandlerQueue)
if err != nil {
logrus.WithError(err).Error("error calculating handler queue size")
}
dbWaitDurationInPeriod := dbStats.WaitDuration - dbWaitDuration
dbWaitCountInPeriod := dbStats.WaitCount - dbWaitCount
redisWaitDurationInPeriod := redisStats.WaitDuration - redisWaitDuration
redisWaitCountInPeriod := redisStats.WaitCount - redisWaitCount
dbWaitDuration = dbStats.WaitDuration
dbWaitCount = dbStats.WaitCount
redisWaitDuration = redisStats.WaitDuration
redisWaitCount = redisStats.WaitCount
logrus.WithFields(logrus.Fields{
"db_busy": dbStats.InUse,
"db_idle": dbStats.Idle,
"db_wait_time": dbWaitDurationInPeriod,
"db_wait_count": dbWaitCountInPeriod,
"redis_wait_time": dbWaitDurationInPeriod,
"redis_wait_count": dbWaitCountInPeriod,
"handler_size": handlerSize,
"batch_size": batchSize,
}).Info("current analytics")
librato.Gauge("mr.db_busy", float64(dbStats.InUse))
librato.Gauge("mr.db_idle", float64(dbStats.Idle))
librato.Gauge("mr.db_wait_ms", float64(dbWaitDurationInPeriod/time.Millisecond))
librato.Gauge("mr.db_wait_count", float64(dbWaitCountInPeriod))
librato.Gauge("mr.redis_wait_ms", float64(redisWaitDurationInPeriod/time.Millisecond))
librato.Gauge("mr.redis_wait_count", float64(redisWaitCountInPeriod))
librato.Gauge("mr.handler_queue", float64(handlerSize))
librato.Gauge("mr.batch_queue", float64(batchSize))
return nil
}