Skip to content

Commit

Permalink
Merge pull request #553 from nyaruka/session_storage_config
Browse files Browse the repository at this point in the history
Move session storage mode to the `runtime.Config` instead of an org config value
  • Loading branch information
rowanseymour authored Jan 3, 2022
2 parents 812305e + 65e6885 commit 6b2b1a4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 19 deletions.
12 changes: 0 additions & 12 deletions core/models/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,13 @@ func airtimeServiceFactory(c *runtime.Config) engine.AirtimeServiceFactory {
// OrgID is our type for orgs ids
type OrgID int

// SessionStorageMode is our type for how we persist our sessions
type SessionStorageMode string

const (
// NilOrgID is the id 0 considered as nil org id
NilOrgID = OrgID(0)

configSMTPServer = "smtp_server"
configDTOneKey = "dtone_key"
configDTOneSecret = "dtone_secret"

configSessionStorageMode = "session_storage_mode"

DBSessions = SessionStorageMode("db")
S3Sessions = SessionStorageMode("s3")
)

// Org is mailroom's type for RapidPro orgs. It also implements the envs.Environment interface for GoFlow
Expand All @@ -94,10 +86,6 @@ func (o *Org) Suspended() bool { return o.o.Suspended }
// UsesTopups returns whether the org uses topups
func (o *Org) UsesTopups() bool { return o.o.UsesTopups }

func (o *Org) SessionStorageMode() SessionStorageMode {
return SessionStorageMode(o.ConfigValue(configSessionStorageMode, string(DBSessions)))
}

// DateFormat returns the date format for this org
func (o *Org) DateFormat() envs.DateFormat { return o.env.DateFormat() }

Expand Down
6 changes: 2 additions & 4 deletions core/models/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,7 @@ func (s *Session) WriteUpdatedSession(ctx context.Context, rt *runtime.Runtime,
updateSQL := updateSessionSQL

// if writing to S3, do so
sessionMode := org.Org().SessionStorageMode()
if sessionMode == S3Sessions {
if rt.Config.SessionStorage == "s3" {
err := WriteSessionOutputsToStorage(ctx, rt, []*Session{s})
if err != nil {
logrus.WithError(err).Error("error writing session to s3")
Expand Down Expand Up @@ -820,8 +819,7 @@ func WriteSessions(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, org *O
insertIncompleteSQL := insertIncompleteSessionSQL

// if writing our sessions to S3, do so
sessionMode := org.Org().SessionStorageMode()
if sessionMode == S3Sessions {
if rt.Config.SessionStorage == "s3" {
err := WriteSessionOutputsToStorage(ctx, rt, sessions)
if err != nil {
// for now, continue on for errors, we are still reading from the DB
Expand Down
4 changes: 2 additions & 2 deletions core/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ func TestBatchStart(t *testing.T) {
func TestResume(t *testing.T) {
ctx, rt, db, _ := testsuite.Get()

defer testsuite.Reset(testsuite.ResetAll)
defer testsuite.Reset(testsuite.ResetData | testsuite.ResetStorage)

// write sessions to s3 storage
db.MustExec(`UPDATE orgs_org set config = '{"session_storage_mode": "s3"}' WHERE id = 1`)
rt.Config.SessionStorage = "s3"

oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshOrg)
require.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import (

"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
"gopkg.in/go-playground/validator.v9"
)

func init() {
utils.RegisterValidatorAlias("session_storage", "eq=db|eq=s3", func(e validator.FieldError) string { return "is not a valid session storage mode" })
}

// Config is our top level configuration object
type Config struct {
DB string `validate:"url,startswith=postgres:" help:"URL for your Postgres database"`
Expand Down Expand Up @@ -42,6 +47,7 @@ type Config struct {
MaxStepsPerSprint int `help:"the maximum number of steps allowed per engine sprint"`
MaxResumesPerSession int `help:"the maximum number of resumes allowed per engine session"`
MaxValueLength int `help:"the maximum size in characters for contact field values and run result values"`
SessionStorage string `validate:"omitempty,session_storage" help:"where to store session output (s3|db)"`

S3Endpoint string `help:"the S3 endpoint we will write attachments to"`
S3Region string `help:"the S3 region we will write attachments to"`
Expand Down Expand Up @@ -97,6 +103,7 @@ func NewDefaultConfig() *Config {
MaxStepsPerSprint: 100,
MaxResumesPerSession: 250,
MaxValueLength: 640,
SessionStorage: "db",

S3Endpoint: "https://s3.amazonaws.com",
S3Region: "us-east-1",
Expand Down
3 changes: 2 additions & 1 deletion runtime/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func TestValidate(t *testing.T) {
c.ReadonlyDB = "??"
c.Redis = "??"
c.Elastic = "??"
assert.EqualError(t, c.Validate(), "field 'DB' is not a valid URL, field 'ReadonlyDB' is not a valid URL, field 'Redis' is not a valid URL, field 'Elastic' is not a valid URL")
c.SessionStorage = "??"
assert.EqualError(t, c.Validate(), "field 'DB' is not a valid URL, field 'ReadonlyDB' is not a valid URL, field 'Redis' is not a valid URL, field 'Elastic' is not a valid URL, field 'SessionStorage' is not a valid session storage mode")

c = runtime.NewDefaultConfig()
c.DB = "mysql://temba:temba@localhost/temba"
Expand Down

0 comments on commit 6b2b1a4

Please sign in to comment.