Partition mail retention by tenant

This commit is contained in:
2026-08-04 09:29:36 +02:00
parent d7dc4f6b04
commit 7c8cb21144
2 changed files with 9 additions and 5 deletions
+8 -4
View File
@@ -874,15 +874,19 @@ def resend_delivery_command(
def purge_expired(
session: Session,
*,
tenant_id: str | None = None,
limit: int = 250,
) -> dict[str, object]:
now = utcnow()
clauses = [
MailDeliveryCommand.expires_at <= now,
MailDeliveryCommand.payload_purged_at.is_(None),
]
if tenant_id:
clauses.append(MailDeliveryCommand.tenant_id == tenant_id)
commands = session.scalars(
select(MailDeliveryCommand)
.where(
MailDeliveryCommand.expires_at <= now,
MailDeliveryCommand.payload_purged_at.is_(None),
)
.where(*clauses)
.order_by(MailDeliveryCommand.expires_at)
.limit(max(1, min(int(limit), 1000)))
).all()