Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/alert/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,12 @@ func (e *Engine) settleEmail(ctx context.Context, row generated.AlertEmailDelive
return
}
if len(rows) == 0 {
// Every recipient was dropped, or the rule went with them.
e.setRuleEmailStatus(ctx, row.RuleID, "", "")
// Every recipient was dropped, or the rule went with them. Only this
// notification's own retry state is cleared; any other status belongs
// to a newer notification.
if err := e.store.q.ClearRetryingRuleEmailStatus(ctx, row.RuleID); err != nil {
slog.Error("alert: clear rule email status", "rule", row.RuleID, "err", err)
}
return
}
var failed, suppressed int
Expand Down
39 changes: 39 additions & 0 deletions internal/alert/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,45 @@ func TestRetryDropsARecipientNoLongerOnTheRule(t *testing.T) {
}
}

func TestDroppingEveryRecipientClearsOnlyItsOwnRetryStatus(t *testing.T) {
setup := func(t *testing.T) (*mailHarness, Rule) {
h := newMailHarness(t, 30)
rule := h.rule(t, "errors", "gone@example.com", "keep@example.com")
h.box.fail = func(auth.AlertMail) error { return errors.New("451 later") }
h.fire(rule)
h.drain()
h.box.fail = nil
if status := h.ruleStatus(t, rule.ID).EmailStatus; status != "retrying" {
t.Fatalf("rule status = %q", status)
}
rule.EmailRecipients = []string{"new@example.com"}
if _, err := h.store.ReplaceRule(rule); err != nil {
t.Fatal(err)
}
return h, rule
}

t.Run("retrying status is cleared", func(t *testing.T) {
h, rule := setup(t)
h.now = h.now.Add(2 * time.Minute)
h.drain()
if status := h.ruleStatus(t, rule.ID).EmailStatus; status != "" {
t.Fatalf("rule status = %q", status)
}
})

t.Run("a newer failure stays visible", func(t *testing.T) {
h, rule := setup(t)
h.engine.setRuleEmailStatus(context.Background(), rule.ID, "failed", "newer notification")
h.now = h.now.Add(2 * time.Minute)
h.drain()
stored := h.ruleStatus(t, rule.ID)
if stored.EmailStatus != "failed" || stored.EmailStatusDetail != "newer notification" {
t.Fatalf("rule = %+v", stored)
}
})
}

func TestEmailWorkerKeepsRunningAfterAPanic(t *testing.T) {
h := newMailHarness(t, 30)
h.engine.now = nil
Expand Down
2 changes: 1 addition & 1 deletion internal/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (h *UserHandler) ListUsers(c *echo.Context) error {
// CreateUser adds a new user (admin only).
func (h *UserHandler) CreateUser(c *echo.Context) error {
if h.ownership != accountOwnershipLocal {
return ownedByIdentityProvider("Accounts are created by the identity provider, not in Fanout.")
return ownedByIdentityProvider("Accounts cannot be created in Fanout when roles come from the identity provider.")
}
var req struct {
Email string `json:"email"`
Expand Down
9 changes: 9 additions & 0 deletions internal/db/generated/alert_email.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/db/queries/alert_email.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ DELETE FROM alert_email_deliveries WHERE status IN ('sent', 'failed', 'suppresse

-- name: UpdateRuleEmailStatus :exec
UPDATE alert_rules SET email_status = ?, email_status_detail = ?, email_status_at = ? WHERE id = ?;

-- name: ClearRetryingRuleEmailStatus :exec
UPDATE alert_rules SET email_status = '', email_status_detail = '', email_status_at = '' WHERE id = ? AND email_status = 'retrying';
Loading