-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🔁 Add task to resend messages #420
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98c09fa
Add task to resend messages
rowanseymour 74e2f68
Add tests
rowanseymour dc39983
Import msgs task package in mailroom cmd
rowanseymour 910e8f8
Rather than clone messages for resending, reset their state
rowanseymour ecb4f52
Fix test
rowanseymour a74599e
Set queued_on to now() when resending messages
rowanseymour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package msgs | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/nyaruka/mailroom" | ||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/core/msgio" | ||
"github.com/nyaruka/mailroom/core/tasks" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// TypeResendMsgs is the type of the resend messages task | ||
const TypeResendMsgs = "resend_msgs" | ||
|
||
func init() { | ||
tasks.RegisterType(TypeResendMsgs, func() tasks.Task { return &ResendMsgsTask{} }) | ||
} | ||
|
||
// ResendMsgsTask is our task for resending messages | ||
type ResendMsgsTask struct { | ||
MsgIDs []models.MsgID `json:"msg_ids"` | ||
} | ||
|
||
// Timeout is the maximum amount of time the task can run for | ||
func (t *ResendMsgsTask) Timeout() time.Duration { | ||
return time.Minute * 5 | ||
} | ||
|
||
func (t *ResendMsgsTask) Perform(ctx context.Context, mr *mailroom.Mailroom, orgID models.OrgID) error { | ||
db := mr.DB | ||
rp := mr.RP | ||
|
||
oa, err := models.GetOrgAssets(ctx, mr.DB, orgID) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to load org") | ||
} | ||
|
||
msgs, err := models.LoadMessages(ctx, db, orgID, models.DirectionOut, t.MsgIDs) | ||
if err != nil { | ||
return errors.Wrap(err, "error loading messages to resend") | ||
} | ||
|
||
clones, err := models.CloneMessages(ctx, db, rp, oa, msgs) | ||
if err != nil { | ||
return errors.Wrap(err, "error cloning messages") | ||
} | ||
|
||
// update existing messages as RESENT | ||
err = models.UpdateMessageStatus(ctx, db, msgs, models.MsgStatusResent) | ||
if err != nil { | ||
return errors.Wrap(err, "error updating message status") | ||
} | ||
|
||
msgio.SendMessages(ctx, db, rp, nil, clones) | ||
return nil | ||
} |
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,52 @@ | ||
package msgs_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/goflow/utils" | ||
"github.com/nyaruka/mailroom" | ||
"github.com/nyaruka/mailroom/config" | ||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/core/tasks/msgs" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/nyaruka/mailroom/testsuite/testdata" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestResendMsgs(t *testing.T) { | ||
testsuite.Reset() | ||
ctx := testsuite.CTX() | ||
db := testsuite.DB() | ||
mr := &mailroom.Mailroom{Config: config.Mailroom, DB: db, RP: testsuite.RP(), ElasticClient: nil} | ||
|
||
msgOut1 := testdata.InsertOutgoingMsg(t, db, models.Org1, models.CathyID, models.CathyURN, models.CathyURNID, "out 1", []utils.Attachment{"image/jpeg:hi.jpg"}) | ||
msgOut2 := testdata.InsertOutgoingMsg(t, db, models.Org1, models.BobID, models.BobURN, models.BobURNID, "out 2", nil) | ||
msgOut3 := testdata.InsertOutgoingMsg(t, db, models.Org1, models.CathyID, models.CathyURN, models.CathyURNID, "out 3", nil) | ||
|
||
db.MustExec(`UPDATE msgs_msg SET metadata = '{"topic":"cool-stuff"}' WHERE id = $1`, msgOut1.ID()) | ||
|
||
// create our task | ||
task := &msgs.ResendMsgsTask{ | ||
MsgIDs: []models.MsgID{models.MsgID(msgOut1.ID()), models.MsgID(msgOut2.ID())}, | ||
} | ||
|
||
// execute it | ||
err := task.Perform(ctx, mr, models.Org1) | ||
require.NoError(t, err) | ||
|
||
// there should be 2 new pending outgoing messages in the database, with channel set | ||
testsuite.AssertQueryCount(t, db, | ||
`SELECT count(*) FROM msgs_msg WHERE direction = 'O' AND status = 'P' AND channel_id IS NOT NULL AND id > $1`, | ||
[]interface{}{msgOut3.ID()}, 2, | ||
) | ||
|
||
// cloning will have cloned message text, attachments and metadata | ||
testsuite.AssertQueryCount(t, db, | ||
`SELECT count(*) FROM msgs_msg WHERE text = 'out 1' AND attachments = '{"image/jpeg:hi.jpg"}' AND metadata = '{"topic":"cool-stuff"}' AND id != $1`, | ||
[]interface{}{msgOut1.ID()}, 1, | ||
) | ||
|
||
// the resent messages should have had their status updated | ||
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'R'`, nil, 2) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure it's worth having a separate function for each status