forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
14 changed files
with
340 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tasks | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/nyaruka/goflow/utils" | ||
"github.com/nyaruka/mailroom" | ||
"github.com/nyaruka/mailroom/queue" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var registeredTypes = map[string](func() Task){} | ||
|
||
// RegisterType registers a new type of task | ||
func RegisterType(name string, initFunc func() Task) { | ||
registeredTypes[name] = initFunc | ||
|
||
mailroom.AddTaskFunction(name, func(ctx context.Context, mr *mailroom.Mailroom, task *queue.Task) error { | ||
// decode our task body | ||
typedTask, err := ReadTask(task.Type, task.Task) | ||
if err != nil { | ||
return errors.Wrapf(err, "error reading task of type %s", task.Type) | ||
} | ||
|
||
return typedTask.Perform(ctx, mr) | ||
}) | ||
} | ||
|
||
// Task is the common interface for all task types | ||
type Task interface { | ||
// Perform performs the task | ||
Perform(ctx context.Context, mr *mailroom.Mailroom) error | ||
} | ||
|
||
//------------------------------------------------------------------------------------------ | ||
// JSON Encoding / Decoding | ||
//------------------------------------------------------------------------------------------ | ||
|
||
// ReadTask reads an action from the given JSON | ||
func ReadTask(typeName string, data json.RawMessage) (Task, error) { | ||
f := registeredTypes[typeName] | ||
if f == nil { | ||
return nil, errors.Errorf("unknown task type: '%s'", typeName) | ||
} | ||
|
||
task := f() | ||
return task, utils.UnmarshalAndValidate(data, task) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tasks_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/mailroom/models" | ||
"github.com/nyaruka/mailroom/tasks" | ||
"github.com/nyaruka/mailroom/tasks/groups" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadTask(t *testing.T) { | ||
task, err := tasks.ReadTask("populate_dynamic_group", []byte(`{ | ||
"org_id": 2, | ||
"group_id": 23, | ||
"query": "gender = F" | ||
}`)) | ||
require.NoError(t, err) | ||
|
||
typedTask := task.(*groups.PopulateTask) | ||
assert.Equal(t, models.OrgID(2), typedTask.OrgID) | ||
assert.Equal(t, models.GroupID(23), typedTask.GroupID) | ||
assert.Equal(t, "gender = F", typedTask.Query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package groups_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/nyaruka/gocommon/uuids" | ||
"github.com/nyaruka/mailroom" | ||
"github.com/nyaruka/mailroom/config" | ||
"github.com/nyaruka/mailroom/models" | ||
"github.com/nyaruka/mailroom/tasks/groups" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
|
||
"github.com/olivere/elastic" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPopulateTask(t *testing.T) { | ||
testsuite.Reset() | ||
ctx := testsuite.CTX() | ||
db := testsuite.DB() | ||
|
||
mes := testsuite.NewMockElasticServer() | ||
defer mes.Close() | ||
|
||
es, err := elastic.NewClient( | ||
elastic.SetURL(mes.URL()), | ||
elastic.SetHealthcheck(false), | ||
elastic.SetSniff(false), | ||
) | ||
require.NoError(t, err) | ||
|
||
mr := &mailroom.Mailroom{Config: config.Mailroom, DB: db, RP: testsuite.RP(), ElasticClient: es} | ||
|
||
mes.NextResponse = fmt.Sprintf(`{ | ||
"_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAbgc0WS1hqbHlfb01SM2lLTWJRMnVOSVZDdw==", | ||
"took": 2, | ||
"timed_out": false, | ||
"_shards": { | ||
"total": 1, | ||
"successful": 1, | ||
"skipped": 0, | ||
"failed": 0 | ||
}, | ||
"hits": { | ||
"total": 1, | ||
"max_score": null, | ||
"hits": [ | ||
{ | ||
"_index": "contacts", | ||
"_type": "_doc", | ||
"_id": "%d", | ||
"_score": null, | ||
"_routing": "1", | ||
"sort": [15124352] | ||
} | ||
] | ||
} | ||
}`, models.CathyID) | ||
|
||
var groupID models.GroupID | ||
err = db.Get(&groupID, | ||
`INSERT INTO contacts_contactgroup(uuid, org_id, group_type, name, query, status, is_active, created_by_id, created_on, modified_by_id, modified_on) | ||
VALUES($1, $2, 'U', $3, $4, 'R', TRUE, 1, NOW(), 1, NOW()) RETURNING id`, | ||
uuids.New(), models.Org1, "Women", "gender = F", | ||
) | ||
require.NoError(t, err) | ||
|
||
task := &groups.PopulateTask{ | ||
OrgID: models.Org1, | ||
GroupID: groupID, | ||
Query: "gender = F", | ||
} | ||
err = task.Perform(ctx, mr) | ||
require.NoError(t, err) | ||
|
||
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contactgroup_contacts WHERE contactgroup_id = $1`, []interface{}{groupID}, 1) | ||
} |
Oops, something went wrong.