forked from nyaruka/courier
/
backend.go
520 lines (432 loc) · 15.8 KB
/
backend.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package rapidpro
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/garyburd/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/chatbase"
"github.com/nyaruka/courier/config"
"github.com/nyaruka/courier/queue"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
"github.com/sirupsen/logrus"
)
// the name for our message queue
const msgQueueName = "msgs"
// the name of our set for tracking sends
const sentSetName = "msgs_sent_%s"
// constants used in org configs for chatbase
const chatbaseAPIKey = "CHATBASE_API_KEY"
const chatbaseVersion = "CHATBASE_VERSION"
const chatbaseMessageType = "msg"
// our timeout for backend operations
const backendTimeout = time.Second * 30
func init() {
courier.RegisterBackend("rapidpro", newBackend)
}
// GetChannel returns the channel for the passed in type and UUID
func (b *backend) GetChannel(ctx context.Context, ct courier.ChannelType, uuid courier.ChannelUUID) (courier.Channel, error) {
timeout, cancel := context.WithTimeout(ctx, backendTimeout)
defer cancel()
return getChannel(timeout, b.db, ct, uuid)
}
// GetContact returns the contact for the passed in channel and URN
func (b *backend) GetContact(ctx context.Context, c courier.Channel, urn urns.URN, auth string, name string) (courier.Contact, error) {
dbChannel := c.(*DBChannel)
return contactForURN(ctx, b, dbChannel.OrgID_, dbChannel, urn, auth, name)
}
// NewIncomingMsg creates a new message from the given params
func (b *backend) NewIncomingMsg(channel courier.Channel, urn urns.URN, text string) courier.Msg {
// remove any control characters
text = utils.CleanString(text)
// create our msg
msg := newMsg(MsgIncoming, channel, urn, text)
// set received on to now
msg.WithReceivedOn(time.Now().UTC())
// have we seen this msg in the past period?
prevUUID := checkMsgSeen(b, msg)
if prevUUID != courier.NilMsgUUID {
// if so, use its UUID and that we've been written
msg.UUID_ = prevUUID
msg.alreadyWritten = true
}
return msg
}
// NewOutgoingMsg creates a new outgoing message from the given params
func (b *backend) NewOutgoingMsg(channel courier.Channel, urn urns.URN, text string) courier.Msg {
return newMsg(MsgOutgoing, channel, urn, text)
}
// PopNextOutgoingMsg pops the next message that needs to be sent
func (b *backend) PopNextOutgoingMsg(ctx context.Context) (courier.Msg, error) {
// pop the next message off our queue
rc := b.redisPool.Get()
defer rc.Close()
token, msgJSON, err := queue.PopFromQueue(rc, msgQueueName)
for token == queue.Retry {
token, msgJSON, err = queue.PopFromQueue(rc, msgQueueName)
}
if msgJSON != "" {
dbMsg := &DBMsg{}
err = json.Unmarshal([]byte(msgJSON), dbMsg)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal message '%s': %s", msgJSON, err)
}
// populate the channel on our db msg
channel, err := b.GetChannel(ctx, courier.AnyChannelType, dbMsg.ChannelUUID_)
if err != nil {
return nil, err
}
dbMsg.channel = channel.(*DBChannel)
dbMsg.workerToken = token
return dbMsg, nil
}
return nil, nil
}
var luaSent = redis.NewScript(3,
`-- KEYS: [TodayKey, YesterdayKey, MsgId]
local found = redis.call("sismember", KEYS[1], KEYS[3])
if found == 1 then
return 1
end
return redis.call("sismember", KEYS[2], KEYS[3])
`)
// WasMsgSent returns whether the passed in message has already been sent
func (b *backend) WasMsgSent(ctx context.Context, msg courier.Msg) (bool, error) {
rc := b.redisPool.Get()
defer rc.Close()
todayKey := fmt.Sprintf(sentSetName, time.Now().UTC().Format("2006_01_02"))
yesterdayKey := fmt.Sprintf(sentSetName, time.Now().Add(time.Hour*-24).UTC().Format("2006_01_02"))
return redis.Bool(luaSent.Do(rc, todayKey, yesterdayKey, msg.ID().String()))
}
// MarkOutgoingMsgComplete marks the passed in message as having completed processing, freeing up a worker for that channel
func (b *backend) MarkOutgoingMsgComplete(ctx context.Context, msg courier.Msg, status courier.MsgStatus) {
rc := b.redisPool.Get()
defer rc.Close()
dbMsg := msg.(*DBMsg)
queue.MarkComplete(rc, msgQueueName, dbMsg.workerToken)
// mark as sent in redis as well if this was actually wired or sent
if status != nil && (status.Status() == courier.MsgSent || status.Status() == courier.MsgWired) {
dateKey := fmt.Sprintf(sentSetName, time.Now().UTC().Format("2006_01_02"))
rc.Do("sadd", dateKey, msg.ID().String())
}
// if this org has chatbase connected, notify chatbase
chatKey, _ := msg.Channel().OrgConfigForKey(chatbaseAPIKey, "").(string)
if chatKey != "" {
chatVersion, _ := msg.Channel().OrgConfigForKey(chatbaseVersion, "").(string)
err := chatbase.SendChatbaseMessage(chatKey, chatVersion, chatbaseMessageType, dbMsg.ContactID_.String(), msg.Channel().Name(), msg.Text(), time.Now().UTC())
if err != nil {
logrus.WithError(err).WithField("chatbase_api_key", chatKey).WithField("chatbase_version", chatVersion).WithField("msg_id", dbMsg.ID().String()).Error("unable to write chatbase message")
}
}
}
// StopMsgContact marks the contact for the passed in msg as stopped, that is they no longer want to receive messages
func (b *backend) StopMsgContact(ctx context.Context, m courier.Msg) {
rc := b.redisPool.Get()
defer rc.Close()
dbMsg := m.(*DBMsg)
queueStopContact(rc, dbMsg.OrgID_, dbMsg.ContactID_)
}
// WriteMsg writes the passed in message to our store
func (b *backend) WriteMsg(ctx context.Context, m courier.Msg) error {
timeout, cancel := context.WithTimeout(ctx, backendTimeout)
defer cancel()
return writeMsg(timeout, b, m)
}
// NewStatusUpdateForID creates a new Status object for the given message id
func (b *backend) NewMsgStatusForID(channel courier.Channel, id courier.MsgID, status courier.MsgStatusValue) courier.MsgStatus {
return newMsgStatus(channel, id, "", status)
}
// NewStatusUpdateForID creates a new Status object for the given message id
func (b *backend) NewMsgStatusForExternalID(channel courier.Channel, externalID string, status courier.MsgStatusValue) courier.MsgStatus {
return newMsgStatus(channel, courier.NilMsgID, externalID, status)
}
// WriteMsgStatus writes the passed in MsgStatus to our store
func (b *backend) WriteMsgStatus(ctx context.Context, status courier.MsgStatus) error {
timeout, cancel := context.WithTimeout(ctx, backendTimeout)
defer cancel()
err := writeMsgStatus(timeout, b, status)
if err != nil {
return err
}
// if we have an id and are marking an outgoing msg as errored, then clear our sent flag
if status.ID() != courier.NilMsgID && status.Status() == courier.MsgErrored {
rc := b.redisPool.Get()
defer rc.Close()
dateKey := fmt.Sprintf(sentSetName, time.Now().UTC().Format("2006_01_02"))
prevDateKey := fmt.Sprintf(sentSetName, time.Now().Add(time.Hour*-24).UTC().Format("2006_01_02"))
// we pipeline the removals because we don't care about the return value
rc.Send("srem", dateKey, status.ID().String())
rc.Send("srem", prevDateKey, status.ID().String())
_, err := rc.Do("")
if err != nil {
logrus.WithError(err).WithField("msg", status.ID().String()).Error("error clearing sent flags")
}
}
return nil
}
// NewChannelEvent creates a new channel event with the passed in parameters
func (b *backend) NewChannelEvent(channel courier.Channel, eventType courier.ChannelEventType, urn urns.URN) courier.ChannelEvent {
return newChannelEvent(channel, eventType, urn)
}
// WriteChannelEvent writes the passed in channel even returning any error
func (b *backend) WriteChannelEvent(ctx context.Context, event courier.ChannelEvent) error {
timeout, cancel := context.WithTimeout(ctx, backendTimeout)
defer cancel()
return writeChannelEvent(timeout, b, event)
}
// WriteChannelLogs persists the passed in logs to our database, for rapidpro we swallow all errors, logging isn't critical
func (b *backend) WriteChannelLogs(ctx context.Context, logs []*courier.ChannelLog) error {
timeout, cancel := context.WithTimeout(ctx, backendTimeout)
defer cancel()
for _, l := range logs {
err := writeChannelLog(timeout, b, l)
if err != nil {
logrus.WithError(err).Error("error writing channel log")
}
}
return nil
}
// Health returns the health of this backend as a string, returning "" if all is well
func (b *backend) Health() string {
// test redis
rc := b.redisPool.Get()
defer rc.Close()
_, redisErr := rc.Do("PING")
// test our db
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
dbErr := b.db.PingContext(ctx)
cancel()
health := bytes.Buffer{}
if redisErr != nil {
health.WriteString(fmt.Sprintf("\n% 16s: %v", "redis err", redisErr))
}
if dbErr != nil {
health.WriteString(fmt.Sprintf("\n% 16s: %v", "db err", dbErr))
}
return health.String()
}
// Status returns information on our queue sizes, number of workers etc..
func (b *backend) Status() string {
rc := b.redisPool.Get()
defer rc.Close()
status := bytes.Buffer{}
status.WriteString("------------------------------------------------------------------------------------\n")
status.WriteString(" Size | Bulk Size | Workers | TPS | Type | Channel \n")
status.WriteString("------------------------------------------------------------------------------------\n")
var queue string
var workers float64
// get all our queues
rc.Send("zrevrangebyscore", fmt.Sprintf("%s:active", msgQueueName), "+inf", "-inf", "withscores")
rc.Send("zrevrangebyscore", fmt.Sprintf("%s:throttled", msgQueueName), "+inf", "-inf", "withscores")
rc.Flush()
active, err := redis.Values(rc.Receive())
if err != nil {
return fmt.Sprintf("unable to read active queues: %v", err)
}
throttled, err := redis.Values(rc.Receive())
if err != nil {
return fmt.Sprintf("unable to read throttled queues: %v", err)
}
values := append(active, throttled...)
for len(values) > 0 {
values, err = redis.Scan(values, &queue, &workers)
if err != nil {
return fmt.Sprintf("error reading active queues: %v", err)
}
// our queue name is in the format msgs:uuid|tps, break it apart
queue = strings.TrimPrefix(queue, "msgs:")
parts := strings.Split(queue, "|")
if len(parts) != 2 {
return fmt.Sprintf("error parsing queue name '%s'", queue)
}
uuid := parts[0]
tps := parts[1]
// try to look up our channel
channelUUID, _ := courier.NewChannelUUID(uuid)
channel, err := getChannel(context.Background(), b.db, courier.AnyChannelType, channelUUID)
channelType := "!!"
if err == nil {
channelType = channel.ChannelType().String()
}
// get # of items in our normal queue
size, err := redis.Int64(rc.Do("zcard", fmt.Sprintf("%s:%s/1", msgQueueName, queue)))
if err != nil {
return fmt.Sprintf("error reading queue size: %v", err)
}
// get # of items in the bulk queue
bulkSize, err := redis.Int64(rc.Do("zcard", fmt.Sprintf("%s:%s/0", msgQueueName, queue)))
if err != nil {
return fmt.Sprintf("error reading bulk queue size: %v", err)
}
status.WriteString(fmt.Sprintf("% 9d % 9d % 7d % 3s % 4s %s\n", size, bulkSize, int(workers), tps, channelType, uuid))
}
return status.String()
}
// Start starts our RapidPro backend, this tests our various connections and starts our spool flushers
func (b *backend) Start() error {
// parse and test our redis config
log := logrus.WithFields(logrus.Fields{
"comp": "backend",
"state": "starting",
})
log.Info("starting backend")
// parse and test our db config
dbURL, err := url.Parse(b.config.DB)
if err != nil {
return fmt.Errorf("unable to parse DB URL '%s': %s", b.config.DB, err)
}
if dbURL.Scheme != "postgres" {
return fmt.Errorf("invalid DB URL: '%s', only postgres is supported", b.config.DB)
}
// build our db
db, err := sqlx.Open("postgres", b.config.DB)
if err != nil {
return fmt.Errorf("unable to open DB with config: '%s': %s", b.config.DB, err)
}
// configure our pool
b.db = db
b.db.SetMaxIdleConns(4)
b.db.SetMaxOpenConns(16)
// try connecting
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err = b.db.PingContext(ctx)
cancel()
if err != nil {
log.Error("db not reachable")
} else {
log.Info("db ok")
}
// parse and test our redis config
redisURL, err := url.Parse(b.config.Redis)
if err != nil {
return fmt.Errorf("unable to parse Redis URL '%s': %s", b.config.Redis, err)
}
// create our pool
redisPool := &redis.Pool{
Wait: true, // makes callers wait for a connection
MaxActive: 8, // only open this many concurrent connections at once
MaxIdle: 4, // only keep up to this many idle
IdleTimeout: 240 * time.Second, // how long to wait before reaping a connection
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", fmt.Sprintf("%s", redisURL.Host))
if err != nil {
return nil, err
}
// send auth if required
if redisURL.User != nil {
pass, authRequired := redisURL.User.Password()
if authRequired {
if _, err := conn.Do("AUTH", pass); err != nil {
conn.Close()
return nil, err
}
}
}
// switch to the right DB
_, err = conn.Do("SELECT", strings.TrimLeft(redisURL.Path, "/"))
return conn, err
},
}
b.redisPool = redisPool
// test our redis connection
conn := redisPool.Get()
defer conn.Close()
_, err = conn.Do("PING")
if err != nil {
log.WithError(err).Error("redis not reachable")
} else {
log.Info("redis ok")
}
// start our dethrottler if we are going to be doing some sending
if b.config.MaxWorkers > 0 {
queue.StartDethrottler(redisPool, b.stopChan, b.waitGroup, msgQueueName)
}
// create our s3 client
s3Session, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(b.config.AWSAccessKeyID, b.config.AWSSecretAccessKey, ""),
Region: aws.String(b.config.S3Region),
})
if err != nil {
return err
}
b.s3Client = s3.New(s3Session)
// test out our S3 credentials
err = utils.TestS3(b.s3Client, b.config.S3MediaBucket)
if err != nil {
log.WithError(err).Error("s3 bucket not reachable")
} else {
log.Info("s3 bucket ok")
}
// make sure our spool dirs are writable
err = courier.EnsureSpoolDirPresent(b.config.SpoolDir, "msgs")
if err == nil {
err = courier.EnsureSpoolDirPresent(b.config.SpoolDir, "statuses")
}
if err == nil {
err = courier.EnsureSpoolDirPresent(b.config.SpoolDir, "events")
}
if err != nil {
log.WithError(err).Error("spool directories not writable")
} else {
log.Info("spool directories ok")
}
// register and start our spool flushers
courier.RegisterFlusher(path.Join(b.config.SpoolDir, "msgs"), b.flushMsgFile)
courier.RegisterFlusher(path.Join(b.config.SpoolDir, "statuses"), b.flushStatusFile)
courier.RegisterFlusher(path.Join(b.config.SpoolDir, "events"), b.flushChannelEventFile)
logrus.WithFields(logrus.Fields{
"comp": "backend",
"state": "started",
}).Info("backend started")
return nil
}
// Stop stops our RapidPro backend, closing our db and redis connections
func (b *backend) Stop() error {
// close our stop channel
close(b.stopChan)
// wait for our threads to exit
b.waitGroup.Wait()
return nil
}
func (b *backend) Cleanup() error {
// close our db and redis pool
if b.db != nil {
b.db.Close()
}
return b.redisPool.Close()
}
// RedisPool returns the redisPool for this backend
func (b *backend) RedisPool() *redis.Pool {
return b.redisPool
}
// NewBackend creates a new RapidPro backend
func newBackend(config *config.Courier) courier.Backend {
return &backend{
config: config,
stopChan: make(chan bool),
waitGroup: &sync.WaitGroup{},
}
}
type backend struct {
config *config.Courier
db *sqlx.DB
redisPool *redis.Pool
s3Client s3iface.S3API
awsCreds *credentials.Credentials
popScript *redis.Script
stopChan chan bool
waitGroup *sync.WaitGroup
}