411 lines
16 KiB
Go
411 lines
16 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const TaskDigestRetentionDays = 90
|
|
|
|
type TaskDigestAttempt struct {
|
|
UUID string `json:"uuid"`
|
|
Day string `json:"day"`
|
|
Kind string `json:"kind"`
|
|
State string `json:"state"`
|
|
Reason string `json:"reason"`
|
|
Timezone string `json:"timezone"`
|
|
CreatedMS int64 `json:"created_ms"`
|
|
UpdatedMS int64 `json:"updated_ms"`
|
|
}
|
|
type TaskDigestRetry struct {
|
|
RequestID string `json:"request_id"`
|
|
Day string `json:"day"`
|
|
ExpectedAttempt string `json:"expected_attempt"`
|
|
DuplicateAcknowledged bool `json:"duplicate_acknowledged"`
|
|
}
|
|
type TaskDigestHistory struct {
|
|
Items []TaskDigestAttempt `json:"items"`
|
|
Next string `json:"next"`
|
|
Today string `json:"today"`
|
|
Current *TaskDigestAttempt `json:"current"`
|
|
Retries int `json:"retries"`
|
|
Enabled bool `json:"enabled"`
|
|
RetentionDays int `json:"retention_days"`
|
|
}
|
|
|
|
const digestAttemptColumns = `uuid,day,kind,state,reason,timezone,created_ms,updated_ms`
|
|
|
|
func scanDigestAttempt(row interface{ Scan(...any) error }) (TaskDigestAttempt, error) {
|
|
var v TaskDigestAttempt
|
|
err := row.Scan(&v.UUID, &v.Day, &v.Kind, &v.State, &v.Reason, &v.Timezone, &v.CreatedMS, &v.UpdatedMS)
|
|
return v, err
|
|
}
|
|
|
|
func (s *Store) insertDigestAttempt(ctx context.Context, tx *sql.Tx, c TaskDigestClaim, kind, state string, now time.Time) error {
|
|
_, err := tx.ExecContext(ctx, `INSERT INTO organizer_digest_attempts(uuid,domain_uuid,owner_uuid,day,kind,state,preference_version,timezone,created_ms,updated_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`,`+s.sqlParam(9)+`,`+s.sqlParam(10)+`)`, c.AttemptUUID, c.Domain, c.Owner, c.Day, kind, state, c.Version, c.Timezone, now.UnixMilli(), now.UnixMilli())
|
|
return err
|
|
}
|
|
|
|
// Receipt lookup is read-only. It lets clients confirm an accepted request even
|
|
// when SMTP is subsequently disabled; it never reactivates a delivery.
|
|
func (s *Store) TaskDigestRetryReceipt(ctx context.Context, domain, actor string, input TaskDigestRetry) (result string, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return "", err
|
|
}
|
|
id, err := uuid.Parse(input.RequestID)
|
|
if err != nil || id.String() != input.RequestID {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
var day, expected string
|
|
var ack bool
|
|
err = s.DB.QueryRowContext(ctx, `SELECT day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND owner_uuid=`+s.sqlParam(3), input.RequestID, domain, actor).Scan(&day, &expected, &ack)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if day != input.Day || expected != input.ExpectedAttempt || ack != input.DuplicateAcknowledged {
|
|
return "", ErrTaskConflict
|
|
}
|
|
return input.RequestID, nil
|
|
}
|
|
|
|
// A retry is durable queue admission, never an SMTP send in a browser request.
|
|
// Keep request receipts and daily reservations even after detail retention.
|
|
func (s *Store) QueueTaskDigestRetry(ctx context.Context, domain, actor string, input TaskDigestRetry, now time.Time) (result string, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if s.ReadOnly {
|
|
return "", ErrReadOnly
|
|
}
|
|
id, err := uuid.Parse(input.RequestID)
|
|
if err != nil || id.String() != input.RequestID {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = s.taskProductivityWriter(ctx, tx, domain, actor); err != nil {
|
|
return "", err
|
|
}
|
|
var oldDomain, oldOwner, oldDay, oldExpected string
|
|
var oldAck bool
|
|
err = tx.QueryRowContext(ctx, `SELECT domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=`+s.sqlParam(1), input.RequestID).Scan(&oldDomain, &oldOwner, &oldDay, &oldExpected, &oldAck)
|
|
if err == nil {
|
|
if oldDomain != domain || oldOwner != actor || oldDay != input.Day || oldExpected != input.ExpectedAttempt || oldAck != input.DuplicateAcknowledged {
|
|
return "", ErrTaskConflict
|
|
}
|
|
return input.RequestID, tx.Commit()
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return "", err
|
|
}
|
|
var raw string
|
|
if err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2), domain, actor).Scan(&raw); err != nil {
|
|
return "", err
|
|
}
|
|
var p TaskPreferences
|
|
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
|
return "", err
|
|
}
|
|
loc, err := time.LoadLocation(p.Timezone)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !p.DigestEnabled {
|
|
return "", ErrAccessDenied
|
|
}
|
|
if input.Day != now.In(loc).Format("2006-01-02") {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
var state, current string
|
|
if err = tx.QueryRowContext(ctx, `SELECT state,attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND day=`+s.sqlParam(3), domain, actor, input.Day).Scan(&state, ¤t); err != nil {
|
|
return "", err
|
|
}
|
|
if current != input.ExpectedAttempt || state == "queued" || state == "sending" {
|
|
return "", ErrTaskConflict
|
|
}
|
|
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
|
return "", ErrTaskConflict
|
|
}
|
|
if (state == "sent" || state == "uncertain") && !input.DuplicateAcknowledged {
|
|
return "", ErrTaskInvalid
|
|
}
|
|
var count int
|
|
if err = tx.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND day=`+s.sqlParam(3), domain, actor, input.Day).Scan(&count); err != nil {
|
|
return "", err
|
|
}
|
|
if count >= 5 {
|
|
return "", ErrTaskConflict
|
|
}
|
|
ack := 0
|
|
if input.DuplicateAcknowledged {
|
|
ack = 1
|
|
}
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_digest_requests(uuid,domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack,created_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`)`, input.RequestID, domain, actor, input.Day, input.ExpectedAttempt, ack, now.UnixMilli())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
c := TaskDigestClaim{TaskDigestDue: TaskDigestDue{domain, actor}, Day: input.Day, Version: p.Version, Timezone: p.Timezone, AttemptUUID: input.RequestID}
|
|
if err = s.insertDigestAttempt(ctx, tx, c, "manual", "queued", now); err != nil {
|
|
return "", err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='queued',attempt_uuid=`+s.sqlParam(1)+`,updated_ms=`+s.sqlParam(2)+` WHERE domain_uuid=`+s.sqlParam(3)+` AND owner_uuid=`+s.sqlParam(4)+` AND day=`+s.sqlParam(5), c.AttemptUUID, now.UnixMilli(), domain, actor, input.Day); err != nil {
|
|
return "", err
|
|
}
|
|
return c.AttemptUUID, tx.Commit()
|
|
}
|
|
|
|
func (s *Store) QueuedTaskDigests(ctx context.Context) (result []TaskDigestClaim, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT uuid,domain_uuid,owner_uuid,day,preference_version,timezone FROM organizer_digest_attempts WHERE state='queued' ORDER BY updated_ms,uuid LIMIT 10`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []TaskDigestClaim{}
|
|
for rows.Next() {
|
|
var c TaskDigestClaim
|
|
if err = rows.Scan(&c.AttemptUUID, &c.Domain, &c.Owner, &c.Day, &c.Version, &c.Timezone); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) ClaimQueuedTaskDigest(ctx context.Context, c TaskDigestClaim, now time.Time) (result TaskDigestClaim, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if s.ReadOnly {
|
|
return c, ErrReadOnly
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
|
return c, err
|
|
}
|
|
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='sending',updated_ms=`+s.sqlParam(1)+` WHERE uuid=`+s.sqlParam(2)+` AND domain_uuid=`+s.sqlParam(3)+` AND owner_uuid=`+s.sqlParam(4)+` AND state='queued' AND EXISTS(SELECT 1 FROM organizer_digest_deliveries d WHERE d.domain_uuid=`+s.sqlParam(5)+` AND d.owner_uuid=`+s.sqlParam(6)+` AND d.day=`+s.sqlParam(7)+` AND d.attempt_uuid=`+s.sqlParam(8)+` AND d.state='queued')`, now.UnixMilli(), c.AttemptUUID, c.Domain, c.Owner, c.Domain, c.Owner, c.Day, c.AttemptUUID)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
if n != 1 {
|
|
return c, sql.ErrNoRows
|
|
}
|
|
// Reload all claim fields from the queue, not from an earlier enumeration.
|
|
if err = tx.QueryRowContext(ctx, `SELECT day,preference_version,timezone FROM organizer_digest_attempts WHERE uuid=`+s.sqlParam(1), c.AttemptUUID).Scan(&c.Day, &c.Version, &c.Timezone); err != nil {
|
|
return c, err
|
|
}
|
|
state, reason := "sending", ""
|
|
allowed := s.taskActor(ctx, tx, c.Domain, c.Owner, true)
|
|
if allowed != nil && !errors.Is(allowed, ErrAccessDenied) {
|
|
return c, allowed
|
|
}
|
|
on, err := s.taskReminderExtensionEnabled(ctx, tx, c.Domain)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
var raw string
|
|
err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2), c.Domain, c.Owner).Scan(&raw)
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return c, err
|
|
}
|
|
var p TaskPreferences
|
|
if err == nil {
|
|
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
|
return c, err
|
|
}
|
|
}
|
|
if allowed != nil || !on || !p.DigestEnabled || p.Version != c.Version {
|
|
state, reason = "cancelled", "consent_changed"
|
|
}
|
|
loc, e := time.LoadLocation(c.Timezone)
|
|
if e != nil || now.In(loc).Format("2006-01-02") != c.Day {
|
|
state, reason = "cancelled", "day_expired"
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=`+s.sqlParam(1)+`,reason=`+s.sqlParam(2)+` WHERE uuid=`+s.sqlParam(3), state, reason, c.AttemptUUID); err != nil {
|
|
return c, err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=`+s.sqlParam(1)+`,updated_ms=`+s.sqlParam(2)+` WHERE domain_uuid=`+s.sqlParam(3)+` AND owner_uuid=`+s.sqlParam(4)+` AND day=`+s.sqlParam(5)+` AND attempt_uuid=`+s.sqlParam(6), state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID); err != nil {
|
|
return c, err
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return c, err
|
|
}
|
|
if state != "sending" {
|
|
return c, sql.ErrNoRows
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (s *Store) FinishTaskDigestReason(ctx context.Context, c TaskDigestClaim, state, reason string, now time.Time) (resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
|
return ErrTaskInvalid
|
|
}
|
|
switch reason {
|
|
case "", "smtp_unavailable", "tasks_unavailable", "consent_changed", "transport_uncertain", "extension_disabled":
|
|
default:
|
|
return ErrTaskInvalid
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
|
return err
|
|
}
|
|
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=`+s.sqlParam(1)+`,updated_ms=`+s.sqlParam(2)+` WHERE domain_uuid=`+s.sqlParam(3)+` AND owner_uuid=`+s.sqlParam(4)+` AND day=`+s.sqlParam(5)+` AND attempt_uuid=`+s.sqlParam(6)+` AND state='sending'`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != 1 {
|
|
return ErrTaskConflict
|
|
}
|
|
res, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=`+s.sqlParam(1)+`,reason=`+s.sqlParam(2)+`,updated_ms=`+s.sqlParam(3)+` WHERE uuid=`+s.sqlParam(4)+` AND state='sending'`, state, reason, now.UnixMilli(), c.AttemptUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err = res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != 1 {
|
|
return ErrTaskConflict
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (s *Store) TaskDigestHistory(ctx context.Context, domain, actor, after string, limit int, now time.Time) (result TaskDigestHistory, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
out := TaskDigestHistory{Items: []TaskDigestAttempt{}, RetentionDays: TaskDigestRetentionDays}
|
|
p, err := s.TaskPreferences(ctx, domain, actor)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
loc, err := time.LoadLocation(p.Timezone)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.Today = now.In(loc).Format("2006-01-02")
|
|
out.Enabled = p.DigestEnabled
|
|
if limit < 1 || limit > 100 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
query := `SELECT ` + digestAttemptColumns + ` FROM organizer_digest_attempts WHERE domain_uuid=` + s.sqlParam(1) + ` AND owner_uuid=` + s.sqlParam(2)
|
|
args := []any{domain, actor}
|
|
if after != "" {
|
|
if len(after) > 256 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
var cursor struct {
|
|
MS int64
|
|
ID string
|
|
}
|
|
raw, e := base64.RawURLEncoding.DecodeString(after)
|
|
if e != nil || json.Unmarshal(raw, &cursor) != nil || cursor.MS < 0 || len(cursor.ID) > 80 || cursor.ID == "" {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
query += ` AND (created_ms,uuid)<(` + s.sqlParam(len(args)+1) + `,` + s.sqlParam(len(args)+2) + `)`
|
|
args = append(args, cursor.MS, cursor.ID)
|
|
}
|
|
query += ` ORDER BY created_ms DESC,uuid DESC LIMIT ` + s.sqlParam(len(args)+1)
|
|
args = append(args, limit+1)
|
|
rows, err := s.DB.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
for rows.Next() {
|
|
v, e := scanDigestAttempt(rows)
|
|
if e != nil {
|
|
rows.Close()
|
|
return out, e
|
|
}
|
|
out.Items = append(out.Items, v)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
if len(out.Items) > limit {
|
|
out.Items = out.Items[:limit]
|
|
last := out.Items[limit-1]
|
|
raw, _ := json.Marshal(struct {
|
|
MS int64
|
|
ID string
|
|
}{last.CreatedMS, last.UUID})
|
|
out.Next = base64.RawURLEncoding.EncodeToString(raw)
|
|
}
|
|
current, e := scanDigestAttempt(s.DB.QueryRowContext(ctx, `SELECT `+digestAttemptColumns+` FROM organizer_digest_attempts WHERE uuid=(SELECT attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND day=`+s.sqlParam(3)+`)`, domain, actor, out.Today))
|
|
if e == nil {
|
|
out.Current = ¤t
|
|
} else if !errors.Is(e, sql.ErrNoRows) {
|
|
return out, e
|
|
}
|
|
err = s.DB.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND day=`+s.sqlParam(3), domain, actor, out.Today).Scan(&out.Retries)
|
|
return out, err
|
|
}
|
|
|
|
// Only detailed terminal attempts expire. Reservations and request receipts are
|
|
// permanent deduplication state; retention must never delete them.
|
|
func (s *Store) MaintainTaskDigests(ctx context.Context, now time.Time) (result int64, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("task digest", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
if s.ReadOnly {
|
|
return 0, ErrReadOnly
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
|
return 0, err
|
|
}
|
|
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='uncertain',reason='worker_interrupted',updated_ms=`+s.sqlParam(1)+` WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='sending' AND updated_ms<`+s.sqlParam(2)+` ORDER BY updated_ms,uuid LIMIT 100)`, now.UnixMilli(), now.Add(-10*time.Minute).UnixMilli())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='uncertain',updated_ms=`+s.sqlParam(1)+` WHERE state='sending' AND attempt_uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='uncertain' AND reason='worker_interrupted' AND updated_ms=`+s.sqlParam(2)+`)`, now.UnixMilli(), now.UnixMilli())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
res, err := tx.ExecContext(ctx, `DELETE FROM organizer_digest_attempts WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state IN ('sent','uncertain','cancelled') AND updated_ms<`+s.sqlParam(1)+` ORDER BY updated_ms,uuid LIMIT 500)`, now.Add(-TaskDigestRetentionDays*24*time.Hour).UnixMilli())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return n, tx.Commit()
|
|
}
|