forked from Ilhasoft/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mailroom.go
286 lines (235 loc) · 7.64 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
package mailroom
import (
"context"
"net/url"
"strings"
"sync"
"time"
"github.com/nyaruka/gocommon/storage"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/routers"
"github.com/nyaruka/mailroom/core/queue"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/web"
"github.com/pkg/errors"
"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/librato"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
)
// InitFunction is a function that will be called when mailroom starts
type InitFunction func(runtime *runtime.Runtime, wg *sync.WaitGroup, quit chan bool) error
var initFunctions = make([]InitFunction, 0)
// AddInitFunction adds an init function that will be called on startup
func AddInitFunction(initFunc InitFunction) {
initFunctions = append(initFunctions, initFunc)
}
// TaskFunction is the function that will be called for a type of task
type TaskFunction func(ctx context.Context, rt *runtime.Runtime, task *queue.Task) error
var taskFunctions = make(map[string]TaskFunction)
// AddTaskFunction adds an task function that will be called for a type of task
func AddTaskFunction(taskType string, taskFunc TaskFunction) {
taskFunctions[taskType] = taskFunc
}
// 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
batchForeman *Foreman
handlerForeman *Foreman
flowBatchForeman *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.batchForeman = NewForeman(mr.rt, mr.wg, queue.BatchQueue, config.BatchWorkers)
mr.handlerForeman = NewForeman(mr.rt, mr.wg, queue.HandlerQueue, config.HandlerWorkers)
mr.flowBatchForeman = NewForeman(mr.rt, mr.wg, queue.FlowBatchQueue, config.FlowBatchWorkers)
// set authentication token for zeroshot requests in goflow
routers.SetZeroshotToken(mr.rt.Config.ZeroshotAPIToken)
// set base url for zeroshot requests
routers.SetZeroshotAPIURL(mr.rt.Config.ZeroshotAPIUrl)
// set whatsapp system user token to be used in goflow
flows.SetWhatsAppSystemUserToken(mr.rt.Config.WhatsappSystemUserToken)
return mr
}
// Start starts the mailroom service
func (mr *Mailroom) Start() error {
c := mr.rt.Config
log := logrus.WithFields(logrus.Fields{"state": "starting"})
var err error
mr.rt.DB, err = openAndCheckDBConnection(c.DB, c.DBPoolSize)
if err != nil {
log.WithError(err).Error("db not reachable")
} else {
log.Info("db ok")
}
if c.ReadonlyDB != "" {
mr.rt.ReadonlyDB, err = openAndCheckDBConnection(c.ReadonlyDB, c.DBPoolSize)
if err != nil {
log.WithError(err).Error("readonly db not reachable")
} else {
log.Info("readonly db ok")
}
} else {
// if readonly DB not specified, just use default DB again
mr.rt.ReadonlyDB = mr.rt.DB
log.Warn("no distinct readonly db configured")
}
mr.rt.RP, err = openAndCheckRedisPool(c.Redis)
if err != nil {
log.WithError(err).Error("redis not reachable")
} else {
log.Info("redis ok")
}
// create our storage (S3 or file system)
if mr.rt.Config.AWSAccessKeyID != "" {
s3Client, err := storage.NewS3Client(&storage.S3Options{
AWSAccessKeyID: c.AWSAccessKeyID,
AWSSecretAccessKey: c.AWSSecretAccessKey,
Endpoint: c.S3Endpoint,
Region: c.S3Region,
DisableSSL: c.S3DisableSSL,
ForcePathStyle: c.S3ForcePathStyle,
MaxRetries: 3,
})
if err != nil {
return err
}
mr.rt.MediaStorage = storage.NewS3(s3Client, mr.rt.Config.S3MediaBucket, c.S3Region, 32)
mr.rt.SessionStorage = storage.NewS3(s3Client, mr.rt.Config.S3SessionBucket, c.S3Region, 32)
} else {
mr.rt.MediaStorage = storage.NewFS("_storage")
mr.rt.SessionStorage = storage.NewFS("_storage")
}
// test our media storage
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
err = mr.rt.MediaStorage.Test(ctx)
cancel()
if err != nil {
log.WithError(err).Error(mr.rt.MediaStorage.Name() + " media storage not available")
} else {
log.Info(mr.rt.MediaStorage.Name() + " media storage ok")
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
err = mr.rt.SessionStorage.Test(ctx)
cancel()
if err != nil {
log.WithError(err).Warn(mr.rt.SessionStorage.Name() + " session storage not available")
} else {
log.Info(mr.rt.SessionStorage.Name() + " session storage ok")
}
// initialize our elastic client
mr.rt.ES, err = newElasticClient(c.Elastic)
if err != nil {
log.WithError(err).Error("elastic search not available")
} else {
log.Info("elastic ok")
}
// warn if we won't be doing FCM syncing
if c.FCMKey == "" {
logrus.Error("fcm not configured, no syncing of android channels")
}
for _, initFunc := range initFunctions {
initFunc(mr.rt, mr.wg, mr.quit)
}
// if we have a librato token, configure it
if c.LibratoToken != "" {
librato.Configure(c.LibratoUsername, c.LibratoToken, c.InstanceName, time.Second, mr.wg)
librato.Start()
}
// init our foremen and start it
mr.batchForeman.Start()
mr.handlerForeman.Start()
mr.flowBatchForeman.Start()
// start our web server
mr.webserver = web.NewServer(mr.ctx, mr.rt, mr.wg)
mr.webserver.Start()
logrus.WithField("domain", c.Domain).Info("mailroom started")
return nil
}
// Stop stops the mailroom service
func (mr *Mailroom) Stop() error {
logrus.Info("mailroom stopping")
mr.batchForeman.Stop()
mr.handlerForeman.Stop()
mr.flowBatchForeman.Stop()
librato.Stop()
close(mr.quit)
mr.cancel()
// stop our web server
mr.webserver.Stop()
mr.wg.Wait()
mr.rt.ES.Stop()
logrus.Info("mailroom stopped")
return nil
}
func openAndCheckDBConnection(url string, maxOpenConns int) (*sqlx.DB, error) {
db, err := sqlx.Open("postgres", url)
if err != nil {
return nil, errors.Wrapf(err, "unable to open database connection: '%s'", url)
}
// 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, err
}
func openAndCheckRedisPool(redisUrl string) (*redis.Pool, error) {
redisURL, _ := url.Parse(redisUrl)
rp := &redis.Pool{
Wait: true, // makes callers wait for a connection
MaxActive: 36, // 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", 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
},
}
// test the connection
conn := rp.Get()
defer conn.Close()
_, err := conn.Do("PING")
return rp, err
}
func newElasticClient(url string) (*elastic.Client, error) {
// enable retrying
backoff := elastic.NewSimpleBackoff(500, 1000, 2000)
backoff.Jitter(true)
retrier := elastic.NewBackoffRetrier(backoff)
return elastic.NewClient(
elastic.SetURL(url),
elastic.SetSniff(false),
elastic.SetRetrier(retrier),
)
}