feat: add audited work records and controlled digest retries
Build and publish container images / images (push) Successful in 12m27s
Build and publish container images / images (push) Successful in 12m27s
This commit is contained in:
1 parent
6e0aecdfb3
commit
e65ca56f92
33 files changed
+2262
-114
No files matched your search
@@ -6,6 +6,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const TaskDigestEventType = "task.digest.deliver"
|
||||
@@ -16,9 +18,10 @@ type TaskDigestDue struct {
|
||||
}
|
||||
type TaskDigestClaim struct {
|
||||
TaskDigestDue
|
||||
Day string
|
||||
Version int64
|
||||
Timezone string
|
||||
Day string
|
||||
Version int64
|
||||
Timezone string
|
||||
AttemptUUID string
|
||||
}
|
||||
|
||||
func (s *Store) EnsureTaskDigestEvent() error {
|
||||
@@ -124,11 +127,17 @@ func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time
|
||||
}
|
||||
return out, sql.ErrNoRows
|
||||
}
|
||||
res, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_digest_deliveries VALUES(?,?,?,'sending',?)`, due.Domain, due.Owner, out.Day, now.UnixMilli())
|
||||
out.AttemptUUID = uuid.NewString()
|
||||
res, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_digest_deliveries(domain_uuid,owner_uuid,day,state,updated_ms,attempt_uuid) VALUES(?,?,?,'sending',?,?)`, due.Domain, due.Owner, out.Day, now.UnixMilli(), out.AttemptUUID)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
n, _ = res.RowsAffected()
|
||||
if n == 1 {
|
||||
if err = insertDigestAttempt(ctx, tx, out, "scheduled", "sending", now); err != nil {
|
||||
return out, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return out, err
|
||||
}
|
||||
@@ -139,12 +148,20 @@ func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time
|
||||
}
|
||||
|
||||
func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (string, error) {
|
||||
if err := taskActor(ctx, s.DB, c.Domain, c.Owner, true); err != nil {
|
||||
if s.ReadOnly {
|
||||
return "", ErrReadOnly
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := taskActor(ctx, tx, c.Domain, c.Owner, true); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var raw, email string
|
||||
var version int64
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT f.data,f.version,p.email FROM organizer_task_preferences f JOIN principals p ON p.uuid=f.owner_uuid WHERE f.domain_uuid=? AND f.owner_uuid=?`, c.Domain, c.Owner).Scan(&raw, &version, &email)
|
||||
err = tx.QueryRowContext(ctx, `SELECT f.data,f.version,p.email FROM organizer_task_preferences f JOIN principals p ON p.uuid=f.owner_uuid JOIN organizer_digest_deliveries d ON d.domain_uuid=f.domain_uuid AND d.owner_uuid=f.owner_uuid JOIN organizer_digest_attempts a ON a.uuid=d.attempt_uuid WHERE f.domain_uuid=? AND f.owner_uuid=? AND d.day=? AND d.attempt_uuid=? AND d.state='sending' AND a.state='sending' AND a.updated_ms>?`, c.Domain, c.Owner, c.Day, c.AttemptUUID, time.Now().Add(-2*time.Minute).UnixMilli()).Scan(&raw, &version, &email)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -152,15 +169,22 @@ func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (str
|
||||
if json.Unmarshal([]byte(raw), &p) != nil || !p.DigestEnabled || version != c.Version {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
return email, nil
|
||||
loc, err := time.LoadLocation(c.Timezone)
|
||||
if err != nil || time.Now().In(loc).Format("2006-01-02") != c.Day {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
on, err := taskReminderExtensionEnabled(ctx, tx, c.Domain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !on {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
return email, tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) FinishTaskDigest(ctx context.Context, c TaskDigestClaim, state string) error {
|
||||
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
_, err := s.DB.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND state='sending'`, state, time.Now().UnixMilli(), c.Domain, c.Owner, c.Day)
|
||||
return err
|
||||
return s.FinishTaskDigestReason(ctx, c, state, "", time.Now())
|
||||
}
|
||||
|
||||
func (s *Store) LastTaskDigest(ctx context.Context, domain, actor string) (map[string]any, error) {
|
||||
|
||||
Reference in new issue
Block a user