164 lines
5.9 KiB
Go
164 lines
5.9 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 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
|
|
}
|
|
|
|
// Sent records that a channel confirmed a message.
|
|
func (s *Store) Sent(ctx context.Context, seq int64, externalID string) error {
|
|
if _, err := s.db.ExecContext(ctx, `
|
|
UPDATE deliveries.messages SET sent_at = now(), external_id = $2, last_error = NULL WHERE seq = $1`,
|
|
seq, externalID); err != nil {
|
|
return fmt.Errorf("mark message sent: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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; the lease already set when it is
|
|
// tried again.
|
|
func (s *Store) Failed(ctx context.Context, seq int64, reason string) error {
|
|
if _, err := s.db.ExecContext(ctx, `
|
|
UPDATE deliveries.messages SET last_error = $2 WHERE seq = $1`, seq, reason); err != nil {
|
|
return fmt.Errorf("record failed delivery: %w", err)
|
|
}
|
|
return nil
|
|
}
|