Skip to content

Commit

Permalink
Merge pull request #675 from nyaruka/channel-removed-failed-reason
Browse files Browse the repository at this point in the history
Add channel removed failed reason
  • Loading branch information
rowanseymour authored Oct 12, 2022
2 parents 738ca23 + 34b1b7f commit ae470a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
21 changes: 11 additions & 10 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ const (
type MsgFailedReason null.String

const (
NilMsgFailedReason = MsgFailedReason("")
MsgFailedSuspended = MsgFailedReason("S") // workspace suspended
MsgFailedContact = MsgFailedReason("C") // contact blocked, stopped or archived
MsgFailedLooping = MsgFailedReason("L")
MsgFailedErrorLimit = MsgFailedReason("E")
MsgFailedTooOld = MsgFailedReason("O")
MsgFailedNoDestination = MsgFailedReason("D")
NilMsgFailedReason = MsgFailedReason("")
MsgFailedSuspended = MsgFailedReason("S") // workspace suspended
MsgFailedContact = MsgFailedReason("C") // contact blocked, stopped or archived
MsgFailedLooping = MsgFailedReason("L")
MsgFailedErrorLimit = MsgFailedReason("E")
MsgFailedTooOld = MsgFailedReason("O")
MsgFailedNoDestination = MsgFailedReason("D")
MsgFailedChannelRemoved = MsgFailedReason("R")
)

var unsendableToFailedReason = map[flows.UnsendableReason]MsgFailedReason{
Expand Down Expand Up @@ -1285,12 +1286,12 @@ WITH rows AS (
WHERE org_id = $1 AND direction = 'O' AND channel_id = $2 AND status IN ('P', 'Q', 'E')
LIMIT 1000
)
UPDATE msgs_msg SET status = 'F', modified_on = NOW() WHERE id IN (SELECT id FROM rows)`
UPDATE msgs_msg SET status = 'F', failed_reason = $3, modified_on = NOW() WHERE id IN (SELECT id FROM rows)`

func FailChannelMessages(ctx context.Context, db Queryer, orgID OrgID, channelID ChannelID) error {
func FailChannelMessages(ctx context.Context, db Queryer, orgID OrgID, channelID ChannelID, failedReason MsgFailedReason) error {
for {
// and update the messages as FAILED
res, err := db.ExecContext(ctx, sqlFailChannelMessages, orgID, channelID)
res, err := db.ExecContext(ctx, sqlFailChannelMessages, orgID, channelID, failedReason)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,15 @@ func TestFailMessages(t *testing.T) {
now := dates.Now()

// fail the msgs
err := models.FailChannelMessages(ctx, db, testdata.Org1.ID, testdata.TwilioChannel.ID)
err := models.FailChannelMessages(ctx, db, testdata.Org1.ID, testdata.TwilioChannel.ID, models.MsgFailedChannelRemoved)
require.NoError(t, err)

//assert.Len(t, failedMsgs, 3)

assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' AND modified_on > $1`, now).Returns(4)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' AND failed_reason = 'R' AND modified_on > $1`, now).Returns(4)
assertdb.Query(t, db, `SELECT status FROM msgs_msg WHERE id = $1`, out3.ID()).Columns(map[string]interface{}{"status": "F"})
assertdb.Query(t, db, `SELECT failed_reason FROM msgs_msg WHERE id = $1`, out3.ID()).Columns(map[string]interface{}{"failed_reason": nil})

}

Expand Down
2 changes: 1 addition & 1 deletion core/tasks/interrupts/interrupt_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (t *InterruptChannelTask) Perform(ctx context.Context, rt *runtime.Runtime,
return errors.Wrapf(err, "error clearing courier queues")
}

err = models.FailChannelMessages(ctx, db, orgID, t.ChannelID)
err = models.FailChannelMessages(ctx, db, orgID, t.ChannelID, models.MsgFailedChannelRemoved)
if err != nil {
return errors.Wrapf(err, "error failing channel messages")
}
Expand Down
7 changes: 5 additions & 2 deletions core/tasks/interrupts/interrupt_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestInterruptChannel(t *testing.T) {
assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID2).Returns("W")
assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID3).Returns("W")

assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and failed_reason = 'R' and channel_id = $1`, testdata.VonageChannel.ID).Returns(0)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.VonageChannel.ID).Returns(1)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(0)

Expand All @@ -62,7 +63,8 @@ func TestInterruptChannel(t *testing.T) {
assert.NoError(t, err)

assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.VonageChannel.ID).Returns(1)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(1)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and failed_reason = 'R' and channel_id = $1`, testdata.VonageChannel.ID).Returns(0)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and failed_reason = 'R' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(1)

assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID1).Returns("W")
assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID2).Returns("W")
Expand Down Expand Up @@ -92,8 +94,9 @@ func TestInterruptChannel(t *testing.T) {
err = task.Perform(ctx, rt, testdata.Org1.ID)
assert.NoError(t, err)

assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and failed_reason = 'R' and channel_id = $1`, testdata.VonageChannel.ID).Returns(6)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.VonageChannel.ID).Returns(7)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(1)
assertdb.Query(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'F' and failed_reason = 'R' and channel_id = $1`, testdata.TwilioChannel.ID).Returns(1)

assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID1).Returns("W")
assertdb.Query(t, db, `SELECT status FROM flows_flowsession WHERE id = $1`, sessionID2).Returns("I")
Expand Down

0 comments on commit ae470a2

Please sign in to comment.