handle pending and completed state for alert notifications

This commit is contained in:
Marcus Efraimsson
2018-09-28 14:22:03 +02:00
parent 2bf399d3c8
commit c1763508e0
5 changed files with 108 additions and 94 deletions
+16 -5
View File
@@ -255,20 +255,26 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToCompleteCommand) error {
return withDbSession(ctx, func(sess *DBSession) error {
version := cmd.State.Version
var current m.AlertNotificationState
sess.ID(cmd.State.Id).Get(&current)
cmd.State.State = m.AlertNotificationStateCompleted
cmd.State.Version++
sql := `UPDATE alert_notification_state SET
state = ?,
version = ?
WHERE
id = ?`
res, err := sess.Exec(sql, m.AlertNotificationStateCompleted, cmd.Id, cmd.Version+1)
_, err := sess.Exec(sql, cmd.State.State, cmd.State.Version, cmd.State.Id)
if err != nil {
return err
}
affected, _ := res.RowsAffected()
if affected == 0 {
if current.Version != version {
return m.ErrAlertNotificationStateVersionConflict
}
@@ -278,6 +284,10 @@ func SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *m.SetA
func SetAlertNotificationStateToPendingCommand(ctx context.Context, cmd *m.SetAlertNotificationStateToPendingCommand) error {
return withDbSession(ctx, func(sess *DBSession) error {
currentVersion := cmd.State.Version
cmd.State.State = m.AlertNotificationStatePending
cmd.State.Version++
sql := `UPDATE alert_notification_state SET
state = ?,
version = ?
@@ -285,7 +295,8 @@ func SetAlertNotificationStateToPendingCommand(ctx context.Context, cmd *m.SetAl
id = ? AND
version = ?`
res, err := sess.Exec(sql, m.AlertNotificationStatePending, cmd.State.Version+1, cmd.State.Id, cmd.State.Version)
res, err := sess.Exec(sql, cmd.State.State, cmd.State.Version, cmd.State.Id, currentVersion)
if err != nil {
return err
}
@@ -25,6 +25,7 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(query.Result.State, ShouldEqual, "unknown")
So(query.Result.Version, ShouldEqual, 0)
Convey("Get existing state should not create a new state", func() {
query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
@@ -33,50 +34,67 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
So(query2.Result, ShouldNotBeNil)
So(query2.Result.Id, ShouldEqual, query.Result.Id)
})
Convey("Update existing state to pending with correct version should update database", func() {
s := *query.Result
cmd := models.SetAlertNotificationStateToPendingCommand{
State: &s,
}
err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
So(err, ShouldBeNil)
So(cmd.State.Version, ShouldEqual, 1)
So(cmd.State.State, ShouldEqual, models.AlertNotificationStatePending)
query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
err = GetAlertNotificationState(context.Background(), query2)
So(err, ShouldBeNil)
So(query2.Result.Version, ShouldEqual, 1)
So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending)
Convey("Update existing state to completed should update database", func() {
s := *cmd.State
cmd := models.SetAlertNotificationStateToCompleteCommand{
State: &s,
}
err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
So(err, ShouldBeNil)
query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
err = GetAlertNotificationState(context.Background(), query3)
So(err, ShouldBeNil)
So(query3.Result.Version, ShouldEqual, 2)
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
})
Convey("Update existing state to completed should update database, but return version mismatch", func() {
cmd.State.Version = 1000
s := *cmd.State
cmd := models.SetAlertNotificationStateToCompleteCommand{
State: &s,
}
err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
err = GetAlertNotificationState(context.Background(), query3)
So(err, ShouldBeNil)
So(query3.Result.Version, ShouldEqual, 1001)
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
})
})
Convey("Update existing state to pending with incorrect version should return version mismatch error", func() {
s := *query.Result
s.Version = 1000
cmd := models.SetAlertNotificationStateToPendingCommand{
State: &s,
}
err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
})
})
})
//Convey("Can insert new state for alert notifier", func() {
// createCmd := &models.InsertAlertNotificationCommand{
// AlertId: alertId,
// NotifierId: notifierId,
// OrgId: orgId,
// SentAt: 1,
// State: models.AlertNotificationStateCompleted,
// }
//
// err := InsertAlertNotificationState(context.Background(), createCmd)
// So(err, ShouldBeNil)
//
// err = InsertAlertNotificationState(context.Background(), createCmd)
// So(err, ShouldEqual, models.ErrAlertNotificationStateAlreadyExist)
//
// Convey("should be able to update alert notifier state", func() {
// updateCmd := &models.SetAlertNotificationStateToPendingCommand{
// State: models.AlertNotificationState{
// Id: 1,
// SentAt: 1,
// Version: 0,
// }
// }
//
// err := SetAlertNotificationStateToPendingCommand(context.Background(), updateCmd)
// So(err, ShouldBeNil)
//
// Convey("should not be able to set pending on old version", func() {
// err = SetAlertNotificationStateToPendingCommand(context.Background(), updateCmd)
// So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
// })
//
// Convey("should be able to set state to completed", func() {
// cmd := &models.SetAlertNotificationStateToCompleteCommand{Id: 1}
// err = SetAlertNotificationStateToCompleteCommand(context.Background(), cmd)
// So(err, ShouldBeNil)
// })
// })
// })
//})
Convey("Alert notifications should be empty", func() {
cmd := &models.GetAlertNotificationsQuery{
OrgId: 2,