forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailroom.go
224 lines (187 loc) · 5.76 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
package mailroom
import (
"context"
"fmt"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/librato"
"github.com/nyaruka/mailroom/config"
"github.com/nyaruka/mailroom/queue"
"github.com/nyaruka/mailroom/s3utils"
"github.com/nyaruka/mailroom/web"
"github.com/sirupsen/logrus"
"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"
)
// InitFunction is a function that will be called when mailroom starts
type InitFunction func(mr *Mailroom) 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, mr *Mailroom, 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 {
Config *config.Config
DB *sqlx.DB
RP *redis.Pool
S3Client s3iface.S3API
Quit chan bool
CTX context.Context
Cancel context.CancelFunc
WaitGroup *sync.WaitGroup
batchForeman *Foreman
handlerForeman *Foreman
webserver *web.Server
}
// NewMailroom creates and returns a new mailroom instance
func NewMailroom(config *config.Config) *Mailroom {
mr := &Mailroom{
Config: config,
Quit: make(chan bool),
WaitGroup: &sync.WaitGroup{},
}
mr.CTX, mr.Cancel = context.WithCancel(context.Background())
mr.batchForeman = NewForeman(mr, queue.BatchQueue, config.BatchWorkers)
mr.handlerForeman = NewForeman(mr, queue.HandlerQueue, config.HandlerWorkers)
return mr
}
// Start starts the mailroom service
func (mr *Mailroom) Start() error {
log := logrus.WithFields(logrus.Fields{
"state": "starting",
})
// parse and test our db config
dbURL, err := url.Parse(mr.Config.DB)
if err != nil {
return fmt.Errorf("unable to parse DB URL '%s': %s", mr.Config.DB, err)
}
if dbURL.Scheme != "postgres" {
return fmt.Errorf("invalid DB URL: '%s', only postgres is supported", mr.Config.DB)
}
// build our db
db, err := sqlx.Open("postgres", mr.Config.DB)
if err != nil {
return fmt.Errorf("unable to open DB with config: '%s': %s", mr.Config.DB, err)
}
// configure our pool
mr.DB = db
mr.DB.SetMaxIdleConns(8)
mr.DB.SetMaxOpenConns(mr.Config.DBPoolSize)
mr.DB.SetConnMaxLifetime(time.Minute * 30)
// try connecting
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err = mr.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(mr.Config.Redis)
if err != nil {
return fmt.Errorf("unable to parse Redis URL '%s': %s", mr.Config.Redis, err)
}
// create our pool
redisPool := &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", 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
},
}
mr.RP = 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")
}
// create our s3 client
s3Session, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(mr.Config.AWSAccessKeyID, mr.Config.AWSSecretAccessKey, ""),
Endpoint: aws.String(mr.Config.S3Endpoint),
Region: aws.String(mr.Config.S3Region),
DisableSSL: aws.Bool(mr.Config.S3DisableSSL),
S3ForcePathStyle: aws.Bool(mr.Config.S3ForcePathStyle),
})
if err != nil {
return err
}
mr.S3Client = s3.New(s3Session)
// test out our S3 credentials
err = s3utils.TestS3(mr.S3Client, mr.Config.S3MediaBucket)
if err != nil {
log.WithError(err).Error("s3 bucket not reachable")
} else {
log.Info("s3 bucket ok")
}
for _, initFunc := range initFunctions {
initFunc(mr)
}
// if we have a librato token, configure it
if mr.Config.LibratoToken != "" {
host, _ := os.Hostname()
librato.Configure(mr.Config.LibratoUsername, mr.Config.LibratoToken, host, time.Second, mr.WaitGroup)
librato.Start()
}
// init our foremen and start it
mr.batchForeman.Start()
mr.handlerForeman.Start()
// start our web server
mr.webserver = web.NewServer(mr.CTX, mr.Config, mr.DB, mr.RP, mr.S3Client, mr.WaitGroup)
mr.webserver.Start()
logrus.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()
librato.Stop()
close(mr.Quit)
mr.Cancel()
// stop our web server
mr.webserver.Stop()
mr.WaitGroup.Wait()
logrus.Info("mailroom stopped")
return nil
}