Files
pipelines/internal/pipeline/run_notifications.go
T

88 lines
4.2 KiB
Go

package pipeline
import (
"context"
"database/sql"
"errors"
)
// RunNotification is durable publisher evidence, not proof of device reception.
// A sending lease permits restart recovery; a crash after publish but before
// acknowledgement can repeat a message. Attempts are bounded, not guaranteed.
type RunNotification struct {
RunID int64
Sequence int
Event, Channel, Title, Body, Click, Status, Diagnostic, MessageID string
Attempts int
}
func (s *Store) initRunNotifications() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS run_notifications (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, event TEXT NOT NULL, channel TEXT NOT NULL,
title TEXT NOT NULL, body TEXT NOT NULL, click TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending', attempts INTEGER NOT NULL DEFAULT 0,
next_attempt INTEGER NOT NULL DEFAULT 0, diagnostic TEXT NOT NULL DEFAULT '', message_id TEXT NOT NULL DEFAULT '',
PRIMARY KEY(run_id,sequence,event,channel));
CREATE INDEX IF NOT EXISTS run_notifications_due ON run_notifications(status,next_attempt);`)
return err
}
func (s *Store) EnqueueRunNotification(ctx context.Context, n RunNotification) error {
_, err := s.db.ExecContext(ctx, `INSERT OR IGNORE INTO run_notifications
(run_id,sequence,event,channel,title,body,click) VALUES (?,?,?,?,?,?,?)`, n.RunID, n.Sequence, n.Event, n.Channel, n.Title, n.Body, n.Click)
return err
}
// ClaimRunNotification atomically leases one due attempt for 60 seconds. Three
// attempts total bound both transient failures and crash/restart loops.
func (s *Store) ClaimRunNotification(ctx context.Context, now int64) (*RunNotification, error) {
if _, err := s.db.ExecContext(ctx, `UPDATE run_notifications SET status='failed',diagnostic='publisher acknowledgement unknown after final lease expired' WHERE status='sending' AND attempts>=3 AND next_attempt<=?`, now); err != nil {
return nil, err
}
n := new(RunNotification)
err := s.db.QueryRowContext(ctx, `UPDATE run_notifications SET status='sending', attempts=attempts+1, next_attempt=?
WHERE rowid=(SELECT rowid FROM run_notifications WHERE status IN ('pending','sending') AND attempts<3 AND next_attempt<=? ORDER BY next_attempt,rowid LIMIT 1)
RETURNING run_id,sequence,event,channel,title,body,click,status,attempts`, now+60, now).Scan(&n.RunID, &n.Sequence, &n.Event, &n.Channel, &n.Title, &n.Body, &n.Click, &n.Status, &n.Attempts)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return n, err
}
// CoverRunNotification records a successful stage publication covering the same
// run event, without publishing a second final alert.
func (s *Store) CoverRunNotification(ctx context.Context, id int64, sequence int, event string) error {
_, err := s.db.ExecContext(ctx, `INSERT OR IGNORE INTO run_notifications(run_id,sequence,event,channel,title,body,click,status) VALUES (?,?,?,'system','','','','covered')`, id, sequence, event)
return err
}
func (s *Store) FinishRunNotification(ctx context.Context, n RunNotification, now int64, messageID string, failure error) error {
status, diagnostic := "sent", ""
if failure != nil {
status, diagnostic = "pending", failure.Error()
if n.Attempts >= 3 {
status = "failed"
}
}
_, err := s.db.ExecContext(ctx, `UPDATE run_notifications SET status=?,diagnostic=?,message_id=?,next_attempt=?
WHERE run_id=? AND sequence=? AND event=? AND channel=? AND status='sending' AND attempts=?`, status, diagnostic, messageID, now+int64(n.Attempts*5), n.RunID, n.Sequence, n.Event, n.Channel, n.Attempts)
return err
}
func (s *Store) RunNotifications(ctx context.Context, id int64) ([]RunNotification, error) {
rows, err := s.db.QueryContext(ctx, `SELECT run_id,sequence,event,channel,status,attempts,diagnostic,message_id FROM run_notifications WHERE run_id=? ORDER BY sequence,event,channel`, id)
if err != nil {
return nil, err
}
defer rows.Close()
result := []RunNotification{}
for rows.Next() {
var n RunNotification
if err := rows.Scan(&n.RunID, &n.Sequence, &n.Event, &n.Channel, &n.Status, &n.Attempts, &n.Diagnostic, &n.MessageID); err != nil {
return nil, err
}
result = append(result, n)
}
return result, rows.Err()
}