Skip to content

Commit

Permalink
Revert maintenance mode config
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Apr 15, 2019
1 parent 22b7676 commit 4078074
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 77 deletions.
57 changes: 26 additions & 31 deletions backends/rapidpro/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,42 +455,37 @@ func (b *backend) Start() error {
})
log.Info("starting backend")

isMaintenance := b.config.Maintenance

if isMaintenance {
log.Info("starting in maintenance mode, writing to spool only, no DB")
} else {
// 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)
}
// 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)
}
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)
}
// 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)
// 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.WithError(err).Error("db not reachable")
} else {
log.Info("db ok")
}
// try connecting
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err = b.db.PingContext(ctx)
cancel()
if err != nil {
log.WithError(err).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 {
Expand Down
20 changes: 0 additions & 20 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,23 +1030,3 @@ func TestBackendSuite(t *testing.T) {
type ServerTestSuite struct {
suite.Suite
}

func (ts *MaintenanceServerTestSuite) TestMaintenanceMode() {
config := &courier.Config{Redis: "redis://localhost:6379/0", Maintenance: true}
config.Backend = "rapidpro"
b := newBackend(config)
ts.b = b.(*backend)
err := b.Start()
ts.NoError(err)
ts.Nil(ts.b.db)
ts.NotNil(ts.b.redisPool)
}

func TestMaintenanceServerSuite(t *testing.T) {
suite.Run(t, new(MaintenanceServerTestSuite))
}

type MaintenanceServerTestSuite struct {
suite.Suite
b *backend
}
7 changes: 2 additions & 5 deletions backends/rapidpro/channel_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,15 @@ func newChannelEvent(channel courier.Channel, eventType courier.ChannelEventType
// writeChannelEvent writes the passed in event to the database, queueing it to our spool in case the database is down
func writeChannelEvent(ctx context.Context, b *backend, event courier.ChannelEvent) error {
dbEvent := event.(*DBChannelEvent)
var err error

if !b.config.Maintenance {
err = writeChannelEventToDB(ctx, b, dbEvent)
}
err := writeChannelEventToDB(ctx, b, dbEvent)

// failed writing, write to our spool instead
if err != nil {
logrus.WithError(err).WithField("channel_id", dbEvent.ChannelID_.Int64).WithField("event_type", dbEvent.EventType_).Error("error writing channel event to db")
}

if err != nil || b.config.Maintenance {
if err != nil {
err = courier.WriteToSpool(b.config.SpoolDir, "events", dbEvent)
}

Expand Down
9 changes: 3 additions & 6 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const (
// WriteMsg creates a message given the passed in arguments
func writeMsg(ctx context.Context, b *backend, msg courier.Msg) error {
m := msg.(*DBMsg)
var err error

// this msg has already been written (we received it twice), we are a no op
if m.alreadyWritten {
Expand All @@ -73,17 +72,15 @@ func writeMsg(ctx context.Context, b *backend, msg courier.Msg) error {
}

// try to write it our db
if !b.config.Maintenance {
err = writeMsgToDB(ctx, b, m)
}
err := writeMsgToDB(ctx, b, m)

// fail? log
if err != nil {
logrus.WithError(err).WithField("msg", m.UUID().String()).Error("error writing to db")
}

// if we failed or in maintenance mode write to spool
if err != nil || b.config.Maintenance {
// if we failed write to spool
if err != nil {
err = courier.WriteToSpool(b.config.SpoolDir, "msgs", m)
}
// mark this msg as having been seen
Expand Down
9 changes: 3 additions & 6 deletions backends/rapidpro/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ func newMsgStatus(channel courier.Channel, id courier.MsgID, externalID string,
// writeMsgStatus writes the passed in status to the database, queueing it to our spool in case the database is down
func writeMsgStatus(ctx context.Context, b *backend, status courier.MsgStatus) error {
dbStatus := status.(*DBMsgStatus)
var err error

if !b.config.Maintenance {
err = writeMsgStatusToDB(ctx, b, dbStatus)
}
err := writeMsgStatusToDB(ctx, b, dbStatus)

if err == courier.ErrMsgNotFound {
return err
}

// failed writing or in maintenance, write to our spool instead
if err != nil || b.config.Maintenance {
// failed writing, write to our spool instead
if err != nil {
err = courier.WriteToSpool(b.config.SpoolDir, "statuses", dbStatus)
}

Expand Down
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Config struct {
LogLevel string `help:"the logging level courier should use"`
IgnoreDeliveryReports bool `help:"whether we ignore delivered status reports (errors will still be handled)"`
Version string `help:"the version that will be used in request and response headers"`
Maintenance bool `help:"Whether we are in maintenance window (Write to spool only)"`

// IncludeChannels is the list of channels to enable, empty means include all
IncludeChannels []string
Expand Down
10 changes: 2 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ func (s *server) Start() error {
}

// start our spool flushers
if !s.config.Maintenance {
startSpoolFlushers(s)
}
startSpoolFlushers(s)

// wire up our main pages
s.router.NotFound(s.handle404)
Expand Down Expand Up @@ -160,11 +158,7 @@ func (s *server) Start() error {

// start our foreman for outgoing messages
s.foreman = NewForeman(s, s.config.MaxWorkers)

// Do not send in maintenance mode
if !s.config.Maintenance {
s.foreman.Start()
}
s.foreman.Start()

return nil
}
Expand Down

0 comments on commit 4078074

Please sign in to comment.