forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailroom.go
197 lines (161 loc) · 4.92 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
package mailroom
import (
"context"
"database/sql"
"fmt"
"log/slog"
"sync"
"time"
"github.com/appleboy/go-fcm"
"github.com/elastic/go-elasticsearch/v8"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/gocommon/analytics"
"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
}
// NewMailroom creates and returns a new mailroom instance
func NewMailroom(config *runtime.Config) *Mailroom {
mr := &Mailroom{
rt: &runtime.Runtime{Config: config},
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")
}
// if we have a librato token, configure it
if c.LibratoToken != "" {
analytics.RegisterBackend(analytics.NewLibrato(c.LibratoUsername, c.LibratoToken, c.InstanceID, time.Second, mr.wg))
}
analytics.Start()
// 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)
log.Info("mailroom started", "domain", c.Domain)
return 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()
analytics.Stop()
close(mr.quit)
mr.cancel()
// stop our web server
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
}