From 34b1b7f249fb1a52d6b1492d8eced3c6de08c3d3 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 12 Oct 2022 16:09:27 +0200 Subject: [PATCH] Add channel removed failed reason --- core/models/msgs.go | 21 ++++++++++--------- core/models/msgs_test.go | 4 +++- core/tasks/interrupts/interrupt_channel.go | 2 +- .../interrupts/interrupt_channel_test.go | 7 +++++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/core/models/msgs.go b/core/models/msgs.go index c93d369ca..34acb7a46 100644 --- a/core/models/msgs.go +++ b/core/models/msgs.go @@ -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{ @@ -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 } diff --git a/core/models/msgs_test.go b/core/models/msgs_test.go index 4778bcb8d..fa02bd071 100644 --- a/core/models/msgs_test.go +++ b/core/models/msgs_test.go @@ -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}) } diff --git a/core/tasks/interrupts/interrupt_channel.go b/core/tasks/interrupts/interrupt_channel.go index 1577790fb..ddb8bb787 100644 --- a/core/tasks/interrupts/interrupt_channel.go +++ b/core/tasks/interrupts/interrupt_channel.go @@ -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") } diff --git a/core/tasks/interrupts/interrupt_channel_test.go b/core/tasks/interrupts/interrupt_channel_test.go index 4f452176e..628d9d069 100644 --- a/core/tasks/interrupts/interrupt_channel_test.go +++ b/core/tasks/interrupts/interrupt_channel_test.go @@ -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) @@ -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") @@ -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")