feat(mail): send service identity mail through a leased worker

This commit is contained in:
shira committed 2026-10-06 21:08:11 +03:00
1 parent 204c6bae62
commit d21fd49a42
58 files changed
+7577 -164

No files matched your search

@@ -19,6 +19,32 @@ type Querier interface {
// handler committed the job is done and nothing is canceled; otherwise the
// worker finds its lease gone and does not run or complete the job.
CancelScheduledJob(ctx context.Context, arg CancelScheduledJobParams) (int64, error)
// Ends a claimed delivery whose stored content no longer matches its hash, so
// one broken row never blocks the delivery queue. It matches only a delivery
// that the worker could still send, ends the lease, advances the version, and
// redacts the bodies of a token mail like any other cancellation. now is the
// application clock.
CancelUnreadableDelivery(ctx context.Context, arg CancelUnreadableDeliveryParams) (int64, error)
// Claims the most overdue delivery that the sending worker may send, in one
// statement: a pending or waiting_quota delivery of the service mail kinds
// (the literals match the predicate of deliveries_due_idx, so an idle poll
// reads only that small index) whose next_attempt_at has passed, pinned to the
// configured provider account (or to rebind_provider before its first
// attempt, which the worker moves to the configured account), and held by no
// live lease. A row that another worker locks is skipped without waiting, so
// several processes never take the same delivery, and a delivery whose lease
// expired, because its worker stopped, is claimed again.
// The lease is measured by the database clock, never by the clock of an
// application instance: it lasts lease_seconds from clock_timestamp(), and a
// lease is live while claimed_until is later than clock_timestamp(). Due times
// stay on the application clock that wrote them. Neither the version nor
// updated_at changes: a claim is not a change of the delivery.
// previous_claimed_until is the deadline of an abandoned lease, NULL for a
// delivery nobody held; since_first_send_seconds is how long ago, by the
// database clock, its first request started, -1 when none did. now is the
// application clock. Deliveries that are unknown or wait for an unknown quota
// reset have no due time here and wait for an operator.
ClaimDueDelivery(ctx context.Context, arg ClaimDueDeliveryParams) (ClaimDueDeliveryRow, error)
// Claims up to batch_limit jobs of the listed kinds in one statement: jobs
// whose available_at has passed, and running jobs whose lease expired
// because their worker stopped without finishing them. Rows that another
@@ -156,6 +182,12 @@ type Querier interface {
// the handler transaction that holds its row lock: only that transaction
// can have changed it, for example by canceling its own job.
GetScheduledJobStatus(ctx context.Context, id int64) (string, error)
// Reads the token a delivery is bound to and the account it is addressed to
// in one statement, for the check right before a service mail is sent. It
// takes no lock. It reads the account address only as the comparison key and
// selects no token hash. A missing binding or token row gives NULL token
// columns.
GetServiceMailState(ctx context.Context, id pgtype.UUID) (GetServiceMailStateRow, error)
GetUnrevokedConsentRecordByVersion(ctx context.Context, arg GetUnrevokedConsentRecordByVersionParams) (GetUnrevokedConsentRecordByVersionRow, error)
GetUserByEmailNormalized(ctx context.Context, emailNormalized string) (User, error)
GetUserByID(ctx context.Context, id pgtype.UUID) (User, error)
@@ -253,6 +285,11 @@ type Querier interface {
// published or open the category, until the transaction ends. The key is
// namespaced, so it cannot collide with another use of advisory locks.
LockAssessmentCategory(ctx context.Context, category string) error
// Locks the delivery row of the lease holder until the transaction ends, so
// every path that settles a send takes the delivery lock before the quota
// rows, the same order as the claim. No row means that owner no longer holds
// the lease.
LockClaimedDelivery(ctx context.Context, arg LockClaimedDeliveryParams) (pgtype.UUID, error)
// Locks a claimed job for its handler transaction and checks that the lease
// still belongs to this attempt. It returns no row when the job was
// canceled, or reclaimed after the lease expired. While the lock is held,
@@ -279,6 +316,12 @@ type Querier interface {
// because aggregate writers check the expected version, so scanning without a
// position watermark cannot overtake an earlier version.
LockPendingOutboxEvents(ctx context.Context, batchLimit int32) ([]LockPendingOutboxEventsRow, error)
// Notes that the first request of a claimed delivery is about to start, by the
// database clock, and clears the count of worker failures: the claim and the
// preparation succeeded, so the failures are no longer consecutive. The provider keeps the idempotency key for a window counted
// from this moment, so a later claim can tell whether repeating the request is
// still safe. It never moves.
MarkDeliverySendStarted(ctx context.Context, arg MarkDeliverySendStartedParams) (int64, error)
// Records that the provider refused a send because the quota of a window is
// used up: the usage rises to at least the configured limit, so the local
// gate stops admitting sends until the window resets or an operator raises
@@ -304,11 +347,32 @@ type Querier interface {
// transaction. The update applies only while the stored version still
// equals expected_version; otherwise nothing is written and the statement
// returns 0 rows. Two workers that record the same attempt number therefore
// cannot both succeed. Content columns are not touched; a trigger refuses
// any change to them.
// cannot both succeed. The lease of the sending worker ends with the
// attempt, and so does the count of worker failures. When lease_owner is
// given, the update applies only while that worker still holds the lease.
// A token mail that reaches a terminal status loses its bodies in the same
// statement, so the one-time token does not stay at rest; the trigger allows
// exactly this change of the content columns.
RecordDeliveryAttempt(ctx context.Context, arg RecordDeliveryAttemptParams) (int64, error)
// Records that the sending worker failed on a delivery, so a broken row does
// not stay at the head of the queue: the delivery is due again at
// next_attempt_at and its lease ends. With count_failure the failure is
// counted; after max_failures consecutive failures the delivery is cancelled
// with the reason processing_failed, which an operator sees, and a token mail
// loses its bodies. Without it nothing is counted and nothing is cancelled:
// the failure says nothing about the row (a transient storage error, or a
// stretch of failures across many rows). The row is touched only when no other
// worker holds a live lease on it: lease_owner may hold it, or the lease is
// free or expired. It returns the status after the change; no row means that
// the delivery is no longer pending or waiting, or another worker holds it.
// now is the application clock.
RecordDeliveryFailure(ctx context.Context, arg RecordDeliveryFailureParams) (string, error)
// Records the latest settled outcome of a send under the reservation.
RecordMailQuotaOutcome(ctx context.Context, arg RecordMailQuotaOutcomeParams) (int64, error)
// Gives a claimed delivery back without changing it, so the next pass may
// claim it. It matches only the lease of lease_owner: a delivery that another
// worker took over after the lease expired is left alone.
ReleaseDeliveryClaim(ctx context.Context, arg ReleaseDeliveryClaimParams) (int64, error)
// Marks a reservation released. It matches only an open reservation, so a
// repeated release changes nothing and returns 0 rows.
ReleaseMailQuotaReservation(ctx context.Context, arg ReleaseMailQuotaReservationParams) (int64, error)
@@ -365,10 +429,13 @@ type Querier interface {
// revoked, expired, or idle session is not revived; the update re-checks
// those conditions against a concurrent change.
TouchSession(ctx context.Context, arg TouchSessionParams) (TouchSessionRow, error)
// Stores a change of the delivery state that is not an attempt: a quota wait
// or a new provider account before the first attempt. It applies only while
// the stored version still equals expected_version and returns 0 rows
// otherwise. The trigger refuses a provider change after an attempt.
// Stores a change of the delivery state that is not an attempt: a quota wait,
// a cancellation, or a new provider account before the first attempt. It
// applies only while the stored version still equals expected_version and
// returns 0 rows otherwise. The trigger refuses a provider change after an
// attempt. release_claim ends the lease of the sending worker with the
// change; a new provider account keeps it, because the worker rebinds a
// delivery it holds and then sends it.
UpdateDeliveryState(ctx context.Context, arg UpdateDeliveryStateParams) (int64, error)
// Changes the profile of an organization over the version the caller read
// and advances the version in the same statement. It names no verification