"""Shared claim and completion rules for the two durable email queues.""" import uuid def queue_table(table: str) -> str: if table not in {"email_recap_deliveries", "newsletter_deliveries"}: raise ValueError("Unknown email queue") return table def claim(conn, table: str, now: float) -> dict | None: table = queue_table(table) conn.execute(f"""UPDATE {table} SET state='unknown', detail='Delivery interrupted after sending began; check the mail server.', updated_at=? WHERE state='sending' AND lease_until=3 THEN 'failed' ELSE 'retry' END, next_attempt_at=?, updated_at=?, detail='Email preparation interrupted.' WHERE state='preparing' AND lease_until