Files
bvandeusenandClaude Opus 5.5 4e77ad0e7f
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m15s
CI / unit (push) Successful in 1m29s
CI / publish (push) Successful in 1m30s
feat(email): every email is a row in email_outbox until the server accepts it (#5506)
#5506 "Email outbox: a table of outgoing email and a sender that retries
until accepted, for up to an hour" (milestone 501).

- email_outbox (migration 0042) holds each message per recipient, its
  words, its state (held / pending / sending / sent / failed), attempts and
  the server's last answer. email_outbox_events links alert events for
  #5507.
- core/outbox.py: enqueue; a sender task every 15 s claims what is due
  (SKIP LOCKED, stale claims released), sends the batch over one
  connection with no session held, and settles each row:
  - accepted -> sent;
  - a 5xx refusal -> failed at once with the server's words;
  - no connection, a timeout, a 4xx -> retried after 30 s, 1, 2, 5, 10,
    15 min, the last try at the hour, then failed;
  - a test (retry=False) settles on its first answer.
  It runs on startup, so a row left by a restart goes out at boot.
- notifications.smtp_open is the one place a connection is opened.
- mail.message takes the html part as optional.
- cleanup ages email_outbox rows on the data window.

Senders move onto it in #5507 and #5508.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 15:53:53 -04:00

58 lines
2.7 KiB
Python

"""Outgoing email as rows until the mail server accepts them (#5506), and
which alert events each one carries (#5507).
Revision ID: 0042_email_outbox
Revises: 0041_alert_rule_covers
Create Date: 2026-10-09
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "0042_email_outbox"
down_revision: Union[str, None] = "0041_alert_rule_covers"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
"email_outbox",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("kind", sa.String(16), nullable=False),
sa.Column("recipient", sa.String(320), nullable=False),
sa.Column("tz", sa.String(64), nullable=False, server_default="UTC"),
sa.Column("show_utc", sa.Boolean, nullable=False, server_default=sa.false()),
sa.Column("subject", sa.Text, nullable=True),
sa.Column("text", sa.Text, nullable=True),
sa.Column("html", sa.Text, nullable=True),
sa.Column("state", sa.String(10), nullable=False, server_default="pending"),
sa.Column("retry", sa.Boolean, nullable=False, server_default=sa.true()),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("send_after", sa.DateTime(timezone=True), nullable=True),
sa.Column("next_attempt_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("first_attempt_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("attempts", sa.Integer, nullable=False, server_default="0"),
sa.Column("claimed_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_error", sa.Text, nullable=True),
sa.Column("sent_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_email_outbox_state_due", "email_outbox", ["state", "next_attempt_at"])
op.create_index("ix_email_outbox_created", "email_outbox", ["created_at"])
op.create_table(
"email_outbox_events",
sa.Column("outbox_id", sa.String(36),
sa.ForeignKey("email_outbox.id", ondelete="CASCADE"), primary_key=True),
sa.Column("event_id", sa.String(36),
sa.ForeignKey("alert_events.id", ondelete="CASCADE"), primary_key=True),
)
op.create_index("ix_email_outbox_events_event_id", "email_outbox_events", ["event_id"])
def downgrade() -> None:
op.drop_index("ix_email_outbox_events_event_id", table_name="email_outbox_events")
op.drop_table("email_outbox_events")
op.drop_index("ix_email_outbox_created", table_name="email_outbox")
op.drop_index("ix_email_outbox_state_due", table_name="email_outbox")
op.drop_table("email_outbox")