forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailroom.go
287 lines (237 loc) · 8 KB
/
mailroom.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package mailroom
import (
"context"
"database/sql"
"fmt"
"log/slog"
"sync"
"time"
"github.com/appleboy/go-fcm"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/elastic/go-elasticsearch/v8"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/gocommon/aws/cwatch"
"github.com/nyaruka/gocommon/aws/dynamo"
"github.com/nyaruka/gocommon/aws/s3x"
"github.com/nyaruka/mailroom/core/tasks"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/web"
"github.com/nyaruka/redisx"
)
// Mailroom is a service for handling RapidPro events
type Mailroom struct {
ctx context.Context
cancel context.CancelFunc
rt *runtime.Runtime
wg *sync.WaitGroup
quit chan bool
handlerForeman *Foreman
batchForeman *Foreman
throttledForeman *Foreman
webserver *web.Server
// both sqlx and redis provide wait stats which are cummulative that we need to convert into increments by
// tracking their previous values
dbWaitDuration time.Duration
redisWaitDuration time.Duration
}
// NewMailroom creates and returns a new mailroom instance
func NewMailroom(config *runtime.Config) *Mailroom {
mr := &Mailroom{
rt: &runtime.Runtime{Config: config, Stats: runtime.NewStatsCollector()},
quit: make(chan bool),
wg: &sync.WaitGroup{},
}
mr.ctx, mr.cancel = context.WithCancel(context.Background())
mr.handlerForeman = NewForeman(mr.rt, mr.wg, tasks.HandlerQueue, config.HandlerWorkers)
mr.batchForeman = NewForeman(mr.rt, mr.wg, tasks.BatchQueue, config.BatchWorkers)
mr.throttledForeman = NewForeman(mr.rt, mr.wg, tasks.ThrottledQueue, config.BatchWorkers)
return mr
}
// Start starts the mailroom service
func (mr *Mailroom) Start() error {
c := mr.rt.Config
log := slog.With("comp", "mailroom")
var err error
_, mr.rt.DB, err = openAndCheckDBConnection(c.DB, c.DBPoolSize)
if err != nil {
log.Error("db not reachable", "error", err)
} else {
log.Info("db ok")
}
if c.ReadonlyDB != "" {
mr.rt.ReadonlyDB, _, err = openAndCheckDBConnection(c.ReadonlyDB, c.DBPoolSize)
if err != nil {
log.Error("readonly db not reachable", "error", err)
} else {
log.Info("readonly db ok")
}
} else {
// if readonly DB not specified, just use default DB again
mr.rt.ReadonlyDB = mr.rt.DB.DB
log.Warn("no distinct readonly db configured")
}
mr.rt.RP, err = redisx.NewPool(c.Redis)
if err != nil {
log.Error("redis not reachable", "error", err)
} else {
log.Info("redis ok")
}
if c.AndroidCredentialsFile != "" {
mr.rt.FCM, err = fcm.NewClient(mr.ctx, fcm.WithCredentialsFile(c.AndroidCredentialsFile))
if err != nil {
log.Error("unable to create FCM client", "error", err)
}
} else {
log.Warn("fcm not configured, no android syncing")
}
// setup DynamoDB
mr.rt.Dynamo, err = dynamo.NewService(c.AWSAccessKeyID, c.AWSSecretAccessKey, c.AWSRegion, c.DynamoEndpoint, c.DynamoTablePrefix)
if err != nil {
return err
}
if err := mr.rt.Dynamo.Test(mr.ctx); err != nil {
log.Error("dynamodb not reachable", "error", err)
} else {
log.Info("dynamodb ok")
}
// setup S3 storage
mr.rt.S3, err = s3x.NewService(c.AWSAccessKeyID, c.AWSSecretAccessKey, c.AWSRegion, c.S3Endpoint, c.S3Minio)
if err != nil {
return err
}
// check buckets
if err := mr.rt.S3.Test(mr.ctx, c.S3AttachmentsBucket); err != nil {
log.Error("attachments bucket not accessible", "error", err)
} else {
log.Info("attachments bucket ok")
}
if err := mr.rt.S3.Test(mr.ctx, c.S3SessionsBucket); err != nil {
log.Error("sessions bucket not accessible", "error", err)
} else {
log.Info("sessions bucket ok")
}
// initialize our elastic client
mr.rt.ES, err = elasticsearch.NewTypedClient(elasticsearch.Config{Addresses: []string{c.Elastic}, Username: c.ElasticUsername, Password: c.ElasticPassword})
if err != nil {
log.Error("elastic search not available", "error", err)
} else {
log.Info("elastic ok")
}
// configure and start cloudwatch
mr.rt.CW, err = cwatch.NewService(c.AWSAccessKeyID, c.AWSSecretAccessKey, c.AWSRegion, c.CloudwatchNamespace, c.DeploymentID)
if err != nil {
log.Error("cloudwatch not available", "error", err)
} else {
log.Info("cloudwatch ok")
}
// init our foremen and start it
mr.handlerForeman.Start()
mr.batchForeman.Start()
mr.throttledForeman.Start()
// start our web server
mr.webserver = web.NewServer(mr.ctx, mr.rt, mr.wg)
mr.webserver.Start()
tasks.StartCrons(mr.rt, mr.wg, mr.quit)
mr.startMetricsReporter(time.Minute)
log.Info("mailroom started", "domain", c.Domain)
return nil
}
func (mr *Mailroom) startMetricsReporter(interval time.Duration) {
mr.wg.Add(1)
report := func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
count, err := mr.reportMetrics(ctx)
cancel()
if err != nil {
slog.Error("error reporting metrics", "error", err)
} else {
slog.Info("sent metrics to cloudwatch", "count", count)
}
}
go func() {
defer func() {
slog.Info("metrics reporter exiting")
mr.wg.Done()
}()
for {
select {
case <-mr.quit:
report()
return
case <-time.After(interval): // TODO align to half minute marks for queue sizes?
report()
}
}
}()
}
func (mr *Mailroom) reportMetrics(ctx context.Context) (int, error) {
metrics := mr.rt.Stats.Extract().ToMetrics()
handlerSize, batchSize, throttledSize := getQueueSizes(mr.rt)
// calculate DB and redis stats
dbStats := mr.rt.DB.Stats()
redisStats := mr.rt.RP.Stats()
dbWaitDurationInPeriod := dbStats.WaitDuration - mr.dbWaitDuration
redisWaitDurationInPeriod := redisStats.WaitDuration - mr.redisWaitDuration
mr.dbWaitDuration = dbStats.WaitDuration
mr.redisWaitDuration = redisStats.WaitDuration
hostDim := cwatch.Dimension("Host", mr.rt.Config.InstanceID)
metrics = append(metrics,
cwatch.Datum("DBConnectionsInUse", float64(dbStats.InUse), types.StandardUnitCount, hostDim),
cwatch.Datum("DBConnectionWaitDuration", float64(dbWaitDurationInPeriod)/float64(time.Second), types.StandardUnitSeconds, hostDim),
cwatch.Datum("RedisConnectionsInUse", float64(redisStats.ActiveCount), types.StandardUnitCount, hostDim),
cwatch.Datum("RedisConnectionsWaitDuration", float64(redisWaitDurationInPeriod)/float64(time.Second), types.StandardUnitSeconds, hostDim),
cwatch.Datum("QueuedTasks", float64(handlerSize), types.StandardUnitCount, cwatch.Dimension("QueueName", "handler")),
cwatch.Datum("QueuedTasks", float64(batchSize), types.StandardUnitCount, cwatch.Dimension("QueueName", "batch")),
cwatch.Datum("QueuedTasks", float64(throttledSize), types.StandardUnitCount, cwatch.Dimension("QueueName", "throttled")),
)
if err := mr.rt.CW.Send(ctx, metrics...); err != nil {
return 0, fmt.Errorf("error sending metrics: %w", err)
}
return len(metrics), nil
}
// Stop stops the mailroom service
func (mr *Mailroom) Stop() error {
log := slog.With("comp", "mailroom")
log.Info("mailroom stopping")
mr.handlerForeman.Stop()
mr.batchForeman.Stop()
mr.throttledForeman.Stop()
close(mr.quit) // tell workers and crons to stop
mr.cancel()
mr.webserver.Stop()
mr.wg.Wait()
log.Info("mailroom stopped")
return nil
}
func openAndCheckDBConnection(url string, maxOpenConns int) (*sql.DB, *sqlx.DB, error) {
db, err := sqlx.Open("postgres", url)
if err != nil {
return nil, nil, fmt.Errorf("unable to open database connection: '%s': %w", url, err)
}
// configure our pool
db.SetMaxIdleConns(8)
db.SetMaxOpenConns(maxOpenConns)
db.SetConnMaxLifetime(time.Minute * 30)
// ping database...
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err = db.PingContext(ctx)
cancel()
return db.DB, db, err
}
func getQueueSizes(rt *runtime.Runtime) (int, int, int) {
rc := rt.RP.Get()
defer rc.Close()
handler, err := tasks.HandlerQueue.Size(rc)
if err != nil {
slog.Error("error calculating handler queue size", "error", err)
}
batch, err := tasks.BatchQueue.Size(rc)
if err != nil {
slog.Error("error calculating batch queue size", "error", err)
}
throttled, err := tasks.ThrottledQueue.Size(rc)
if err != nil {
slog.Error("error calculating throttled queue size", "error", err)
}
return handler, batch, throttled
}