feat(notification): add durable mail delivery state
This commit is contained in:
1 parent
391c8a6cec
commit
2db585fbff
41 files changed
+4903
-108
No files matched your search
@@ -0,0 +1,203 @@
|
||||
-- Stores a new delivery. A delivery with the same ID already exists when the
|
||||
-- same request is handled again; the statement then inserts nothing and
|
||||
-- returns 0 rows, and the stored content stays as it was. While another
|
||||
-- transaction holds an uncommitted row with the same ID, the insert waits
|
||||
-- for it.
|
||||
-- name: InsertDelivery :execrows
|
||||
INSERT INTO deliveries (
|
||||
id,
|
||||
source_event_id,
|
||||
recipient_user_id,
|
||||
recipient_email,
|
||||
kind,
|
||||
template_version,
|
||||
subject,
|
||||
text_body,
|
||||
html_body,
|
||||
content_sha256,
|
||||
provider,
|
||||
provider_account,
|
||||
idempotency_key,
|
||||
status,
|
||||
attempt_count,
|
||||
next_attempt_at,
|
||||
last_attempt_at,
|
||||
provider_message_id,
|
||||
version,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.narg(source_event_id),
|
||||
sqlc.arg(recipient_user_id),
|
||||
sqlc.arg(recipient_email),
|
||||
sqlc.arg(kind),
|
||||
sqlc.arg(template_version),
|
||||
sqlc.arg(subject),
|
||||
sqlc.arg(text_body),
|
||||
sqlc.arg(html_body),
|
||||
sqlc.arg(content_sha256),
|
||||
sqlc.arg(provider),
|
||||
sqlc.arg(provider_account),
|
||||
sqlc.arg(idempotency_key),
|
||||
sqlc.arg(status),
|
||||
sqlc.arg(attempt_count),
|
||||
sqlc.narg(next_attempt_at),
|
||||
sqlc.narg(last_attempt_at),
|
||||
sqlc.narg(provider_message_id),
|
||||
sqlc.arg(version),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(updated_at)
|
||||
)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
-- name: GetDeliveryByID :one
|
||||
SELECT
|
||||
id,
|
||||
source_event_id,
|
||||
recipient_user_id,
|
||||
recipient_email,
|
||||
kind,
|
||||
template_version,
|
||||
subject,
|
||||
text_body,
|
||||
html_body,
|
||||
content_sha256,
|
||||
provider,
|
||||
provider_account,
|
||||
idempotency_key,
|
||||
status,
|
||||
attempt_count,
|
||||
next_attempt_at,
|
||||
last_attempt_at,
|
||||
provider_message_id,
|
||||
version,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM deliveries
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- Advances a delivery past one attempt and appends the attempt in one
|
||||
-- statement, so both happen or neither does, inside or outside a
|
||||
-- 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.
|
||||
-- name: RecordDeliveryAttempt :execrows
|
||||
WITH advanced AS (
|
||||
UPDATE deliveries
|
||||
SET
|
||||
status = sqlc.arg(status),
|
||||
attempt_count = sqlc.arg(attempt_number),
|
||||
next_attempt_at = sqlc.narg(next_attempt_at),
|
||||
last_attempt_at = sqlc.arg(attempted_at),
|
||||
provider_message_id = coalesce(
|
||||
sqlc.narg(provider_message_id), deliveries.provider_message_id
|
||||
),
|
||||
version = deliveries.version + 1,
|
||||
updated_at = sqlc.arg(updated_at)
|
||||
WHERE
|
||||
deliveries.id = sqlc.arg(delivery_id)
|
||||
AND deliveries.version = sqlc.arg(expected_version)
|
||||
RETURNING deliveries.id
|
||||
)
|
||||
|
||||
INSERT INTO delivery_attempts (
|
||||
delivery_id,
|
||||
attempt_number,
|
||||
attempted_at,
|
||||
outcome,
|
||||
rejection,
|
||||
provider_code,
|
||||
provider_message_id,
|
||||
retry_after_ns,
|
||||
status_after,
|
||||
next_attempt_at
|
||||
)
|
||||
SELECT
|
||||
advanced.id,
|
||||
sqlc.arg(attempt_number),
|
||||
sqlc.arg(attempted_at),
|
||||
sqlc.arg(outcome),
|
||||
sqlc.narg(rejection),
|
||||
sqlc.narg(provider_code),
|
||||
sqlc.narg(provider_message_id),
|
||||
sqlc.arg(retry_after_ns),
|
||||
sqlc.arg(status),
|
||||
sqlc.narg(next_attempt_at)
|
||||
FROM advanced;
|
||||
|
||||
-- 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.
|
||||
-- name: UpdateDeliveryState :execrows
|
||||
UPDATE deliveries
|
||||
SET
|
||||
status = sqlc.arg(status),
|
||||
next_attempt_at = sqlc.narg(next_attempt_at),
|
||||
provider = sqlc.arg(provider),
|
||||
provider_account = sqlc.arg(provider_account),
|
||||
version = deliveries.version + 1,
|
||||
updated_at = sqlc.arg(updated_at)
|
||||
WHERE
|
||||
deliveries.id = sqlc.arg(id)
|
||||
AND deliveries.version = sqlc.arg(expected_version);
|
||||
|
||||
-- name: ListDeliveryAttempts :many
|
||||
SELECT
|
||||
delivery_id,
|
||||
attempt_number,
|
||||
attempted_at,
|
||||
outcome,
|
||||
rejection,
|
||||
provider_code,
|
||||
provider_message_id,
|
||||
retry_after_ns,
|
||||
status_after,
|
||||
next_attempt_at
|
||||
FROM delivery_attempts
|
||||
WHERE delivery_id = sqlc.arg(delivery_id)
|
||||
ORDER BY attempt_number;
|
||||
|
||||
-- name: InsertMailRequest :exec
|
||||
INSERT INTO mail_requests (
|
||||
id,
|
||||
recipient_user_id,
|
||||
kind,
|
||||
subject,
|
||||
body,
|
||||
link_path,
|
||||
created_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(recipient_user_id),
|
||||
sqlc.arg(kind),
|
||||
sqlc.arg(subject),
|
||||
sqlc.arg(body),
|
||||
sqlc.arg(link_path),
|
||||
sqlc.arg(created_at)
|
||||
);
|
||||
|
||||
-- Locks a request for the consumer transaction that turns it into a
|
||||
-- delivery. A concurrent consumer of the same request waits here and then
|
||||
-- finds the row deleted. The account cleanup and the cascade from users take
|
||||
-- the same row lock, so a request is never converted for an account whose
|
||||
-- deletion is in progress without one of the two transactions waiting.
|
||||
-- name: LockMailRequest :one
|
||||
SELECT
|
||||
id,
|
||||
recipient_user_id,
|
||||
kind,
|
||||
subject,
|
||||
body,
|
||||
link_path,
|
||||
created_at
|
||||
FROM mail_requests
|
||||
WHERE id = sqlc.arg(id)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: DeleteMailRequest :execrows
|
||||
DELETE FROM mail_requests
|
||||
WHERE id = sqlc.arg(id);
|
||||
@@ -121,11 +121,14 @@ RETURNING
|
||||
updated_at;
|
||||
|
||||
-- Deletes one batch of accounts that stayed unverified since before cutoff,
|
||||
-- oldest first, with their tokens and sessions. Every lock is taken with SKIP
|
||||
-- LOCKED, so the statement never waits: an account whose row is locked, for
|
||||
-- example by a re-registration or a token insert, is skipped, and so is an
|
||||
-- account with any token or session row locked, for example by a
|
||||
-- confirmation that consumed a token. Skipped accounts stay for the next run.
|
||||
-- oldest first, with their tokens, sessions, mail requests, and mail
|
||||
-- deliveries (and through the deliveries their attempts). Every lock is taken
|
||||
-- with SKIP LOCKED, so the statement never waits: an account whose row is
|
||||
-- locked, for example by a re-registration, a token insert, or a delivery
|
||||
-- insert, is skipped, and so is an account with any token, session, mail
|
||||
-- request, or delivery row locked, for example by a confirmation that
|
||||
-- consumed a token, the delivery consumer, or a worker recording an attempt.
|
||||
-- Skipped accounts stay for the next run.
|
||||
-- Taking the account lock before the token locks is the reverse of the
|
||||
-- documented order, which is safe only because no lock here waits.
|
||||
-- name: DeleteUnverifiedUsersRegisteredBefore :execrows
|
||||
@@ -154,8 +157,22 @@ locked_session AS (
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
-- An account qualifies only when every token and session row visible to the
|
||||
-- statement was locked above.
|
||||
locked_mail_request AS (
|
||||
SELECT request.recipient_user_id
|
||||
FROM mail_requests AS request
|
||||
WHERE request.recipient_user_id IN (SELECT candidate.id FROM candidate)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
locked_delivery AS (
|
||||
SELECT delivery.recipient_user_id
|
||||
FROM deliveries AS delivery
|
||||
WHERE delivery.recipient_user_id IN (SELECT candidate.id FROM candidate)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
-- An account qualifies only when every token, session, mail request, and
|
||||
-- delivery row visible to the statement was locked above.
|
||||
deletable AS (
|
||||
SELECT candidate.id
|
||||
FROM candidate
|
||||
@@ -174,6 +191,20 @@ deletable AS (
|
||||
SELECT count(*) FROM locked_session
|
||||
WHERE locked_session.user_id = candidate.id
|
||||
)
|
||||
AND (
|
||||
SELECT count(*) FROM mail_requests AS request
|
||||
WHERE request.recipient_user_id = candidate.id
|
||||
) = (
|
||||
SELECT count(*) FROM locked_mail_request
|
||||
WHERE locked_mail_request.recipient_user_id = candidate.id
|
||||
)
|
||||
AND (
|
||||
SELECT count(*) FROM deliveries AS delivery
|
||||
WHERE delivery.recipient_user_id = candidate.id
|
||||
) = (
|
||||
SELECT count(*) FROM locked_delivery
|
||||
WHERE locked_delivery.recipient_user_id = candidate.id
|
||||
)
|
||||
)
|
||||
|
||||
DELETE FROM users
|
||||
|
||||
Reference in new issue
Block a user