229 lines
8.7 KiB
Go
229 lines
8.7 KiB
Go
// Package store holds the deliveries module's SQL: the messages each channel
|
|
// carries.
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// Message is one notification or approval request on one channel.
|
|
type Message struct {
|
|
Seq int64 `db:"seq"`
|
|
ID uuid.UUID `db:"id"`
|
|
OrgID string `db:"org_id"`
|
|
Channel string `db:"channel"`
|
|
Kind string `db:"kind"`
|
|
RunID uuid.NullUUID `db:"run_id"`
|
|
Title string `db:"title"`
|
|
Body string `db:"body"`
|
|
Key string `db:"key"`
|
|
// RequestID is the approval request a person answers, on a message that
|
|
// carries one.
|
|
RequestID uuid.NullUUID `db:"request_id"`
|
|
ExpiresAt sql.NullTime `db:"expires_at"`
|
|
CreatedAt time.Time `db:"created_at"`
|
|
// Sent is set on a message its channel has confirmed; the composer's are
|
|
// sent when they are stored.
|
|
Sent bool `db:"-"`
|
|
Attempts int `db:"attempts"`
|
|
}
|
|
|
|
// Store is the deliveries module's Postgres store.
|
|
type Store struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
// New returns a store on db.
|
|
func New(db *sqlx.DB) *Store { return &Store{db: db} }
|
|
|
|
// Insert stores m unless a message with its key exists on its channel, and
|
|
// returns the stored message's position either way.
|
|
func (s *Store) Insert(ctx context.Context, m Message) (int64, bool, error) {
|
|
var seq int64
|
|
err := s.db.GetContext(ctx, &seq, `
|
|
INSERT INTO deliveries.messages (id, org_id, channel, kind, run_id, title, body, key, request_id, expires_at, sent_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, CASE WHEN $11 THEN now() END)
|
|
ON CONFLICT (org_id, channel, key) DO NOTHING
|
|
RETURNING seq`, m.ID, m.OrgID, m.Channel, m.Kind, m.RunID, m.Title, m.Body, m.Key, m.RequestID, m.ExpiresAt, m.Sent)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
err = s.db.GetContext(ctx, &seq, `
|
|
SELECT seq FROM deliveries.messages WHERE org_id = $1 AND channel = $2 AND key = $3`,
|
|
m.OrgID, m.Channel, m.Key)
|
|
if err != nil {
|
|
return 0, false, fmt.Errorf("read existing message: %w", err)
|
|
}
|
|
return seq, false, nil
|
|
}
|
|
if err != nil {
|
|
return 0, false, fmt.Errorf("insert message: %w", err)
|
|
}
|
|
return seq, true, nil
|
|
}
|
|
|
|
// After returns a channel's messages after seq, oldest first.
|
|
func (s *Store) After(ctx context.Context, orgID, channel string, seq int64, limit int) ([]Message, error) {
|
|
var out []Message
|
|
if err := s.db.SelectContext(ctx, &out, `
|
|
SELECT seq, id, org_id, channel, kind, run_id, title, body, key, request_id, expires_at, created_at
|
|
FROM deliveries.messages WHERE org_id = $1 AND channel = $2 AND seq > $3
|
|
ORDER BY seq LIMIT $4`, orgID, channel, seq, limit); err != nil {
|
|
return nil, fmt.Errorf("list messages: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Latest returns a channel's newest messages, oldest first.
|
|
func (s *Store) Latest(ctx context.Context, orgID, channel string, limit int) ([]Message, error) {
|
|
var out []Message
|
|
if err := s.db.SelectContext(ctx, &out, `
|
|
SELECT * FROM (
|
|
SELECT seq, id, org_id, channel, kind, run_id, title, body, key, request_id, expires_at, created_at
|
|
FROM deliveries.messages WHERE org_id = $1 AND channel = $2
|
|
ORDER BY seq DESC LIMIT $3) latest
|
|
ORDER BY seq`, orgID, channel, limit); err != nil {
|
|
return nil, fmt.Errorf("list messages: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Claim takes the oldest message a channel hasn't confirmed whose next attempt
|
|
// is due, and pushes that attempt out by backoff first: the push is the lease,
|
|
// so a crash mid-delivery retries after it rather than never.
|
|
func (s *Store) Claim(ctx context.Context, backoff func(attempts int) time.Duration) (Message, bool, error) {
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
if err != nil {
|
|
return Message{}, false, err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
var m Message
|
|
err = tx.GetContext(ctx, &m, `
|
|
SELECT seq, id, org_id, channel, kind, run_id, title, body, key, request_id, expires_at, created_at, attempts
|
|
FROM deliveries.messages
|
|
WHERE sent_at IS NULL AND failed_at IS NULL AND next_attempt_at <= now()
|
|
ORDER BY seq LIMIT 1 FOR UPDATE SKIP LOCKED`)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Message{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return Message{}, false, fmt.Errorf("claim message: %w", err)
|
|
}
|
|
m.Attempts++
|
|
if _, err := tx.ExecContext(ctx, `
|
|
UPDATE deliveries.messages SET attempts = $2, next_attempt_at = now() + make_interval(secs => $3) WHERE seq = $1`,
|
|
m.Seq, m.Attempts, backoff(m.Attempts).Seconds()); err != nil {
|
|
return Message{}, false, fmt.Errorf("lease message: %w", err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return Message{}, false, fmt.Errorf("lease message: %w", err)
|
|
}
|
|
return m, true, nil
|
|
}
|
|
|
|
// Attempt is one delivery attempt as the dashboard shows it.
|
|
type Attempt struct {
|
|
// Key is the message key the attempt's channel row shares with the
|
|
// composer message.
|
|
Key string `db:"key"`
|
|
Channel string `db:"channel"`
|
|
MessageSeq int64 `db:"message_seq"`
|
|
Attempt int `db:"attempt"`
|
|
At time.Time `db:"at"`
|
|
OK bool `db:"ok"`
|
|
Response string `db:"response"`
|
|
Error string `db:"error"`
|
|
DurationMS int64 `db:"duration_ms"`
|
|
}
|
|
|
|
// Sent records that a channel confirmed a message, with the attempt that
|
|
// confirmed it.
|
|
func (s *Store) Sent(ctx context.Context, seq int64, attempt int, externalID string, took time.Duration) error {
|
|
return s.attempt(ctx, seq, attempt, attemptOutcome{ok: true, response: externalID, took: took},
|
|
`UPDATE deliveries.messages SET sent_at = now(), external_id = $2, last_error = NULL WHERE seq = $1`, externalID)
|
|
}
|
|
|
|
// SentBy finds a message its channel confirmed under one of ids. The run is
|
|
// the one that sent it, nil when the message belonged to none.
|
|
func (s *Store) SentBy(ctx context.Context, orgID, channel string, ids []string) (uuid.NullUUID, bool, error) {
|
|
var run uuid.NullUUID
|
|
err := s.db.GetContext(ctx, &run, `
|
|
SELECT run_id FROM deliveries.messages
|
|
WHERE org_id = $1 AND channel = $2 AND external_id = ANY($3) LIMIT 1`, orgID, channel, ids)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return uuid.NullUUID{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return uuid.NullUUID{}, false, fmt.Errorf("find sent message: %w", err)
|
|
}
|
|
return run, true, nil
|
|
}
|
|
|
|
// Failed records why an attempt failed, with the attempt that failed; the
|
|
// lease already set when it is tried again.
|
|
func (s *Store) Failed(ctx context.Context, seq int64, attempt int, reason string, took time.Duration) error {
|
|
return s.attempt(ctx, seq, attempt, attemptOutcome{failure: reason, took: took},
|
|
`UPDATE deliveries.messages SET last_error = $2 WHERE seq = $1`, reason)
|
|
}
|
|
|
|
// GaveUp records the final attempt of a delivery and marks the message failed,
|
|
// so no worker claims it again.
|
|
func (s *Store) GaveUp(ctx context.Context, seq int64, attempt int, reason string, took time.Duration) error {
|
|
return s.attempt(ctx, seq, attempt, attemptOutcome{failure: reason, took: took},
|
|
`UPDATE deliveries.messages SET failed_at = now(), last_error = $2 WHERE seq = $1`, reason)
|
|
}
|
|
|
|
// attemptOutcome is one attempt's result.
|
|
type attemptOutcome struct {
|
|
ok bool
|
|
response string
|
|
failure string
|
|
took time.Duration
|
|
}
|
|
|
|
// attempt marks the message row and stores the attempt in one transaction, so
|
|
// a retried message never loses the reason it was retried.
|
|
func (s *Store) attempt(ctx context.Context, seq int64, attempt int, outcome attemptOutcome, mark, value string) error {
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
if _, err := tx.ExecContext(ctx, mark, seq, value); err != nil {
|
|
return fmt.Errorf("record delivery: %w", err)
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `
|
|
INSERT INTO deliveries.attempts (message_seq, attempt, ok, response, error, duration_ms)
|
|
VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
seq, attempt, outcome.ok, outcome.response, outcome.failure, outcome.took.Milliseconds()); err != nil {
|
|
return fmt.Errorf("record delivery attempt: %w", err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return fmt.Errorf("record delivery attempt: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AttemptsFor returns the attempts of every connector delivery carrying one of
|
|
// keys. Composer rows have none: they are stored already sent.
|
|
func (s *Store) AttemptsFor(ctx context.Context, orgID string, keys []string) ([]Attempt, error) {
|
|
if len(keys) == 0 {
|
|
return nil, nil
|
|
}
|
|
var out []Attempt
|
|
if err := s.db.SelectContext(ctx, &out, `
|
|
SELECT m.key, m.channel, a.message_seq, a.attempt, a.at, a.ok, a.response, a.error, a.duration_ms
|
|
FROM deliveries.attempts a
|
|
JOIN deliveries.messages m ON m.seq = a.message_seq
|
|
WHERE m.org_id = $1 AND m.key = ANY($2) AND m.channel <> 'composer'
|
|
ORDER BY m.key, m.channel, a.attempt`, orgID, keys); err != nil {
|
|
return nil, fmt.Errorf("list delivery attempts: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|