Files
Pulse/internal/notifications/queue.go
pulse-triage[bot] dc277ba72a Use queue-scoped IDs in notification diagnostics
Keep delivery log, dead-letter and rehearsal destination labels correlated without sharing internal receipt or configuration identities. Preserve retained queue linkage and recovery authority across restarts.

Change-source: pulse-maintainer
2026-11-25 12:05:49 +00:00

3072 lines
99 KiB
Go

package notifications
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
"github.com/rcourtman/pulse-go-rewrite/internal/operationaltrust"
"github.com/rcourtman/pulse-go-rewrite/internal/securityutil"
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/rs/zerolog/log"
_ "modernc.org/sqlite"
)
// ErrNotificationDeliverySkipped distinguishes a policy cancellation from a
// successful provider delivery. Queue processors must not report skips as nil.
var ErrNotificationDeliverySkipped = errors.New("notification delivery disabled")
// defaultQueueMaxAttempts is the default number of delivery attempts
// before a notification is moved to the dead-letter queue. With the
// progressive retry schedule (1s, 2s, 4s, 30s, 2m, 5m, 10m, 15m; 60s
// floor from attempt 3 for rate-limited destinations) eight attempts
// recover momentary blips within seconds while spanning ~33 minutes for
// brief ISP, DNS, SMTP, or webhook outages without dead-lettering alerts.
const defaultQueueMaxAttempts = 8
const (
notificationAuditAlertIdentifiersColumn = "alert_identifiers"
legacyNotificationAuditAlertIdentifiersColumn = "alert_ids"
notificationOperationalLinksColumn = "operational_links"
notificationFailureClassColumn = "failure_class"
notificationAuditDestinationColumn = "destination_id"
notificationQueueDirName = "notifications"
notificationQueueFileName = "notification_queue.db"
autoReplayDLQWindow = 24 * time.Hour
autoReplayDLQBatchLimit = 25
autoReplayDLQCooldown = 5 * time.Minute
)
// NotificationQueueStatus represents the status of a queued notification
type NotificationQueueStatus string
const (
QueueStatusPending NotificationQueueStatus = "pending"
QueueStatusSending NotificationQueueStatus = "sending"
QueueStatusSent NotificationQueueStatus = "sent"
QueueStatusFailed NotificationQueueStatus = "failed"
QueueStatusDLQ NotificationQueueStatus = "dlq"
QueueStatusCancelled NotificationQueueStatus = "cancelled"
)
// NotificationFailureClass is a closed, content-free delivery failure bucket.
// Only these coarse values may leave the installation in telemetry.
type NotificationFailureClass string
const (
NotificationFailureAuthentication NotificationFailureClass = "authentication"
NotificationFailureRateLimited NotificationFailureClass = "rate_limited"
NotificationFailureConnectivity NotificationFailureClass = "connectivity"
NotificationFailureTLS NotificationFailureClass = "tls"
NotificationFailureConfiguration NotificationFailureClass = "configuration"
NotificationFailureRejected NotificationFailureClass = "rejected"
NotificationFailureServerError NotificationFailureClass = "server_error"
NotificationFailureUnknown NotificationFailureClass = "unknown"
)
// NotificationFailureClassCounts contains only bounded aggregate counters.
type NotificationFailureClassCounts struct {
Authentication int
RateLimited int
Connectivity int
TLS int
Configuration int
Rejected int
ServerError int
Unknown int
}
func (counts NotificationFailureClassCounts) AsMap() map[string]int {
return map[string]int{
string(NotificationFailureAuthentication): counts.Authentication,
string(NotificationFailureRateLimited): counts.RateLimited,
string(NotificationFailureConnectivity): counts.Connectivity,
string(NotificationFailureTLS): counts.TLS,
string(NotificationFailureConfiguration): counts.Configuration,
string(NotificationFailureRejected): counts.Rejected,
string(NotificationFailureServerError): counts.ServerError,
string(NotificationFailureUnknown): counts.Unknown,
}
}
// ClassifyNotificationFailure maps local error text to a fixed diagnostic
// bucket. It never returns any portion of the error string.
func ClassifyNotificationFailure(errorMessage string) NotificationFailureClass {
message := strings.ToLower(strings.TrimSpace(errorMessage))
containsAny := func(values ...string) bool {
for _, value := range values {
if strings.Contains(message, value) {
return true
}
}
return false
}
switch {
case containsAny(
"unauthorized", "forbidden", "authentication", "auth failed",
"auth negotiation",
"invalid token", "invalid api key", "invalid credentials",
"status 401", "http 401", "status 403", "http 403", "smtp 535", " 535 ",
):
return NotificationFailureAuthentication
case containsAny("rate limit", "too many requests", "status 429", "http 429"):
return NotificationFailureRateLimited
case containsAny(
"internal server error", "bad gateway", "service unavailable",
"gateway timeout", "status 5", "http 5",
):
return NotificationFailureServerError
case containsAny(
"x509", "certificate", "tls", "ssl", "starttls",
"server gave http response to https client",
):
return NotificationFailureTLS
case containsAny(
"timeout", "timed out", "deadline exceeded", "connection refused",
"connection reset", "network unreachable", "no such host", "dial tcp",
"broken pipe", "unexpected eof",
"http: server closed idle connection", "http2: client connection lost",
"http2: server sent goaway", "use of closed network connection",
"malformed http response",
):
return NotificationFailureConnectivity
case containsAny(
"not configured", "missing ", "invalid address", "invalid url",
"invalid webhook", "invalid configuration",
"must start with http", "validation failed", "executable file not found",
"no supported", "requires payload template",
"unsupported protocol scheme", "blocked private ip",
"blocked unspecified ip", "resolves to blocked private ips",
"private networks are not allowed",
):
return NotificationFailureConfiguration
case containsAny(
"bad request", "not found", "method not allowed", "gone",
"payload too large", "unsupported media type", "unprocessable",
"status 400", "http 400", "status 404", "http 404",
"status 405", "http 405", "status 410", "http 410",
"status 413", "http 413", "status 415", "http 415",
"status 422", "http 422", "status 4", "http 4",
):
return NotificationFailureRejected
default:
return NotificationFailureUnknown
}
}
// QueuedNotification represents a notification in the persistent queue
type QueuedNotification struct {
ID string `json:"id"`
Type string `json:"type"` // email, webhook, apprise
Method string `json:"method,omitempty"`
DestinationID string `json:"destinationId,omitempty"`
Status NotificationQueueStatus `json:"status"`
Alerts []*alerts.Alert `json:"alerts"`
Links []operationaltrust.NotificationLink `json:"links,omitempty"`
Config json.RawMessage `json:"config"` // EmailConfig, WebhookConfig, or AppriseConfig
ProviderAcceptedAt *time.Time `json:"-"`
ReceiptEpoch *string `json:"-"` // Queue metadata, never part of the admitted provider configuration.
Attempts int `json:"attempts"`
MaxAttempts int `json:"maxAttempts"`
LastAttempt *time.Time `json:"lastAttempt,omitempty"`
LastError *string `json:"lastError,omitempty"`
CreatedAt time.Time `json:"createdAt"`
NextRetryAt *time.Time `json:"nextRetryAt,omitempty"`
CompletedAt *time.Time `json:"completedAt,omitempty"`
PayloadBytes *int `json:"payloadBytes,omitempty"`
}
// NotificationQueue manages persistent notification delivery with retries and DLQ
type NotificationQueue struct {
mu sync.RWMutex
rehearsalAdmissionMu sync.Mutex // Serializes producer creation/finish without holding the queue policy lock.
deliveryGateMu sync.Mutex
deliveryGates map[string]*notificationDeliveryGate
stopOnce sync.Once
stopProcessingOnce sync.Once
stopErr error
db *sql.DB
dbPath string
destinationAliasKey []byte // Private, initialized before queue workers start; never used to authorize delivery.
stopChan chan struct{}
wg sync.WaitGroup
processorTicker *time.Ticker
cleanupTicker *time.Ticker
notifyChan chan struct{} // Signal when new notifications are added
processor func(*QueuedNotification) error // Notification processor function
quietHoursPolicy func() func(*alerts.Alert, time.Time) *time.Time
now func() time.Time // Queue scheduling clock; nil uses wall time.
occurrenceLifecycle func(string, time.Time) alerts.NotificationOccurrenceLifecycle
deliveryHealthChanged func() // Reconcile the monitoring-owned delivery alert after health-changing transitions
workerSem chan struct{} // Semaphore for limiting concurrent workers
processingNotifications map[string]struct{} // Owned row attempts; guarded by mu.
rehearsalErrors map[string]struct{} // Evidence failures fence this producer until restart; guarded by mu.
rehearsalOwner string // This queue incarnation owns new rehearsal admissions.
lastAutoReplay map[string]time.Time // Per-destination recovery replay cooldown; guarded by mu.
}
// notificationDeliveryGate orders delivery and cancellation for a single
// alert identifier. References are counted so high-cardinality alert IDs do
// not accumulate in the queue for the lifetime of the process.
type notificationDeliveryGate struct {
mu sync.RWMutex
refs int
}
func queueSQLiteDSN(path string) string {
separator := "?"
if strings.Contains(path, "?") {
separator = "&"
}
return path + separator + url.Values{
"_pragma": []string{
"busy_timeout(30000)",
"journal_mode(WAL)",
"synchronous(NORMAL)",
"foreign_keys(ON)",
pdb.PageCachePragma,
},
}.Encode()
}
func resolveNotificationQueuePath(dataDir string) (string, string, error) {
normalizedDir := strings.TrimSpace(dataDir)
if normalizedDir == "" {
defaultDir, err := securityutil.JoinStorageLeaf(utils.GetDataDir(), notificationQueueDirName)
if err != nil {
return "", "", fmt.Errorf("resolve default notification queue dir: %w", err)
}
normalizedDir = defaultDir
}
normalizedDir, err := securityutil.NormalizeStorageDir(normalizedDir)
if err != nil {
return "", "", fmt.Errorf("normalize notification queue dir: %w", err)
}
dbPath, err := securityutil.JoinStorageLeaf(normalizedDir, notificationQueueFileName)
if err != nil {
return "", "", fmt.Errorf("resolve notification queue db path: %w", err)
}
return normalizedDir, dbPath, nil
}
func newNotificationQueueFromDSN(dbPath, dsn string) (*NotificationQueue, error) {
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, fmt.Errorf("failed to open notification queue database: %w", err)
}
// SQLite works best with a single writer connection
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
db.SetConnMaxLifetime(0)
nq := &NotificationQueue{
db: db,
dbPath: dbPath,
deliveryGates: make(map[string]*notificationDeliveryGate),
stopChan: make(chan struct{}),
processorTicker: time.NewTicker(5 * time.Second),
cleanupTicker: time.NewTicker(1 * time.Hour),
notifyChan: make(chan struct{}, 100),
workerSem: make(chan struct{}, 5), // Allow 5 concurrent workers
}
if err := nq.initSchema(); err != nil {
db.Close()
return nil, fmt.Errorf("failed to initialize schema: %w", err)
}
if err := nq.initDestinationAliasKey(); err != nil {
log.Warn().Msg("Notification diagnostic IDs will change after restart: durable alias key unavailable")
}
// Reset any stuck "sending" items to "pending" (crash recovery)
if _, err := nq.db.Exec(`UPDATE notification_queue SET status = 'pending' WHERE status = 'sending'`); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "recover_stuck_sending").
Str("dbPath", dbPath).
Msg("Failed to recover stuck sending notifications")
}
// Start background processors
nq.wg.Add(2)
go nq.processQueue()
go nq.cleanupOldEntries()
log.Info().
Str("dbPath", dbPath).
Msg("notification queue initialized")
return nq, nil
}
// NewNotificationQueue creates a new persistent notification queue.
func NewNotificationQueue(dataDir string) (*NotificationQueue, error) {
resolvedDataDir, dbPath, err := resolveNotificationQueuePath(dataDir)
if err != nil {
return nil, err
}
// Queue data includes alert payload/context and destination configuration;
// keep it owner-only by default.
if err := os.MkdirAll(resolvedDataDir, 0700); err != nil {
return nil, fmt.Errorf("failed to create notification queue directory: %w", err)
}
return newNotificationQueueFromDSN(dbPath, queueSQLiteDSN(dbPath))
}
// NewInMemoryNotificationQueue creates a non-persistent in-memory queue with the
// same runtime behavior as the persistent queue. It exists as a fallback owner
// when on-disk queue bootstrap fails, so notification delivery still routes
// through the canonical queue processor.
func NewInMemoryNotificationQueue() (*NotificationQueue, error) {
return newNotificationQueueFromDSN(":memory:", queueSQLiteDSN("file::memory:?mode=memory"))
}
func normalizeAlertIdentifiers(alertIdentifiers []string) []string {
seen := make(map[string]struct{}, len(alertIdentifiers))
normalized := make([]string, 0, len(alertIdentifiers))
for _, identifier := range alertIdentifiers {
identifier = strings.TrimSpace(identifier)
if identifier == "" {
continue
}
if _, exists := seen[identifier]; exists {
continue
}
seen[identifier] = struct{}{}
normalized = append(normalized, identifier)
}
sort.Strings(normalized)
return normalized
}
func alertIdentifiersFromAlerts(alertList []*alerts.Alert) []string {
identifiers := make([]string, 0, len(alertList))
for _, alert := range alertList {
if alert != nil {
identifiers = append(identifiers, alert.ID)
}
}
return normalizeAlertIdentifiers(identifiers)
}
// acquireAlertDeliveryGates coordinates a firing delivery with resolution
// cancellation for only the alert IDs involved in that operation. Acquiring
// IDs in sorted order prevents grouped notifications from deadlocking when
// different alerts resolve concurrently.
func (nq *NotificationQueue) acquireAlertDeliveryGates(alertIdentifiers []string, write bool) func() {
identifiers := normalizeAlertIdentifiers(alertIdentifiers)
if len(identifiers) == 0 {
return func() {}
}
nq.deliveryGateMu.Lock()
gates := make([]*notificationDeliveryGate, 0, len(identifiers))
for _, identifier := range identifiers {
gate := nq.deliveryGates[identifier]
if gate == nil {
gate = &notificationDeliveryGate{}
nq.deliveryGates[identifier] = gate
}
gate.refs++
gates = append(gates, gate)
}
nq.deliveryGateMu.Unlock()
for _, gate := range gates {
if write {
gate.mu.Lock()
} else {
gate.mu.RLock()
}
}
return func() {
for i := len(gates) - 1; i >= 0; i-- {
if write {
gates[i].mu.Unlock()
} else {
gates[i].mu.RUnlock()
}
}
nq.deliveryGateMu.Lock()
for i, identifier := range identifiers {
gate := gates[i]
gate.refs--
if gate.refs == 0 && nq.deliveryGates[identifier] == gate {
delete(nq.deliveryGates, identifier)
}
}
nq.deliveryGateMu.Unlock()
}
}
// initSchema creates the database tables
func (nq *NotificationQueue) initSchema() error {
schema := `
CREATE TABLE IF NOT EXISTS notification_queue (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
method TEXT,
status TEXT NOT NULL,
alerts TEXT NOT NULL,
operational_links TEXT NOT NULL DEFAULT '[]',
config TEXT NOT NULL,
receipt_epoch TEXT,
attempts INTEGER NOT NULL DEFAULT 0,
max_attempts INTEGER NOT NULL DEFAULT 3,
last_attempt INTEGER,
last_error TEXT,
created_at INTEGER NOT NULL,
next_retry_at INTEGER,
completed_at INTEGER,
payload_bytes INTEGER
);
CREATE INDEX IF NOT EXISTS idx_status ON notification_queue(status);
CREATE INDEX IF NOT EXISTS idx_next_retry ON notification_queue(next_retry_at) WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS idx_created_at ON notification_queue(created_at);
CREATE INDEX IF NOT EXISTS idx_status_completed ON notification_queue(status, completed_at);
CREATE TABLE IF NOT EXISTS notification_audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
notification_id TEXT NOT NULL,
type TEXT NOT NULL,
method TEXT,
status TEXT NOT NULL,
alert_identifiers TEXT,
alert_count INTEGER,
operational_links TEXT NOT NULL DEFAULT '[]',
attempts INTEGER,
success BOOLEAN,
error_message TEXT,
failure_class TEXT NOT NULL DEFAULT '',
destination_id TEXT NOT NULL DEFAULT '',
payload_size INTEGER,
timestamp INTEGER NOT NULL,
FOREIGN KEY (notification_id) REFERENCES notification_queue(id)
);
CREATE INDEX IF NOT EXISTS idx_audit_timestamp ON notification_audit(timestamp);
CREATE INDEX IF NOT EXISTS idx_audit_notification_id ON notification_audit(notification_id);
CREATE INDEX IF NOT EXISTS idx_audit_status ON notification_audit(status);
CREATE TABLE IF NOT EXISTS notification_delivery_receipts (
alert_id TEXT NOT NULL,
alert_start_nanos INTEGER NOT NULL,
destination_key TEXT NOT NULL,
delivered_at INTEGER NOT NULL,
delivery_epoch TEXT NOT NULL DEFAULT '',
PRIMARY KEY (alert_id, alert_start_nanos, destination_key, delivery_epoch)
);
CREATE INDEX IF NOT EXISTS idx_delivery_receipts_delivered_at
ON notification_delivery_receipts(delivered_at);
`
if _, err := nq.db.Exec(schema); err != nil {
return err
}
if err := nq.migrateDeliveryReceipts(); err != nil {
return err
}
if err := nq.migrateQueueReceiptEpoch(); err != nil {
return err
}
if err := nq.migrateAlertIdentifierColumns(); err != nil {
return err
}
if err := nq.ensureJSONColumn(
"notification_queue",
notificationOperationalLinksColumn,
); err != nil {
return err
}
if err := nq.ensureJSONColumn(
"notification_audit",
notificationOperationalLinksColumn,
); err != nil {
return err
}
if err := nq.ensureTextColumn(
"notification_audit",
notificationFailureClassColumn,
); err != nil {
return err
}
if err := nq.ensureTextColumn(
"notification_audit",
notificationAuditDestinationColumn,
); err != nil {
return err
}
if err := nq.migrateDeliveryFacts(); err != nil {
return err
}
return nq.migrateRehearsals()
}
func (nq *NotificationQueue) recordDeliveryReceipt(alertID string, alertStart time.Time, destinationKey string, deliveredAt time.Time, severity int, epoch string, queueIDs ...string) error {
if nq == nil || strings.TrimSpace(alertID) == "" || strings.TrimSpace(destinationKey) == "" || alertStart.IsZero() {
return fmt.Errorf("alert occurrence and destination are required for a delivery receipt")
}
now := time.Now()
if deliveredAt.IsZero() || deliveredAt.After(now) {
deliveredAt = now
}
nq.mu.Lock()
defer nq.mu.Unlock()
queueID := ""
if len(queueIDs) > 0 {
queueID = queueIDs[0]
}
order, err := nq.receiptSuccessOrderLocked(queueID)
if err != nil {
return err
}
resolved, recovered, resolutionOrder, err := nq.queuedReceiptDispositionLocked(queueID, alertID, alertStart)
if err != nil {
return err
}
if recovered {
return nil
}
resolvedAt := int64(0)
if !resolved.IsZero() {
resolvedAt = resolved.UnixNano()
}
retired := int64(0)
{
var current string
err := nq.db.QueryRow(`SELECT epoch FROM notification_destination_state WHERE destination_key=?`, destinationKey).Scan(&current)
if err != nil && err != sql.ErrNoRows {
return err
}
if err == sql.ErrNoRows || current != epoch {
retired = time.Now().Unix()
}
}
_, err = nq.db.Exec(`
INSERT INTO notification_delivery_receipts
(alert_id, alert_start_nanos, destination_key, delivery_epoch, delivered_at, delivered_nanos, severity, retired_at,resolved_at,success_order,resolution_order)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(alert_id, alert_start_nanos, destination_key, delivery_epoch) DO UPDATE SET
delivered_at = MAX(delivered_at, excluded.delivered_at),
delivered_nanos = MAX(delivered_nanos, delivered_at*1000000000, excluded.delivered_nanos),
severity = MAX(severity, excluded.severity),
resolved_at=CASE WHEN excluded.success_order>=success_order THEN excluded.resolved_at ELSE resolved_at END,
resolution_order=CASE WHEN excluded.success_order>=success_order THEN excluded.resolution_order ELSE resolution_order END,
success_order=MAX(success_order,excluded.success_order),
retired_at = MAX(retired_at, excluded.retired_at),
lifecycle_revision = lifecycle_revision + 1
`, alertID, alertStart.UnixNano(), destinationKey, epoch, deliveredAt.Unix(), deliveredAt.UnixNano(), severity, retired, resolvedAt, order, resolutionOrder)
if err != nil {
return fmt.Errorf("record notification delivery receipt: %w", err)
}
return nil
}
func (nq *NotificationQueue) HasDeliveryReceipt(alertID string, alertStart time.Time, destinationKey string) (bool, error) {
if nq == nil || strings.TrimSpace(alertID) == "" || strings.TrimSpace(destinationKey) == "" || alertStart.IsZero() {
return false, nil
}
nq.mu.RLock()
defer nq.mu.RUnlock()
var marker int
err := nq.db.QueryRow(`
SELECT 1 FROM notification_delivery_receipts r JOIN notification_destination_state s ON s.destination_key=r.destination_key AND s.epoch=r.delivery_epoch
WHERE r.alert_id = ? AND r.alert_start_nanos = ? AND r.destination_key = ? AND r.retired_at = 0
`, alertID, alertStart.UnixNano(), destinationKey).Scan(&marker)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, fmt.Errorf("read notification delivery receipt: %w", err)
}
return true, nil
}
func (nq *NotificationQueue) migrateAlertIdentifierColumns() error {
columns, err := nq.tableColumns("notification_audit")
if err != nil {
return err
}
if columns[notificationAuditAlertIdentifiersColumn] || !columns[legacyNotificationAuditAlertIdentifiersColumn] {
return nil
}
if _, err := nq.db.Exec(
`ALTER TABLE notification_audit RENAME COLUMN ` +
legacyNotificationAuditAlertIdentifiersColumn +
` TO ` +
notificationAuditAlertIdentifiersColumn,
); err != nil {
return fmt.Errorf(
"rename notification_audit.%s to %s: %w",
legacyNotificationAuditAlertIdentifiersColumn,
notificationAuditAlertIdentifiersColumn,
err,
)
}
return nil
}
func (nq *NotificationQueue) tableColumns(table string) (map[string]bool, error) {
rows, err := nq.db.Query(`PRAGMA table_info(` + table + `)`)
if err != nil {
return nil, fmt.Errorf("inspect columns for %s: %w", table, err)
}
defer rows.Close()
columns := make(map[string]bool)
for rows.Next() {
var (
cid int
name string
columnType string
notNull int
defaultVal sql.NullString
pk int
)
if err := rows.Scan(&cid, &name, &columnType, &notNull, &defaultVal, &pk); err != nil {
return nil, fmt.Errorf("scan column for %s: %w", table, err)
}
columns[name] = true
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterate columns for %s: %w", table, err)
}
return columns, nil
}
func (nq *NotificationQueue) ensureJSONColumn(table, column string) error {
columns, err := nq.tableColumns(table)
if err != nil {
return err
}
if columns[column] {
return nil
}
if _, err := nq.db.Exec(
`ALTER TABLE ` + table + ` ADD COLUMN ` + column + ` TEXT NOT NULL DEFAULT '[]'`,
); err != nil {
return fmt.Errorf("add %s.%s: %w", table, column, err)
}
return nil
}
func (nq *NotificationQueue) ensureTextColumn(table, column string) error {
columns, err := nq.tableColumns(table)
if err != nil {
return err
}
if columns[column] {
return nil
}
if _, err := nq.db.Exec(
`ALTER TABLE ` + table + ` ADD COLUMN ` + column + ` TEXT NOT NULL DEFAULT ''`,
); err != nil {
return fmt.Errorf("add %s.%s: %w", table, column, err)
}
return nil
}
func notificationLinksForAlerts(
alertsToLink []*alerts.Alert,
destinationID string,
) []operationaltrust.NotificationLink {
destinationID = strings.TrimSpace(destinationID)
if destinationID == "" {
return nil
}
links := make([]operationaltrust.NotificationLink, 0, len(alertsToLink))
for _, alert := range alertsToLink {
if alert == nil ||
alert.OperationalRecord == nil ||
alert.LatestTransition == nil {
continue
}
links = append(links, operationaltrust.NotificationLink{
OperationalRecordID: alert.OperationalRecord.ID,
TransitionID: alert.LatestTransition.ID,
LifecycleState: alert.LatestTransition.To,
CauseKey: alert.LatestTransition.CauseKey,
DestinationID: destinationID,
DeliveryState: operationaltrust.NotificationQueued,
})
}
return links
}
func notificationTransitionIDs(
links []operationaltrust.NotificationLink,
) []string {
ids := make([]string, 0, len(links))
for _, link := range links {
if id := strings.TrimSpace(link.TransitionID); id != "" {
ids = append(ids, id)
}
}
return ids
}
func normalizeNotificationLinks(
notificationID string,
destinationID string,
links []operationaltrust.NotificationLink,
state operationaltrust.NotificationDeliveryState,
attemptedAt *time.Time,
completedAt *time.Time,
) ([]operationaltrust.NotificationLink, error) {
if len(links) == 0 {
return nil, nil
}
normalized := make([]operationaltrust.NotificationLink, 0, len(links))
seen := make(map[string]struct{}, len(links))
for _, link := range links {
link = link.Clone()
link.NotificationID = notificationID
if strings.TrimSpace(link.DestinationID) == "" {
link.DestinationID = strings.TrimSpace(destinationID)
}
link.DeliveryState = state
link.AttemptedAt = attemptedAt
link.CompletedAt = completedAt
if err := link.Validate(); err != nil {
return nil, err
}
key := link.OperationalRecordID + "\x00" +
link.TransitionID + "\x00" +
link.DestinationID
if _, exists := seen[key]; exists {
continue
}
seen[key] = struct{}{}
normalized = append(normalized, link)
}
return normalized, nil
}
// Enqueue adds a notification to the queue
func (nq *NotificationQueue) Enqueue(notif *QueuedNotification) error {
nq.mu.Lock()
defer nq.mu.Unlock()
return nq.enqueueLocked(notif)
}
func (nq *NotificationQueue) enqueueLocked(notif *QueuedNotification) error {
if notif == nil {
return fmt.Errorf("notification cannot be nil")
}
for _, alert := range notif.Alerts {
if _, failed := nq.rehearsalErrors[rehearsalID(alert)]; failed {
return fmt.Errorf("rehearsal evidence is unavailable")
}
}
notif.Type = strings.TrimSpace(notif.Type)
if notif.Type == "" {
return fmt.Errorf("notification type cannot be empty")
}
notif.Method = strings.TrimSpace(notif.Method)
if len(notif.Config) == 0 {
return fmt.Errorf("notification config cannot be empty")
}
if notif.CreatedAt.IsZero() {
notif.CreatedAt = time.Now()
}
if len(notif.Links) == 0 {
notif.Links = notificationLinksForAlerts(
notif.Alerts,
notif.DestinationID,
)
}
if strings.TrimSpace(notif.DestinationID) == "" && len(notif.Links) > 0 {
notif.DestinationID = strings.TrimSpace(notif.Links[0].DestinationID)
}
if notif.ID == "" {
transitionIDs := notificationTransitionIDs(notif.Links)
if len(transitionIDs) > 0 && strings.TrimSpace(notif.DestinationID) != "" {
id, err := operationaltrust.NewNotificationID(
nq.diagnosticDestinationID(notif.DestinationID),
notif.Type,
notif.CreatedAt,
transitionIDs,
)
if err != nil {
return fmt.Errorf("build notification id: %w", err)
}
notif.ID = id
} else {
notif.ID = fmt.Sprintf("%s-%d", notif.Type, notif.CreatedAt.UnixNano())
}
}
if notif.Status == "" {
notif.Status = QueueStatusPending
}
if notif.MaxAttempts <= 0 {
notif.MaxAttempts = defaultQueueMaxAttempts
}
if notif.Attempts < 0 {
notif.Attempts = 0
}
var err error
notif.Links, err = normalizeNotificationLinks(
notif.ID,
notif.DestinationID,
notif.Links,
notificationDeliveryStateForQueueStatus(notif.Status),
nil,
nil,
)
if err != nil {
return fmt.Errorf("normalize notification links: %w", err)
}
alertsJSON, err := json.Marshal(notif.Alerts)
if err != nil {
return fmt.Errorf("failed to marshal alerts: %w", err)
}
linksJSON, err := json.Marshal(notif.Links)
if err != nil {
return fmt.Errorf("failed to marshal operational links: %w", err)
}
if err := nq.bindQueuedRecoveryEpochLocked(notif); err != nil {
return err
}
query := `
INSERT INTO notification_queue
(id, type, method, status, alerts, operational_links, config, attempts, max_attempts, created_at, next_retry_at, receipt_epoch)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
var nextRetryAt *int64
if notif.NextRetryAt != nil {
ts := notif.NextRetryAt.Unix()
nextRetryAt = &ts
}
_, err = nq.db.Exec(query,
notif.ID,
notif.Type,
notif.Method,
notif.Status,
string(alertsJSON),
string(linksJSON),
string(notif.Config),
notif.Attempts,
notif.MaxAttempts,
notif.CreatedAt.Unix(),
nextRetryAt,
notif.ReceiptEpoch,
)
if err != nil {
return fmt.Errorf("failed to enqueue notification: %w", err)
}
log.Debug().
Str("id", notif.ID).
Str("type", notif.Type).
Int("alertCount", len(notif.Alerts)).
Msg("notification enqueued")
metrics := operationaltrust.GetMetrics()
metrics.ObserveNotificationDelivery("queued")
for _, alert := range notif.Alerts {
if alert == nil ||
alert.LatestTransition == nil ||
alert.LatestTransition.To != operationaltrust.OperationalOpen {
continue
}
metrics.ObserveOpenToNotification(
alert.LatestTransition.At,
notif.CreatedAt,
)
}
// Signal processor that new work is available
select {
case nq.notifyChan <- struct{}{}:
default:
}
return nil
}
// UpdateStatus updates the status of a queued notification without incrementing attempts
func (nq *NotificationQueue) UpdateStatus(id string, status NotificationQueueStatus, errorMsg string) error {
return nq.updateStatus(id, status, errorMsg, true)
}
// Delivery workers defer reconciliation until their audit is recorded and
// their per-alert gates are released. Explicit operator transitions still
// reconcile immediately after committing the status.
func (nq *NotificationQueue) updateStatus(id string, status NotificationQueueStatus, errorMsg string, notifyHealth bool) error {
nq.mu.Lock()
err := nq.updateNotificationStatusNoLock(id, status, errorMsg, time.Now())
nq.mu.Unlock()
if err == nil && notifyHealth && notificationStatusChangesDeliveryHealth(status) {
nq.notifyDeliveryHealthChanged()
}
return err
}
func notificationStatusChangesDeliveryHealth(status NotificationQueueStatus) bool {
switch status {
case QueueStatusFailed, QueueStatusDLQ, QueueStatusCancelled:
return true
default:
return false
}
}
func (nq *NotificationQueue) updateNotificationStatusNoLock(
id string,
status NotificationQueueStatus,
errorMsg string,
now time.Time,
) error {
links, acknowledged, err := nq.acceptedLinksLocked(id)
if err == nil && !acknowledged {
links, err = nq.readNotificationLinksNoLock(id)
}
if err != nil {
return err
}
if !acknowledged || status != QueueStatusSent {
links = transitionNotificationLinks(links, notificationDeliveryStateForQueueStatus(status), now)
}
linksJSON, err := json.Marshal(links)
if err != nil {
return fmt.Errorf("marshal notification links for status update: %w", err)
}
nowUnix := now.Unix()
var completedAt *int64
if status == QueueStatusSent ||
status == QueueStatusFailed ||
status == QueueStatusDLQ ||
status == QueueStatusCancelled {
completedAt = &nowUnix
}
query := `
UPDATE notification_queue
SET status = ?, last_attempt = CASE WHEN ? THEN last_attempt ELSE ? END, last_error = ?, completed_at = ?,
operational_links = ?,
next_retry_at = CASE WHEN ? THEN NULL ELSE next_retry_at END
WHERE id = ?
`
result, err := nq.db.Exec(
query,
status,
acknowledged,
nowUnix,
errorMsg,
completedAt,
string(linksJSON),
completedAt != nil,
id,
)
if err != nil {
return fmt.Errorf("failed to update notification status: %w", err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("notification not found: %s", id)
}
return nil
}
func (nq *NotificationQueue) readNotificationLinksNoLock(
id string,
) ([]operationaltrust.NotificationLink, error) {
var raw string
if err := nq.db.QueryRow(
`SELECT operational_links FROM notification_queue WHERE id = ?`,
id,
).Scan(&raw); err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("notification not found: %s", id)
}
return nil, fmt.Errorf("read notification links: %w", err)
}
var links []operationaltrust.NotificationLink
if err := json.Unmarshal([]byte(raw), &links); err != nil {
return nil, fmt.Errorf("unmarshal notification links: %w", err)
}
return links, nil
}
func (nq *NotificationQueue) getNotificationLinks(
id string,
) ([]operationaltrust.NotificationLink, error) {
nq.mu.RLock()
defer nq.mu.RUnlock()
return nq.readNotificationLinksNoLock(id)
}
func notificationDeliveryStateForQueueStatus(
status NotificationQueueStatus,
) operationaltrust.NotificationDeliveryState {
switch status {
case QueueStatusSending:
return operationaltrust.NotificationDelivering
case QueueStatusSent:
return operationaltrust.NotificationDelivered
case QueueStatusFailed:
return operationaltrust.NotificationFailed
case QueueStatusDLQ:
return operationaltrust.NotificationDeadLetter
case QueueStatusCancelled:
return operationaltrust.NotificationCancelled
default:
return operationaltrust.NotificationQueued
}
}
func transitionNotificationLinks(
links []operationaltrust.NotificationLink,
state operationaltrust.NotificationDeliveryState,
at time.Time,
) []operationaltrust.NotificationLink {
transitioned := make([]operationaltrust.NotificationLink, len(links))
for index := range links {
transitioned[index] = links[index].Clone()
transitioned[index].DeliveryState = state
switch state {
case operationaltrust.NotificationQueued:
transitioned[index].AttemptedAt = nil
transitioned[index].CompletedAt = nil
case operationaltrust.NotificationDelivering:
attemptedAt := at
transitioned[index].AttemptedAt = &attemptedAt
transitioned[index].CompletedAt = nil
case operationaltrust.NotificationRetrying:
transitioned[index].CompletedAt = nil
case operationaltrust.NotificationDelivered,
operationaltrust.NotificationFailed,
operationaltrust.NotificationDeadLetter,
operationaltrust.NotificationCancelled:
if transitioned[index].AttemptedAt != nil {
completedAt := at
transitioned[index].CompletedAt = &completedAt
}
}
}
return transitioned
}
// IncrementAttempt increments the attempt counter for a notification
func (nq *NotificationQueue) IncrementAttempt(id string) error {
nq.mu.Lock()
defer nq.mu.Unlock()
query := `UPDATE notification_queue SET attempts = attempts + 1 WHERE id = ?`
_, err := nq.db.Exec(query, id)
if err != nil {
return fmt.Errorf("failed to increment attempt counter: %w", err)
}
return nil
}
// IncrementAttemptAndSetStatus updates a queue row and its operational links
// atomically. Delivery workers use claimPendingForDelivery so a concurrent
// resolution can cancel a pending row before it is claimed; this method
// remains available for explicit lifecycle transitions and compatibility.
func (nq *NotificationQueue) IncrementAttemptAndSetStatus(id string, status NotificationQueueStatus) error {
nq.mu.Lock()
defer nq.mu.Unlock()
now := time.Now()
links, err := nq.readNotificationLinksNoLock(id)
if err != nil {
return err
}
links = transitionNotificationLinks(
links,
notificationDeliveryStateForQueueStatus(status),
now,
)
linksJSON, err := json.Marshal(links)
if err != nil {
return fmt.Errorf("marshal notification links for attempt: %w", err)
}
query := `
UPDATE notification_queue
SET attempts = attempts + 1,
status = ?,
last_attempt = ?,
operational_links = ?
WHERE id = ?
`
if _, err := nq.db.Exec(query, status, now.Unix(), string(linksJSON), id); err != nil {
return fmt.Errorf("failed to increment attempt and set status: %w", err)
}
return nil
}
// claimPendingForDelivery atomically claims a pending queue row and refreshes
// its alert list. The refresh is required because resolution cancellation may
// have rewritten a grouped row after GetPending returned its snapshot.
func (nq *NotificationQueue) claimPendingForDelivery(notif *QueuedNotification) (bool, error) {
nq.mu.Lock()
defer nq.mu.Unlock()
now := time.Now()
ack, err := nq.providerAcknowledgedAtLocked(notif.ID)
if err != nil {
return false, err
}
notif.ProviderAcceptedAt = ack
if ack == nil {
ready, err := nq.prepareTelegramBatchLocked(notif, now)
if err != nil || !ready {
return false, err
}
}
var links []operationaltrust.NotificationLink
if ack != nil {
links, _, err = nq.acceptedLinksLocked(notif.ID)
} else {
links, err = nq.readNotificationLinksNoLock(notif.ID)
}
if err != nil {
return false, err
}
if ack == nil {
links = transitionNotificationLinks(links, notificationDeliveryStateForQueueStatus(QueueStatusSending), now)
}
linksJSON, err := json.Marshal(links)
if err != nil {
return false, fmt.Errorf("marshal notification links for attempt: %w", err)
}
query := `
UPDATE notification_queue
SET attempts = attempts + CASE WHEN ? THEN 0 ELSE 1 END,
status = ?,
last_attempt = CASE WHEN ? THEN last_attempt ELSE ? END,
operational_links = ?
WHERE id = ? AND status = 'pending'
`
result, err := nq.db.Exec(
query,
ack != nil,
QueueStatusSending,
ack != nil,
now.Unix(),
string(linksJSON),
notif.ID,
)
if err != nil {
return false, fmt.Errorf("failed to claim pending notification: %w", err)
}
rows, err := result.RowsAffected()
if err != nil {
return false, fmt.Errorf("read claimed notification row count: %w", err)
}
if rows == 0 {
return false, nil
}
var refreshedAlerts []*alerts.Alert
if ack == nil {
var alertsJSON []byte
if err := nq.db.QueryRow(`SELECT alerts FROM notification_queue WHERE id = ?`, notif.ID).Scan(&alertsJSON); err != nil {
return false, fmt.Errorf("reload claimed notification alerts: %w", err)
}
if err := json.Unmarshal(alertsJSON, &refreshedAlerts); err != nil {
return false, fmt.Errorf("decode claimed notification alerts: %w", err)
}
}
notif.Alerts = refreshedAlerts
if ack == nil {
notif.Attempts++
}
notif.Status = QueueStatusSending
if ack == nil {
notif.LastAttempt = &now
}
notif.Links = links
return true, nil
}
// GetPending returns notifications ready for processing
func (nq *NotificationQueue) GetPending(limit int) ([]*QueuedNotification, error) {
nq.mu.RLock()
defer nq.mu.RUnlock()
query := `
SELECT id, type, method, status,
CASE WHEN EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1) THEN '[]' ELSE alerts END,
config, attempts, max_attempts,
last_attempt, last_error, created_at, next_retry_at, completed_at, payload_bytes,
CASE WHEN NOT json_valid(operational_links) AND EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1) THEN '[]' ELSE operational_links END, receipt_epoch
FROM notification_queue
WHERE status = 'pending'
AND (next_retry_at IS NULL OR next_retry_at <= ?)
ORDER BY created_at ASC
LIMIT ?
`
rows, err := nq.db.Query(query, time.Now().Unix(), limit)
if err != nil {
return nil, fmt.Errorf("failed to query pending notifications: %w", err)
}
defer rows.Close()
var notifications []*QueuedNotification
for rows.Next() {
notif, err := nq.scanNotification(rows)
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "scan_pending_row").
Str("queueStatus", string(QueueStatusPending)).
Int("batchLimit", limit).
Str("dbPath", nq.dbPath).
Msg("Failed to scan notification row")
continue
}
notifications = append(notifications, notif)
}
return notifications, rows.Err()
}
// scanNotification scans a database row into a QueuedNotification
func (nq *NotificationQueue) scanNotification(rows interface{ Scan(...any) error }) (*QueuedNotification, error) {
var notif QueuedNotification
var alertsJSON, configJSON, linksJSON string
var lastAttempt, nextRetryAt, completedAt *int64
var createdAtUnix int64
err := rows.Scan(
&notif.ID,
&notif.Type,
&notif.Method,
&notif.Status,
&alertsJSON,
&configJSON,
&notif.Attempts,
&notif.MaxAttempts,
&lastAttempt,
&notif.LastError,
&createdAtUnix,
&nextRetryAt,
&completedAt,
&notif.PayloadBytes,
&linksJSON,
&notif.ReceiptEpoch,
)
if err != nil {
return nil, err
}
notif.CreatedAt = time.Unix(createdAtUnix, 0)
if lastAttempt != nil {
t := time.Unix(*lastAttempt, 0)
notif.LastAttempt = &t
}
if nextRetryAt != nil {
t := time.Unix(*nextRetryAt, 0)
notif.NextRetryAt = &t
}
if completedAt != nil {
t := time.Unix(*completedAt, 0)
notif.CompletedAt = &t
}
if err := json.Unmarshal([]byte(alertsJSON), &notif.Alerts); err != nil {
return nil, fmt.Errorf("failed to unmarshal alerts: %w", err)
}
notif.Config = json.RawMessage(configJSON)
if err := json.Unmarshal([]byte(linksJSON), &notif.Links); err != nil {
return nil, fmt.Errorf("failed to unmarshal operational links: %w", err)
}
if len(notif.Links) > 0 {
notif.DestinationID = notif.Links[0].DestinationID
}
return &notif, nil
}
// ScheduleRetry schedules a notification for retry with exponential backoff.
// Cancelled and delivered rows are final, even for stale operator requests.
func (nq *NotificationQueue) ScheduleRetry(id string, attempt int) error {
return nq.scheduleRetryWithClass(id, attempt, NotificationFailureUnknown, true)
}
func (nq *NotificationQueue) scheduleRetry(id string, attempt int, notifyHealth bool) error {
return nq.scheduleRetryWithClass(id, attempt, NotificationFailureUnknown, notifyHealth)
}
func (nq *NotificationQueue) scheduleRetryWithClass(id string, attempt int, failureClass NotificationFailureClass, notifyHealth bool) error {
return nq.scheduleRetryWithHint(id, attempt, failureClass, 0, notifyHealth)
}
func (nq *NotificationQueue) scheduleRetryWithHint(id string, attempt int, failureClass NotificationFailureClass, retryAfter time.Duration, notifyHealth bool) error {
backoff := calculateBackoffForRetry(attempt, failureClass, retryAfter)
nextRetry := time.Now().Add(backoff)
nq.mu.Lock()
links, err := nq.readNotificationLinksNoLock(id)
if err != nil {
nq.mu.Unlock()
return err
}
links = transitionNotificationLinks(
links,
operationaltrust.NotificationRetrying,
time.Now(),
)
linksJSON, err := json.Marshal(links)
if err != nil {
nq.mu.Unlock()
return fmt.Errorf("marshal notification links for retry: %w", err)
}
query := `
UPDATE notification_queue
SET status = 'pending', next_retry_at = ?, last_attempt = ?,
operational_links = ?, completed_at = NULL, last_error = NULL
WHERE id = ? AND status IN ('pending', 'sending', 'failed', 'dlq')
`
result, err := nq.db.Exec(
query,
nextRetry.Unix(),
time.Now().Unix(),
string(linksJSON),
id,
)
if err != nil {
nq.mu.Unlock()
return fmt.Errorf("failed to schedule retry: %w", err)
}
affected, err := result.RowsAffected()
nq.mu.Unlock()
if err != nil {
return fmt.Errorf("read scheduled retry result: %w", err)
}
if affected == 0 {
return fmt.Errorf("notification %s is no longer eligible for retry", id)
}
log.Debug().
Str("id", id).
Int("attempt", attempt).
Dur("backoff", backoff).
Time("nextRetry", nextRetry).
Msg("notification retry scheduled")
if notifyHealth {
nq.notifyDeliveryHealthChanged()
}
return nil
}
// MoveToDLQ moves a notification to the dead letter queue
func (nq *NotificationQueue) MoveToDLQ(id string, reason string) error {
return nq.UpdateStatus(id, QueueStatusDLQ, reason)
}
// GetDLQ returns dead-letter records for operator inspection. Delivery uses
// the stored records, not these diagnostic destination aliases.
func (nq *NotificationQueue) GetDLQ(limit int) ([]*QueuedNotification, error) {
nq.mu.RLock()
defer nq.mu.RUnlock()
query := `
SELECT id, type, method, status, alerts, config, attempts, max_attempts,
last_attempt, last_error, created_at, next_retry_at, completed_at, payload_bytes,
operational_links, receipt_epoch
FROM notification_queue
WHERE status = 'dlq'
ORDER BY completed_at DESC
LIMIT ?
`
rows, err := nq.db.Query(query, limit)
if err != nil {
return nil, fmt.Errorf("failed to query DLQ: %w", err)
}
defer rows.Close()
var notifications []*QueuedNotification
for rows.Next() {
notif, err := nq.scanNotification(rows)
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "scan_dlq_row").
Str("queueStatus", string(QueueStatusDLQ)).
Int("batchLimit", limit).
Str("dbPath", nq.dbPath).
Msg("Failed to scan DLQ notification")
continue
}
notif.DestinationID = nq.diagnosticDestinationID(notif.DestinationID)
for i := range notif.Links {
notif.Links[i].DestinationID = nq.diagnosticDestinationID(notif.Links[i].DestinationID)
}
notifications = append(notifications, notif)
}
return notifications, rows.Err()
}
// RetryTerminalFailures returns every retained failed or dead-lettered
// delivery to the pending queue with a fresh retry budget. The per-attempt
// audit rows remain intact, so this operator recovery never rewrites delivery
// history or makes an earlier failure look successful.
func (nq *NotificationQueue) RetryTerminalFailures() (int, error) {
return nq.resolveTerminalFailures(true)
}
// DismissTerminalFailures marks every retained failed or dead-lettered
// delivery as cancelled. This clears the active queue-health warning while
// retaining the immutable delivery audit instead of asking operators to
// delete the queue database.
func (nq *NotificationQueue) DismissTerminalFailures() (int, error) {
return nq.resolveTerminalFailures(false)
}
// replayRecoverableDeadLettersForDestination re-queues a bounded batch of
// recent, retryable dead-letter deliveries for the exact same destination and
// config snapshot after a live delivery to that destination succeeds.
//
// Invariants:
// - Scoped to the exact same base notification channel (alert + resolved)
// and identical snapshotted config JSON, so edited/sibling destinations
// never cross-replay.
// - Only replays rows whose latest audit failure class is retryable
// (connectivity, server_error, rate_limited, tls, unknown); deterministic
// failures (authentication, configuration, rejected) remain in DLQ.
// - Bounded to autoReplayDLQWindow (24h), autoReplayDLQBatchLimit (25), and
// autoReplayDLQCooldown (5m per destination once a batch replays) to
// prevent ancient alert storms and cascading batch drain.
// - At most one automatic recovery replay per row (enforced via immutable
// audit count <= max_attempts) with a single delivery attempt on replay,
// preventing infinite loops on flapping destinations while leaving manual
// RetryTerminalFailures unconstrained.
func (nq *NotificationQueue) replayRecoverableDeadLettersForDestination(notif *QueuedNotification) (int, error) {
if nq == nil || nq.db == nil || notif == nil {
return 0, nil
}
for _, alert := range notif.Alerts {
if rehearsalID(alert) != "" {
// Rehearsals exercise the saved queue route without releasing
// someone else's retained notifications.
return 0, nil
}
}
baseType, _ := normalizeQueueType(notif.Type)
rawConfig := string(notif.Config)
trimmedConfig := strings.TrimSpace(rawConfig)
if baseType == "" || trimmedConfig == "" || trimmedConfig == "{}" || trimmedConfig == "null" {
return 0, nil
}
nq.mu.Lock()
replayed, err := nq.replayRecoverableDeadLettersForDestinationNoLock(notif.ID, baseType, rawConfig)
nq.mu.Unlock()
if err != nil {
return 0, err
}
if replayed > 0 {
select {
case nq.notifyChan <- struct{}{}:
default:
}
log.Info().
Str("component", "notification_queue").
Str("action", "auto_replay_dlq_on_recovery").
Str("triggerId", notif.ID).
Str("type", baseType).
Int("replayed", replayed).
Msg("Auto-replayed recoverable dead-letter notifications after destination recovery")
}
return replayed, nil
}
func (nq *NotificationQueue) replayRecoverableDeadLettersForDestinationNoLock(triggerID, baseType, rawConfig string) (int, error) {
now := time.Now()
if nq.now != nil {
now = nq.now()
}
destinationKey := baseType + "\x00" + rawConfig
if nq.lastAutoReplay != nil {
if lastAt, ok := nq.lastAutoReplay[destinationKey]; ok && now.Sub(lastAt) < autoReplayDLQCooldown {
return 0, nil
}
}
cutoff := now.Add(-autoReplayDLQWindow).Unix()
resolvedType := baseType + "_resolved"
rows, err := nq.db.Query(`
SELECT
q.id,
q.max_attempts,
q.operational_links,
COALESCE((
SELECT a.failure_class
FROM notification_audit a
WHERE a.notification_id = q.id
ORDER BY a.timestamp DESC, a.id DESC
LIMIT 1
), '') AS latest_failure_class,
COALESCE((
SELECT a.error_message
FROM notification_audit a
WHERE a.notification_id = q.id
ORDER BY a.timestamp DESC, a.id DESC
LIMIT 1
), '') AS latest_error
FROM notification_queue q
WHERE q.status IN ('failed', 'dlq')
AND q.id != ?
AND q.type IN (?, ?)
AND q.config = ?
AND COALESCE(q.completed_at, q.created_at) >= ?
AND NOT EXISTS (
SELECT 1 FROM notification_audit a
WHERE a.notification_id = q.id AND a.success = 1
)
AND NOT EXISTS (
SELECT 1 FROM notification_rehearsal_jobs j WHERE j.notification_id = q.id
)
AND (SELECT COUNT(*) FROM notification_audit a WHERE a.notification_id = q.id)
BETWEEN 1 AND CASE WHEN q.max_attempts > 0 THEN q.max_attempts ELSE 1 END
ORDER BY COALESCE(q.completed_at, q.created_at), q.id
`, triggerID, baseType, resolvedType, rawConfig, cutoff)
if err != nil {
return 0, fmt.Errorf("query recoverable dead-letter notifications: %w", err)
}
type candidate struct {
id string
maxAttempts int
links []operationaltrust.NotificationLink
}
// Cap admitted rows, not the matching prefix: an old configuration
// failure (including a legacy audit with no stored class) must not
// consume a retry slot on every later success.
var candidates []candidate
for len(candidates) < autoReplayDLQBatchLimit && rows.Next() {
var (
id string
maxAttempts int
rawLinks string
latestFailureClass string
latestError string
)
if err := rows.Scan(&id, &maxAttempts, &rawLinks, &latestFailureClass, &latestError); err != nil {
_ = rows.Close()
return 0, fmt.Errorf("scan recoverable dead-letter notification: %w", err)
}
fc := NotificationFailureClass(strings.TrimSpace(latestFailureClass))
if fc == "" {
fc = ClassifyNotificationFailure(latestError)
}
if !fc.Retryable() {
continue
}
var links []operationaltrust.NotificationLink
if err := json.Unmarshal([]byte(rawLinks), &links); err != nil {
_ = rows.Close()
return 0, fmt.Errorf("decode recoverable dead-letter notification links: %w", err)
}
candidates = append(candidates, candidate{id: id, maxAttempts: maxAttempts, links: links})
}
if err := rows.Close(); err != nil {
return 0, fmt.Errorf("close recoverable dead-letter notification rows: %w", err)
}
if err := rows.Err(); err != nil {
return 0, fmt.Errorf("iterate recoverable dead-letter notifications: %w", err)
}
if len(candidates) == 0 {
return 0, nil
}
tx, err := nq.db.Begin()
if err != nil {
return 0, fmt.Errorf("begin auto-replay dead-letter recovery: %w", err)
}
defer func() { _ = tx.Rollback() }()
replayed := 0
for _, item := range candidates {
linksJSON, err := json.Marshal(transitionNotificationLinks(item.links, operationaltrust.NotificationRetrying, now))
if err != nil {
return 0, fmt.Errorf("encode auto-replay notification links: %w", err)
}
replayAttempts := item.maxAttempts - 1
if replayAttempts < 0 {
replayAttempts = 0
}
result, err := tx.Exec(`
UPDATE notification_queue
SET status = 'pending', attempts = ?, last_attempt = NULL,
last_error = NULL, next_retry_at = NULL, completed_at = NULL,
operational_links = ?
WHERE id = ? AND status IN ('failed', 'dlq')
`, replayAttempts, string(linksJSON), item.id)
if err != nil {
return 0, fmt.Errorf("requeue recoverable dead-letter notification %s: %w", item.id, err)
}
if count, err := result.RowsAffected(); err == nil {
replayed += int(count)
}
}
if err := tx.Commit(); err != nil {
return 0, fmt.Errorf("commit auto-replay dead-letter recovery: %w", err)
}
if replayed > 0 {
if nq.lastAutoReplay == nil {
nq.lastAutoReplay = make(map[string]time.Time)
}
if len(nq.lastAutoReplay) >= 256 {
for k, ts := range nq.lastAutoReplay {
if now.Sub(ts) >= autoReplayDLQCooldown {
delete(nq.lastAutoReplay, k)
}
}
}
nq.lastAutoReplay[destinationKey] = now
}
return replayed, nil
}
func (nq *NotificationQueue) resolveTerminalFailures(retry bool) (int, error) {
if nq == nil || nq.db == nil {
return 0, fmt.Errorf("notification queue not initialized")
}
nq.mu.Lock()
affected, err := nq.resolveTerminalFailuresNoLock(retry)
nq.mu.Unlock()
if err != nil {
return 0, err
}
// Reconcile even when no rows were affected. A prior process may already
// have repaired the queue while leaving a persisted system alert active.
nq.notifyDeliveryHealthChanged()
return affected, nil
}
func (nq *NotificationQueue) resolveTerminalFailuresNoLock(retry bool) (int, error) {
rows, err := nq.db.Query(`
SELECT id, operational_links
FROM notification_queue
WHERE status IN ('failed', 'dlq') AND NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)
ORDER BY completed_at, id
`)
if err != nil {
return 0, fmt.Errorf("read retained terminal notifications: %w", err)
}
type terminalNotification struct {
id string
links []operationaltrust.NotificationLink
}
terminal := make([]terminalNotification, 0)
for rows.Next() {
var id, rawLinks string
if err := rows.Scan(&id, &rawLinks); err != nil {
_ = rows.Close()
return 0, fmt.Errorf("scan retained terminal notification: %w", err)
}
var links []operationaltrust.NotificationLink
if err := json.Unmarshal([]byte(rawLinks), &links); err != nil {
_ = rows.Close()
return 0, fmt.Errorf("decode retained terminal notification links: %w", err)
}
terminal = append(terminal, terminalNotification{id: id, links: links})
}
if err := rows.Close(); err != nil {
return 0, fmt.Errorf("close retained terminal notification rows: %w", err)
}
if err := rows.Err(); err != nil {
return 0, fmt.Errorf("iterate retained terminal notifications: %w", err)
}
if len(terminal) == 0 {
return 0, nil
}
tx, err := nq.db.Begin()
if err != nil {
return 0, fmt.Errorf("begin terminal notification recovery: %w", err)
}
defer func() { _ = tx.Rollback() }()
now := time.Now()
affected := 0
for _, notification := range terminal {
state := operationaltrust.NotificationCancelled
if retry {
state = operationaltrust.NotificationRetrying
}
linksJSON, err := json.Marshal(transitionNotificationLinks(notification.links, state, now))
if err != nil {
return 0, fmt.Errorf("encode terminal notification links: %w", err)
}
var result sql.Result
if retry {
result, err = tx.Exec(`
UPDATE notification_queue
SET status = 'pending', attempts = 0, last_attempt = NULL,
last_error = NULL, next_retry_at = ?, completed_at = NULL,
operational_links = ?
WHERE id = ? AND status IN ('failed', 'dlq')
`, now.Unix(), string(linksJSON), notification.id)
} else {
result, err = tx.Exec(`
UPDATE notification_queue
SET status = 'cancelled', last_attempt = ?,
last_error = 'Dismissed by operator', next_retry_at = NULL,
completed_at = ?, operational_links = ?
WHERE id = ? AND status IN ('failed', 'dlq')
`, now.Unix(), now.Unix(), string(linksJSON), notification.id)
}
if err != nil {
return 0, fmt.Errorf("resolve terminal notification %s: %w", notification.id, err)
}
if count, countErr := result.RowsAffected(); countErr == nil {
affected += int(count)
}
}
if err := tx.Commit(); err != nil {
return 0, fmt.Errorf("commit terminal notification recovery: %w", err)
}
if retry && affected > 0 {
select {
case nq.notifyChan <- struct{}{}:
default:
}
}
return affected, nil
}
// SetDeliveryHealthChangedCallback installs the monitoring-owned reconciliation
// hook. Notification delivery owns queue truth; monitoring owns its system
// alert projection, so the queue only announces that the verdict may have
// changed after committing and releasing its database lock.
func (nq *NotificationQueue) SetDeliveryHealthChangedCallback(callback func()) {
if nq == nil {
return
}
nq.mu.Lock()
nq.deliveryHealthChanged = callback
nq.mu.Unlock()
}
func (nq *NotificationQueue) notifyDeliveryHealthChanged() {
if nq == nil {
return
}
nq.mu.RLock()
callback := nq.deliveryHealthChanged
nq.mu.RUnlock()
if callback != nil {
callback()
}
}
// RecordAudit records a notification delivery attempt in the audit log,
// deriving the failure class from the error text. Prefer RecordAuditError,
// which keeps the class the sender declared.
func (nq *NotificationQueue) RecordAudit(notif *QueuedNotification, success bool, errorMsg string) error {
failureClass := NotificationFailureClass("")
if !success {
failureClass = ClassifyNotificationFailure(errorMsg)
}
return nq.recordAudit(notif, success, errorMsg, failureClass)
}
// RecordAuditError records a delivery attempt from the send error itself, so a
// class the sender declared reaches the audit row instead of being re-derived
// from the message. This is what keeps a destination's response body out of the
// classification decision.
func (nq *NotificationQueue) RecordAuditError(notif *QueuedNotification, success bool, sendErr error) error {
errorMsg := ""
failureClass := NotificationFailureClass("")
if !success {
if sendErr != nil {
errorMsg = sendErr.Error()
}
failureClass = ClassifyNotificationFailureError(sendErr)
}
return nq.recordAudit(notif, success, errorMsg, failureClass)
}
func (nq *NotificationQueue) recordAudit(notif *QueuedNotification, success bool, errorMsg string, failureClass NotificationFailureClass) error {
nq.mu.Lock()
defer nq.mu.Unlock()
tx, err := nq.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
err = nq.recordAuditWithExecutor(tx, notif, success, errorMsg, failureClass)
if err == nil {
err = tx.Commit()
}
if err == nil {
operationaltrust.GetMetrics().ObserveNotificationDelivery(deliveryOutcome(notif.Status, success, notif.Attempts))
}
return err
}
type notificationAuditExecutor interface {
Exec(string, ...any) (sql.Result, error)
}
func (nq *NotificationQueue) recordAuditWithExecutor(executor notificationAuditExecutor, notif *QueuedNotification, success bool, errorMsg string, failureClass NotificationFailureClass) error {
alertIdentifiers := make([]string, len(notif.Alerts))
for i, alert := range notif.Alerts {
if alert != nil {
alertIdentifiers[i] = alert.ID
}
}
alertIdentifiersJSON, _ := json.Marshal(alertIdentifiers)
linksJSON, err := json.Marshal(notif.Links)
if err != nil {
return fmt.Errorf("failed to marshal notification links for audit: %w", err)
}
query := `
INSERT INTO notification_audit
(notification_id, type, method, status, alert_identifiers, alert_count, operational_links, attempts, success, error_message, failure_class, destination_id, payload_size, timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
at := time.Now()
result, err := executor.Exec(query,
notif.ID,
notif.Type,
notif.Method,
notif.Status,
string(alertIdentifiersJSON),
len(notif.Alerts),
string(linksJSON),
notif.Attempts,
success,
errorMsg,
string(failureClass),
strings.TrimSpace(notif.DestinationID),
notif.PayloadBytes,
at.Unix(),
)
if err == nil && success {
var auditID int64
auditID, err = result.LastInsertId()
if err == nil {
err = recordAcceptedDeliveryFacts(executor, auditID, notif, at)
}
}
return err
}
// GetQueueStats returns counts for the rows currently retained by the queue.
// Sent, failed, and cancelled rows are retained for seven days; dead-letter
// rows are retained for 30 days; pending and sending rows remain until they
// reach a terminal state. Callers must not present these mixed retention
// windows as a rate or as lifetime delivery history.
func (nq *NotificationQueue) GetQueueStats() (map[string]int, error) {
nq.mu.RLock()
defer nq.mu.RUnlock()
query := `
SELECT status, COUNT(*) as count,
SUM(CASE WHEN (status IN ('pending','sending') AND last_error=?) OR (status='pending' AND EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)) THEN 1 ELSE 0 END),
SUM(CASE WHEN status IN ('failed','dlq') AND EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1) THEN 1 ELSE 0 END),
SUM(CASE WHEN status='pending' AND last_error=? THEN 1 ELSE 0 END)
FROM notification_queue
GROUP BY status
`
rows, err := nq.db.Query(query, notificationSettlementPendingMessage, notificationOccurrencePendingMessage)
if err != nil {
return nil, err
}
defer rows.Close()
stats := make(map[string]int)
for rows.Next() {
var status string
var count int
var unsettled, acceptedTerminal, occurrencePending int
if err := rows.Scan(&status, &count, &unsettled, &acceptedTerminal, &occurrencePending); err != nil {
return nil, fmt.Errorf("scan notification queue statistics: %w", err)
}
stats[status] = count - acceptedTerminal
if occurrencePending > 0 {
stats["occurrence_pending"] += occurrencePending
}
unsettled += acceptedTerminal
if unsettled > 0 {
stats["settlement_pending"] += unsettled
}
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterate notification queue statistics: %w", err)
}
if err := rows.Close(); err != nil {
return nil, err
}
acceptedSending, err := nq.db.Query(`SELECT id FROM notification_queue WHERE status='sending' AND (last_error IS NULL OR last_error<>?) AND EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)`, notificationSettlementPendingMessage)
if err != nil {
return nil, err
}
defer acceptedSending.Close()
for acceptedSending.Next() {
var id string
if err := acceptedSending.Scan(&id); err != nil {
return nil, err
}
if _, owned := nq.processingNotifications[id]; !owned {
stats["settlement_pending"]++
}
}
if err := acceptedSending.Err(); err != nil {
return nil, err
}
return stats, nil
}
// TelemetryStats is a content-free delivery outcome aggregate. It deliberately
// excludes notification, destination, tenant, alert, and resource identities.
type TelemetryStats struct {
Attempts int
Deliveries int
// Failures counts terminal delivery failures only. Recoverable failed
// attempts that returned to pending for retry are included in Attempts but
// not in Failures.
Failures int
FailureClasses NotificationFailureClassCounts
}
// GetTelemetryStats returns aggregate delivery activity from locally retained
// per-attempt audit rows recorded on or after since. Attempts includes retries;
// Deliveries counts successful terminal sends; Failures counts only failed or
// dead-letter terminal outcomes. Callers must not interpret a window longer
// than the queue's completed-row retention as complete.
func (nq *NotificationQueue) GetTelemetryStats(since time.Time) (TelemetryStats, error) {
if nq == nil {
return TelemetryStats{}, nil
}
if since.IsZero() {
since = time.Now().Add(-7 * 24 * time.Hour)
}
nq.mu.RLock()
defer nq.mu.RUnlock()
var stats TelemetryStats
err := nq.db.QueryRow(`
SELECT
COUNT(*),
COALESCE(SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE
WHEN success = 0 AND status IN ('failed', 'dlq') THEN 1
ELSE 0
END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'authentication' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'rate_limited' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'connectivity' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'tls' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'configuration' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'rejected' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN success = 0 AND status IN ('failed', 'dlq') AND failure_class = 'server_error' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE
WHEN success = 0
AND status IN ('failed', 'dlq')
AND COALESCE(failure_class, '') NOT IN ('authentication', 'rate_limited', 'connectivity', 'tls', 'configuration', 'rejected', 'server_error')
THEN 1 ELSE 0
END), 0)
FROM notification_audit
WHERE timestamp >= ?
`, since.UTC().Unix()).Scan(
&stats.Attempts,
&stats.Deliveries,
&stats.Failures,
&stats.FailureClasses.Authentication,
&stats.FailureClasses.RateLimited,
&stats.FailureClasses.Connectivity,
&stats.FailureClasses.TLS,
&stats.FailureClasses.Configuration,
&stats.FailureClasses.Rejected,
&stats.FailureClasses.ServerError,
&stats.FailureClasses.Unknown,
)
if err != nil {
return TelemetryStats{}, fmt.Errorf("read notification telemetry aggregates: %w", err)
}
return stats, nil
}
// processQueue runs in background to process pending notifications
func (nq *NotificationQueue) processQueue() {
defer nq.wg.Done()
for {
select {
case <-nq.stopChan:
return
case <-nq.processorTicker.C:
nq.processBatch()
case <-nq.notifyChan:
// Process immediately when notified
nq.processBatch()
}
}
}
// SetProcessor sets the notification processor function
func (nq *NotificationQueue) SetProcessor(processor func(*QueuedNotification) error) {
nq.mu.Lock()
nq.processor = processor
nq.mu.Unlock()
if processor != nil {
select {
case nq.notifyChan <- struct{}{}:
default:
}
}
}
// installProcessor records the notification processor without waking the worker.
// Manager construction uses this instead of SetProcessor because construction
// runs before the owner applies saved destination configuration (webhooks,
// email, Apprise). Waking here would let an already-due persisted job be
// processed while the destination list is still empty and be terminally
// cancelled as "delivery disabled". The worker's ticker and any later enqueue
// begin delivery once configuration is present.
func (nq *NotificationQueue) installProcessor(processor func(*QueuedNotification) error) {
nq.mu.Lock()
nq.processor = processor
nq.mu.Unlock()
}
// processBatch processes a batch of pending notifications concurrently
func (nq *NotificationQueue) processBatch() {
const batchLimit = 20
nq.mu.RLock()
processorConfigured := nq.processor != nil
nq.mu.RUnlock()
if !processorConfigured {
return
}
if nq.stopping() {
return
}
// The owner loop waits for its prior batch before reaching this sweep.
// A durable acknowledgement permits local replay after a failed status
// transition, including when the first deferral write failed as well.
recoveryErr := nq.recoverAcknowledgedSending()
if recoveryErr != nil {
log.Error().Err(recoveryErr).Msg("Failed to recover acknowledged local settlement")
}
pending, err := nq.GetPending(batchLimit) // Increased batch size for concurrency
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "get_pending_batch").
Int("batchLimit", batchLimit).
Msg("Failed to get pending notifications")
return
}
if len(pending) == 0 {
return
}
log.Debug().
Str("component", "notification_queue").
Int("count", len(pending)).
Int("batchLimit", batchLimit).
Msg("Processing notification batch")
// Process notifications concurrently with semaphore limiting
var wg sync.WaitGroup
for _, notif := range pending {
wg.Add(1)
go func(n *QueuedNotification) {
defer wg.Done()
// Acquire semaphore slot
nq.workerSem <- struct{}{}
defer func() { <-nq.workerSem }()
nq.processNotification(n)
}(notif)
}
wg.Wait()
}
// processNotification processes a single notification
func (nq *NotificationQueue) processNotification(notif *QueuedNotification) {
// Skip cancelled notifications
if notif.Status == QueueStatusCancelled {
log.Debug().
Str("component", "notification_queue").
Str("action", "skip_cancelled").
Str("id", notif.ID).
Str("type", notif.Type).
Str("status", string(notif.Status)).
Msg("Skipping cancelled notification")
return
}
ownedID := notif.ID
nq.mu.Lock()
if _, owned := nq.processingNotifications[notif.ID]; owned {
nq.mu.Unlock()
return
}
if nq.processingNotifications == nil {
nq.processingNotifications = make(map[string]struct{})
}
nq.processingNotifications[notif.ID] = struct{}{}
nq.mu.Unlock()
defer func() {
nq.mu.Lock()
delete(nq.processingNotifications, ownedID)
delete(nq.processingNotifications, notif.ID)
nq.mu.Unlock()
}()
ack, ackErr := nq.providerAcknowledgedAt(notif.ID)
if ackErr != nil {
if err := nq.deferSettlement(notif, false); err != nil {
log.Error().Err(err).Msg("Failed to defer unreadable provider acknowledgement")
}
nq.notifyDeliveryHealthChanged()
return
}
notif.ProviderAcceptedAt = ack
// Obtain policy before the delivery gates: the policy owner may have
// lifecycle callbacks that cancel queued work. The returned evaluator is
// immutable and must not acquire any owner locks.
nq.mu.RLock()
policyProvider := nq.quietHoursPolicy
lifecycleProvider := nq.occurrenceLifecycle
nq.mu.RUnlock()
var quietHoursPolicy func(*alerts.Alert, time.Time) *time.Time
if policyProvider != nil {
quietHoursPolicy = policyProvider()
}
if notif.ProviderAcceptedAt == nil {
_, event := normalizeQueueType(notif.Type)
for _, snapshot := range notif.Alerts {
if snapshot == nil || snapshot.ID == "" || snapshot.StartTime.IsZero() {
continue
}
var policyErr error
if event == eventAlert {
policyErr = nq.reopenReceiptOccurrence(snapshot.ID, snapshot.StartTime, notif.ID)
} else if lifecycleProvider != nil {
state := lifecycleProvider(snapshot.ID, snapshot.StartTime)
resolved, _, dispositionErr := receiptDisposition(snapshot)
// An admitted legacy recovery can have unreadable optional disposition
// metadata. That is not a storage outage or evidence of reactivation.
// Positive current Active authority still cancels it; otherwise retain
// its admitted resolution rather than deferring it forever.
if state.State == alerts.NotificationOccurrenceActive || (dispositionErr == nil && activationAfterRetainedResolution(state.LatestActivationAt, resolved)) {
_, policyErr = nq.reopenReceiptOccurrenceWithToken(snapshot.ID, snapshot.StartTime, true, notif.ID)
// Existing recovery keeps its admitted resolution if newer lifecycle
// authority is unavailable or already inactive. Only guarded positive
// reactivation may cancel it.
if errors.Is(policyErr, ErrNotificationDeliverySkipped) || errors.Is(policyErr, errNotificationOccurrenceUnavailable) {
policyErr = nil
}
}
}
if policyErr != nil {
if errors.Is(policyErr, ErrNotificationDeliverySkipped) {
_, cancelErr := nq.cancelOccurrenceNotifications([]string{snapshot.ID}, func(candidate *alerts.Alert) bool {
return candidate.ID == snapshot.ID && candidate.StartTime.Equal(snapshot.StartTime)
}, eventAlert)
if cancelErr == nil {
return
}
}
reason := notificationSettlementPendingMessage
if errors.Is(policyErr, errNotificationOccurrenceUnavailable) || errors.Is(policyErr, errNotificationDispositionChanged) {
reason = notificationOccurrencePendingMessage
}
if err := nq.deferWithReason(notif, false, reason); err != nil {
log.Error().Err(err).Msg("Failed to defer notification lifecycle settlement")
}
nq.notifyDeliveryHealthChanged()
return
}
}
}
identifiers := alertIdentifiersFromAlerts(notif.Alerts)
if notif.ProviderAcceptedAt != nil {
var err error
identifiers, err = nq.acknowledgedAlertIdentifiers(notif.ID)
if err != nil {
if deferErr := nq.deferSettlement(notif, false); deferErr != nil {
log.Error().Err(deferErr).Msg("Failed to retain unreadable accepted occurrence facts")
}
nq.notifyDeliveryHealthChanged()
return
}
}
releaseDeliveryGates := nq.acquireAlertDeliveryGates(identifiers, false)
healthChanged := false
defer func() {
releaseDeliveryGates()
if healthChanged {
nq.notifyDeliveryHealthChanged()
}
}()
// A worker that observes the stop begins no hold, partition or claim, so
// its row stays pending for the next start. A worker already past this
// check finishes normally; stopProcessing waits for it.
if nq.stopping() {
return
}
if quietHoursPolicy != nil && notif.ProviderAcceptedAt == nil {
ready, err := nq.prepareQuietHoursDelivery(notif, quietHoursPolicy)
if err != nil {
log.Error().Err(err).Str("id", notif.ID).Msg("Failed to revalidate queued quiet-hours delivery")
}
if err != nil || !ready {
return
}
}
// Atomically claim the pending row. A concurrent resolution may have
// cancelled it while it was waiting for its per-alert delivery gate.
previousAttempt := notif.LastAttempt
claimed, err := nq.claimPendingForDelivery(notif)
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "increment_attempt_set_status").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts+1).
Int("maxAttempts", notif.MaxAttempts).
Msg("Failed to claim pending notification")
return
}
if !claimed {
log.Debug().
Str("component", "notification_queue").
Str("action", "skip_unclaimed").
Str("id", notif.ID).
Str("type", notif.Type).
Msg("Skipping notification because it is no longer pending")
return
}
// Call processor if set
nq.mu.RLock()
processor := nq.processor
nq.mu.RUnlock()
if processor == nil {
return
}
// The claim refreshed the durable acknowledgement. The real processor
// can add one during a new send, or return nil after local settlement
// of one that already existed. Only the new send can prove recovery.
alreadyAccepted := notif.ProviderAcceptedAt != nil
err = processor(notif)
var telegramWait *telegramRateHold
if errors.As(err, &telegramWait) {
// No HTTP attempt was made. Keep the row and its remaining attempt
// budget until the local sender can admit another request.
if deferErr := nq.deferTelegramRateHold(notif, telegramWait.retryAt, previousAttempt); deferErr != nil {
log.Error().Err(deferErr).Str("id", notif.ID).Msg("Failed to retain Telegram rate hold")
}
healthChanged = true
return
}
if errors.Is(err, errNotificationSettlementPending) {
if settleErr := nq.deferSettlement(notif, true, previousAttempt); settleErr != nil {
log.Error().Err(settleErr).Str("id", notif.ID).Msg("Failed to retain pending notification settlement")
}
healthChanged = true
return
}
if errors.Is(err, ErrNotificationDeliverySkipped) {
// No provider attempt occurred. Preserve the cancellation and its reason,
// but do not manufacture a successful (or failed) delivery audit entry.
// Reconcile health only after releasing the per-alert gate.
nq.mu.Lock()
cancelErr := nq.updateNotificationStatusNoLock(notif.ID, QueueStatusCancelled, err.Error(), time.Now())
nq.mu.Unlock()
if cancelErr != nil {
log.Error().Err(cancelErr).Str("id", notif.ID).Msg("Failed to cancel skipped notification")
} else {
healthChanged = true
}
return
}
success := err == nil
errorMsg := ""
failureClass := NotificationFailureClass("")
if err != nil {
errorMsg = err.Error()
failureClass = ClassifyNotificationFailureError(err)
}
if success {
// Mark as sent
if err := nq.UpdateStatus(notif.ID, QueueStatusSent, ""); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "mark_sent").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts).
Msg("Failed to update notification status to sent")
if notif.ProviderAcceptedAt != nil {
if settleErr := nq.deferSettlement(notif, false); settleErr != nil {
log.Error().Err(settleErr).Str("id", notif.ID).Msg("Failed to retain acknowledged queue completion")
}
healthChanged = true
return
}
} else {
healthChanged = notif.ProviderAcceptedAt != nil
completedAt := time.Now()
notif.Status = QueueStatusSent
notif.CompletedAt = &completedAt
notif.Links = transitionNotificationLinks(
notif.Links,
operationaltrust.NotificationDelivered,
completedAt,
)
}
log.Info().
Str("component", "notification_queue").
Str("action", "send_success").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts).
Int("maxAttempts", notif.MaxAttempts).
Msg("Notification sent successfully")
} else {
// Check if we should retry or move to DLQ. A deterministic failure
// class is dead-lettered on the spot: retrying an authentication,
// configuration, or rejection verdict cannot change the answer, and
// only delays the dead letter the operator needs to act on.
retryable := failureClass.Retryable()
if notif.Attempts >= notif.MaxAttempts || !retryable {
// Move to DLQ
if dlqErr := nq.updateStatus(notif.ID, QueueStatusDLQ, errorMsg, false); dlqErr != nil {
log.Error().
Err(dlqErr).
Str("component", "notification_queue").
Str("action", "move_to_dlq").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts).
Int("maxAttempts", notif.MaxAttempts).
Msg("Failed to move notification to DLQ")
} else {
healthChanged = true
completedAt := time.Now()
notif.Status = QueueStatusDLQ
notif.CompletedAt = &completedAt
notif.Links = transitionNotificationLinks(
notif.Links,
operationaltrust.NotificationDeadLetter,
completedAt,
)
deadLetterReason := "max_retries_exhausted"
if !retryable {
deadLetterReason = "failure_class_not_retryable"
}
log.Warn().
Str("component", "notification_queue").
Str("action", "move_to_dlq").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempts", notif.Attempts).
Int("maxAttempts", notif.MaxAttempts).
Str("failureClass", string(failureClass)).
Str("deadLetterReason", deadLetterReason).
Str("error", errorMsg).
Msg("notification moved to DLQ")
}
} else {
// Schedule retry, honoring any bounded server Retry-After hint.
retryAfter, _ := RetryAfterFromError(err)
if retryErr := nq.scheduleRetryWithHint(notif.ID, notif.Attempts, failureClass, retryAfter, false); retryErr != nil {
log.Error().
Err(retryErr).
Str("component", "notification_queue").
Str("action", "schedule_retry").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts).
Int("maxAttempts", notif.MaxAttempts).
Msg("Failed to schedule retry")
} else {
healthChanged = true
notif.Status = QueueStatusPending
notif.Links = transitionNotificationLinks(
notif.Links,
operationaltrust.NotificationRetrying,
time.Now(),
)
log.Warn().
Str("component", "notification_queue").
Str("action", "schedule_retry").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts).
Int("maxAttempts", notif.MaxAttempts).
Str("error", errorMsg).
Msg("notification failed, scheduled for retry")
}
}
}
if persistedLinks, linksErr := nq.getNotificationLinks(notif.ID); linksErr != nil {
log.Error().
Err(linksErr).
Str("component", "notification_queue").
Str("action", "read_links_for_audit").
Str("id", notif.ID).
Msg("Failed to read persisted notification links for audit")
} else {
notif.Links = persistedLinks
}
if success && !alreadyAccepted && notif.Status == QueueStatusSent && !nq.stopping() {
replayed, replayErr := nq.replayRecoverableDeadLettersForDestination(notif)
if replayErr != nil {
log.Error().
Err(replayErr).
Str("component", "notification_queue").
Str("action", "auto_replay_dlq_on_recovery").
Str("id", notif.ID).
Str("type", notif.Type).
Msg("Failed to auto-replay recoverable dead-letter notifications")
} else {
healthChanged = healthChanged || replayed > 0
}
}
if notif.ProviderAcceptedAt != nil {
return
}
var acknowledgementError *notificationAcknowledgementError
auditSuccess, auditCause := success, err
auditNotification := notif
if errors.As(err, &acknowledgementError) {
auditSuccess, auditCause = true, nil
accepted := *notif
accepted.Status = QueueStatusSent
accepted.Links = transitionNotificationLinks(notif.Links, operationaltrust.NotificationDelivered, time.Now())
auditNotification = &accepted
}
if auditErr := nq.RecordAuditError(auditNotification, auditSuccess, auditCause); auditErr != nil {
log.Error().
Err(auditErr).
Str("component", "notification_queue").
Str("action", "record_audit").
Str("id", notif.ID).
Str("type", notif.Type).
Int("attempt", notif.Attempts).
Int("maxAttempts", notif.MaxAttempts).
Msg("Failed to record audit")
if acknowledgementError != nil {
nq.mu.Lock()
_, markErr := nq.db.Exec(`UPDATE notification_queue SET last_error=? WHERE id=? AND status='pending'`, notificationSettlementPendingMessage, notif.ID)
nq.mu.Unlock()
if markErr != nil {
log.Error().Err(markErr).Msg("Failed to report unavailable notification acknowledgement storage")
}
}
} else if acknowledgementError != nil {
at := time.Now()
notif.ProviderAcceptedAt = &at
if err := nq.deferSettlement(notif, false); err != nil {
log.Error().Err(err).Msg("Failed to retain acknowledged notification settlement")
}
}
}
// cleanupOldEntries removes old completed notifications
func (nq *NotificationQueue) cleanupOldEntries() {
defer nq.wg.Done()
for {
select {
case <-nq.stopChan:
return
case <-nq.cleanupTicker.C:
nq.performCleanup()
}
}
}
// performCleanup removes notifications older than retention period
func (nq *NotificationQueue) performCleanup() {
if err := nq.expireRehearsals(); err != nil {
log.Error().Msg("Failed to expire notification rehearsal evidence")
}
nq.cleanupDeliveryReceipts()
nq.mu.Lock()
defer nq.mu.Unlock()
// Keep completed/failed for 7 days, DLQ for 30 days
completedCutoff := time.Now().Add(-7 * 24 * time.Hour).Unix()
dlqCutoff := time.Now().Add(-30 * 24 * time.Hour).Unix()
// Delete audit records for notifications about to be cleaned up (FK constraint)
auditCleanup := `DELETE FROM notification_audit WHERE notification_id IN (
SELECT id FROM notification_queue WHERE status IN ('sent', 'failed', 'cancelled') AND completed_at < ? AND (status<>'failed' OR NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1))
)`
if _, err := nq.db.Exec(auditCleanup, completedCutoff); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cleanup_audit_for_completed").
Msg("Failed to cleanup audit records for completed notifications")
}
// Clean completed/sent/failed/cancelled
query := `DELETE FROM notification_queue WHERE status IN ('sent', 'failed', 'cancelled') AND completed_at < ? AND (status<>'failed' OR NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1))`
result, err := nq.db.Exec(query, completedCutoff)
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cleanup_completed_notifications").
Int64("completedCutoff", completedCutoff).
Msg("Failed to cleanup old notifications")
} else {
if rows, _ := result.RowsAffected(); rows > 0 {
log.Info().
Str("component", "notification_queue").
Str("action", "cleanup_completed_notifications").
Int64("count", rows).
Int64("completedCutoff", completedCutoff).
Msg("Cleaned up old completed notifications")
}
}
// Delete audit records for DLQ notifications about to be cleaned up (FK constraint)
auditCleanup = `DELETE FROM notification_audit WHERE notification_id IN (
SELECT id FROM notification_queue WHERE status = 'dlq' AND completed_at < ? AND NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)
)`
if _, err = nq.db.Exec(auditCleanup, dlqCutoff); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cleanup_audit_for_dlq").
Msg("Failed to cleanup audit records for DLQ notifications")
}
// Clean old DLQ entries
query = `DELETE FROM notification_queue WHERE status = 'dlq' AND completed_at < ? AND NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)`
result, err = nq.db.Exec(query, dlqCutoff)
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cleanup_dlq_entries").
Int64("dlqCutoff", dlqCutoff).
Msg("Failed to cleanup old DLQ entries")
} else {
if rows, _ := result.RowsAffected(); rows > 0 {
log.Info().
Str("component", "notification_queue").
Str("action", "cleanup_dlq_entries").
Int64("count", rows).
Int64("dlqCutoff", dlqCutoff).
Msg("Cleaned up old DLQ entries")
}
}
// Clean old audit logs (keep 30 days)
auditCutoff := time.Now().Add(-30 * 24 * time.Hour).Unix()
query = `DELETE FROM notification_audit WHERE timestamp < ? AND NOT EXISTS (SELECT 1 FROM notification_queue q WHERE q.id=notification_audit.notification_id AND q.status IN ('pending','sending','failed','dlq'))`
result, err = nq.db.Exec(query, auditCutoff)
if err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cleanup_audit_logs").
Int64("auditCutoff", auditCutoff).
Msg("Failed to cleanup old audit logs")
} else {
if rows, _ := result.RowsAffected(); rows > 0 {
log.Debug().
Str("component", "notification_queue").
Str("action", "cleanup_audit_logs").
Int64("count", rows).
Int64("auditCutoff", auditCutoff).
Msg("Cleaned up old audit logs")
}
}
}
// stopping reports whether processing has begun to stop. Queues built
// without a stop channel are never stopping.
func (nq *NotificationQueue) stopping() bool {
select {
case <-nq.stopChan:
return true
default:
return false
}
}
// stopProcessing ends delivery work but keeps storage open. Workers that
// observe the stop leave their rows pending for the next start; a worker
// already past that check finishes its hold, partition or attempt and records
// the outcome before this returns. Stop calls it, and an owner that must
// withdraw delivery permission calls it first so no worker sees that change.
func (nq *NotificationQueue) stopProcessing() {
nq.stopProcessingOnce.Do(func() {
close(nq.stopChan)
log.Info().Str("component", "notification_queue").Msg("Stopping notification queue processing")
nq.wg.Wait()
nq.processorTicker.Stop()
nq.cleanupTicker.Stop()
})
}
// Stop gracefully stops the queue processor
func (nq *NotificationQueue) Stop() error {
nq.stopOnce.Do(func() {
nq.stopProcessing()
if err := nq.db.Close(); err != nil {
nq.stopErr = fmt.Errorf("failed to close database: %w", err)
return
}
log.Info().Msg("Notification queue stopped")
})
return nq.stopErr
}
// calculateBackoff calculates the progressive retry delay for an attempt.
// Early retries stay fast (1s, 2s, 4s) for transient blips and rehearsals,
// while later retries stretch across ~33 minutes so brief outages recover
// without flooding or dead-lettering alerts.
func calculateBackoff(attempt int) time.Duration {
schedule := [...]time.Duration{
1 * time.Second,
2 * time.Second,
4 * time.Second,
30 * time.Second,
2 * time.Minute,
5 * time.Minute,
10 * time.Minute,
15 * time.Minute,
}
if attempt <= 0 {
return schedule[0]
}
if attempt >= len(schedule) {
return schedule[len(schedule)-1]
}
return schedule[attempt]
}
// calculateBackoffForClass enforces at least a 60-second breather from attempt
// 3 onward for rate-limited destinations so persistent 429s cool down cleanly.
func calculateBackoffForClass(attempt int, failureClass NotificationFailureClass) time.Duration {
backoff := calculateBackoff(attempt)
if failureClass == NotificationFailureRateLimited && attempt >= 3 && backoff < 60*time.Second {
return 60 * time.Second
}
return backoff
}
// calculateBackoffForRetry respects a bounded server-provided Retry-After hint
// when it exceeds the queue's own attempt/class backoff.
func calculateBackoffForRetry(attempt int, failureClass NotificationFailureClass, retryAfter time.Duration) time.Duration {
backoff := calculateBackoffForClass(attempt, failureClass)
if retryAfter <= 0 {
return backoff
}
if retryAfter > maxNotificationRetryAfter {
retryAfter = maxNotificationRetryAfter
}
if retryAfter > backoff {
return retryAfter
}
return backoff
}
// CancelByAlertIdentifiers suppresses queued firing notifications for resolved
// alerts while preserving unrelated alerts in the same grouped queue row. It
// returns the number of matched firing-alert entries removed from rows that
// were still waiting for delivery ('pending'). Entries in rows already
// mid-send ('sending') are cancelled best-effort but not counted, because
// their delivery may still complete. Failed/dead-lettered firing entries are
// also suppressed so operator retry cannot resurrect a resolved incident;
// these do not contribute to the pending-only return count.
func (nq *NotificationQueue) CancelByAlertIdentifiers(alertIdentifiers []string) (int, error) {
alertIdentifiers = normalizeAlertIdentifiers(alertIdentifiers)
identifiers := make(map[string]struct{}, len(alertIdentifiers))
for _, id := range alertIdentifiers {
identifiers[id] = struct{}{}
}
return nq.cancelAlertNotifications(alertIdentifiers, func(alert *alerts.Alert) bool {
_, matched := identifiers[alert.ID]
return matched
})
}
// CancelByAlertOccurrence retires only the resolved occurrence's firing work.
// Missing occurrence identity must never widen into ID-wide cancellation.
// The same delivery gates, terminal-row handling and audit retention apply.
func (nq *NotificationQueue) CancelByAlertOccurrence(alertID string, start time.Time) (int, error) {
alertID = strings.TrimSpace(alertID)
if alertID == "" || start.IsZero() {
return 0, nil
}
return nq.cancelAlertNotifications([]string{alertID}, func(alert *alerts.Alert) bool {
return alert.ID == alertID && alert.StartTime.Equal(start)
}, func() error { return nq.markReceiptOccurrenceResolvedLocked(alertID, start, time.Now()) })
}
func (nq *NotificationQueue) cancelAlertNotifications(alertIdentifiers []string, matches func(*alerts.Alert) bool, settle ...func() error) (int, error) {
return nq.cancelOccurrenceNotifications(alertIdentifiers, matches, eventAlert, settle...)
}
func (nq *NotificationQueue) cancelOccurrenceNotifications(alertIdentifiers []string, matches func(*alerts.Alert) bool, event notificationEvent, settle ...func() error) (int, error) {
return nq.cancelOccurrenceNotificationsGuarded(alertIdentifiers, matches, event, nil, settle...)
}
func (nq *NotificationQueue) cancelOccurrenceNotificationsGuarded(alertIdentifiers []string, matches func(*alerts.Alert) bool, event notificationEvent, guard func() (bool, error), settle ...func() error) (count int, err error) {
if len(alertIdentifiers) == 0 {
return 0, nil
}
releaseDeliveryGates := nq.acquireAlertDeliveryGates(alertIdentifiers, true)
healthChanged := false
nq.mu.Lock()
defer func() {
for _, complete := range settle {
err = errors.Join(err, complete())
}
nq.mu.Unlock()
releaseDeliveryGates()
if healthChanged {
nq.notifyDeliveryHealthChanged()
}
}()
if guard != nil {
allowed, guardErr := guard()
if guardErr != nil {
settle = nil
return 0, guardErr
}
if !allowed {
settle = nil
return 0, nil
}
}
query := `
SELECT id, type, status, alerts, operational_links
FROM notification_queue
WHERE status IN ('pending', 'sending', 'failed', 'dlq') AND NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)
`
rows, err := nq.db.Query(query)
if err != nil {
return 0, fmt.Errorf("failed to query notifications for cancellation: %w", err)
}
type queuedAlertCancellation struct {
notificationID string
remaining []*alerts.Alert
remainingLinks []operationaltrust.NotificationLink
}
var toCancelIDs []string
var toRewrite []queuedAlertCancellation
suppressedAlertCount := 0
suppressedPendingAlertCount := 0
for rows.Next() {
var notifID string
var notifType string
var notifStatus string
var alertsJSON []byte
var linksJSON []byte
if err := rows.Scan(
&notifID,
&notifType,
&notifStatus,
&alertsJSON,
&linksJSON,
); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cancel_scan_notification").
Msg("Failed to scan notification for cancellation")
continue
}
_, rowEvent := normalizeQueueType(notifType)
if rowEvent != event || (event == eventAlert && !queueTypeCancelableOnAlertResolution(notifType)) {
continue
}
var queuedAlerts []*alerts.Alert
if err := json.Unmarshal(alertsJSON, &queuedAlerts); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cancel_unmarshal_alerts").
Str("notifID", notifID).
Msg("Failed to unmarshal alerts for cancellation check")
continue
}
var links []operationaltrust.NotificationLink
if err := json.Unmarshal(linksJSON, &links); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cancel_unmarshal_links").
Str("notifID", notifID).
Msg("Failed to unmarshal operational links for cancellation check")
continue
}
remainingAlerts := make([]*alerts.Alert, 0, len(queuedAlerts))
removedLinkKeys := make(map[string]struct{})
matchedAlertCount := 0
for _, alert := range queuedAlerts {
if alert == nil {
remainingAlerts = append(remainingAlerts, alert)
continue
}
if matches(alert) {
matchedAlertCount++
if alert.OperationalRecord != nil && alert.LatestTransition != nil {
removedLinkKeys[alert.OperationalRecord.ID+"\x00"+alert.LatestTransition.ID] = struct{}{}
}
continue
}
remainingAlerts = append(remainingAlerts, alert)
}
if matchedAlertCount == 0 {
continue
}
suppressedAlertCount += matchedAlertCount
if notifStatus == string(QueueStatusPending) {
suppressedPendingAlertCount += matchedAlertCount
}
if len(remainingAlerts) == 0 {
toCancelIDs = append(toCancelIDs, notifID)
continue
}
remainingLinks := make([]operationaltrust.NotificationLink, 0, len(links))
for _, link := range links {
key := link.OperationalRecordID + "\x00" + link.TransitionID
if _, removed := removedLinkKeys[key]; removed {
continue
}
remainingLinks = append(remainingLinks, link)
}
toRewrite = append(toRewrite, queuedAlertCancellation{
notificationID: notifID,
remaining: remainingAlerts,
remainingLinks: remainingLinks,
})
}
rowsErr := rows.Err()
rows.Close() // Release connection before executing updates
if rowsErr != nil {
return 0, fmt.Errorf("error iterating notifications for cancellation: %w", rowsErr)
}
if len(toRewrite) > 0 {
updateAlertsQuery := `
UPDATE notification_queue
SET alerts = ?, operational_links = ?
WHERE id = ?
`
for _, rewrite := range toRewrite {
alertsJSON, err := json.Marshal(rewrite.remaining)
if err != nil {
return 0, fmt.Errorf("failed to marshal remaining alerts for %s: %w", rewrite.notificationID, err)
}
linksJSON, err := json.Marshal(rewrite.remainingLinks)
if err != nil {
return 0, fmt.Errorf("failed to marshal remaining links for %s: %w", rewrite.notificationID, err)
}
if _, err := nq.db.Exec(
updateAlertsQuery,
string(alertsJSON),
string(linksJSON),
rewrite.notificationID,
); err != nil {
return 0, fmt.Errorf("failed to rewrite queued notification %s after alert resolution: %w", rewrite.notificationID, err)
}
}
}
if len(toCancelIDs) > 0 {
reason := "Alert resolved"
if event == eventResolved {
reason = "Alert reopened"
}
for _, notifID := range toCancelIDs {
if err := nq.updateNotificationStatusNoLock(
notifID,
QueueStatusCancelled,
reason,
time.Now(),
); err != nil {
log.Error().
Err(err).
Str("component", "notification_queue").
Str("action", "cancel_mark_notification").
Str("notifID", notifID).
Msg("Failed to mark notification as cancelled")
return 0, fmt.Errorf("cancel resolved notification %s: %w", notifID, err)
}
healthChanged = true
}
}
if suppressedAlertCount > 0 {
log.Info().
Str("component", "notification_queue").
Str("action", "cancel_alert_identifiers").
Int("suppressedAlertCount", suppressedAlertCount).
Int("suppressedPendingAlertCount", suppressedPendingAlertCount).
Int("cancelledRows", len(toCancelIDs)).
Int("rewrittenRows", len(toRewrite)).
Strs("alertIdentifiers", alertIdentifiers).
Msg("suppressed resolved alerts in queued notifications")
}
return suppressedPendingAlertCount, nil
}
func queueTypeCancelableOnAlertResolution(notifType string) bool {
_, event := normalizeQueueType(notifType)
return event != eventResolved
}
// CancelByTypes marks queued notifications of the given types as cancelled.
func (nq *NotificationQueue) CancelByTypes(types []string, reason string) error {
if len(types) == 0 {
return nil
}
if strings.TrimSpace(reason) == "" {
reason = "Notification destination disabled"
}
nq.mu.Lock()
defer nq.mu.Unlock()
placeholders := make([]string, len(types))
args := make([]any, 0, len(types))
for i, notifType := range types {
placeholders[i] = "?"
args = append(args, notifType)
}
query := fmt.Sprintf(`
SELECT id
FROM notification_queue
WHERE status IN ('pending', 'sending') AND NOT EXISTS(SELECT 1 FROM notification_audit a WHERE a.notification_id=notification_queue.id AND a.success=1)
AND type IN (%s)
`, strings.Join(placeholders, ","))
rows, err := nq.db.Query(query, args...)
if err != nil {
return fmt.Errorf("failed to query notifications by type: %w", err)
}
var notificationIDs []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
_ = rows.Close()
return fmt.Errorf("scan notification by type: %w", err)
}
notificationIDs = append(notificationIDs, id)
}
rowsErr := rows.Err()
_ = rows.Close()
if rowsErr != nil {
return fmt.Errorf("iterate notifications by type: %w", rowsErr)
}
for _, id := range notificationIDs {
if err := nq.updateNotificationStatusNoLock(
id,
QueueStatusCancelled,
reason,
time.Now(),
); err != nil {
return fmt.Errorf("cancel notification %s by type: %w", id, err)
}
}
if len(notificationIDs) > 0 {
log.Info().
Str("component", "notification_queue").
Str("action", "cancel_types").
Int("count", len(notificationIDs)).
Strs("types", types).
Str("reason", reason).
Msg("cancelled queued notifications by type")
}
return nil
}