-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: send emails as a background tasks
- Loading branch information
1 parent
3bb9865
commit ef797ee
Showing
20 changed files
with
174 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
|
||
from polar.config import EmailSender as EmailSenderType | ||
from polar.config import settings | ||
from polar.exceptions import PolarError | ||
from polar.logging import Logger | ||
from polar.worker import enqueue_job | ||
|
||
log: Logger = structlog.get_logger() | ||
|
||
|
@@ -16,9 +18,17 @@ | |
DEFAULT_REPLY_TO_EMAIL_ADDRESS = "[email protected]" | ||
|
||
|
||
class EmailSenderError(PolarError): ... | ||
|
||
|
||
class SendEmailError(EmailSenderError): | ||
def __init__(self, message: str) -> None: | ||
super().__init__(message) | ||
|
||
|
||
class EmailSender(ABC): | ||
@abstractmethod | ||
async def send_to_user( | ||
async def send( | ||
self, | ||
*, | ||
to_email_addr: str, | ||
|
@@ -34,7 +44,7 @@ async def send_to_user( | |
|
||
|
||
class LoggingEmailSender(EmailSender): | ||
async def send_to_user( | ||
async def send( | ||
self, | ||
*, | ||
to_email_addr: str, | ||
|
@@ -62,7 +72,7 @@ def __init__(self) -> None: | |
super().__init__() | ||
self._api_key = settings.RESEND_API_KEY | ||
|
||
async def send_to_user( | ||
async def send( | ||
self, | ||
*, | ||
to_email_addr: str, | ||
|
@@ -85,15 +95,24 @@ async def send_to_user( | |
payload["reply_to"] = f"{reply_to_name} <{reply_to_email_addr}>" | ||
|
||
async with httpx.AsyncClient() as client: | ||
response = await client.post( | ||
"https://api.resend.com/emails", | ||
headers={ | ||
"Authorization": f"Bearer {self._api_key}", | ||
}, | ||
json=payload, | ||
) | ||
response.raise_for_status() | ||
email = response.json() | ||
try: | ||
response = await client.post( | ||
"https://api.resend.com/emails", | ||
headers={ | ||
"Authorization": f"Bearer {self._api_key}", | ||
}, | ||
json=payload, | ||
) | ||
response.raise_for_status() | ||
email = response.json() | ||
except httpx.HTTPError as e: | ||
log.warning( | ||
"resend.send_error", | ||
to_email_addr=to_email_addr, | ||
subject=subject, | ||
error=e, | ||
) | ||
raise SendEmailError(str(e)) from e | ||
|
||
log.info( | ||
"resend.send", | ||
|
@@ -109,3 +128,26 @@ def get_email_sender() -> EmailSender: | |
|
||
# Logging in development | ||
return LoggingEmailSender() | ||
|
||
|
||
def enqueue_email( | ||
to_email_addr: str, | ||
subject: str, | ||
html_content: str, | ||
from_name: str = DEFAULT_FROM_NAME, | ||
from_email_addr: str = DEFAULT_FROM_EMAIL_ADDRESS, | ||
email_headers: dict[str, str] = {}, | ||
reply_to_name: str | None = DEFAULT_REPLY_TO_NAME, | ||
reply_to_email_addr: str | None = DEFAULT_REPLY_TO_EMAIL_ADDRESS, | ||
) -> None: | ||
enqueue_job( | ||
"email.send", | ||
to_email_addr=to_email_addr, | ||
subject=subject, | ||
html_content=html_content, | ||
from_name=from_name, | ||
from_email_addr=from_email_addr, | ||
email_headers=email_headers, | ||
reply_to_name=reply_to_name, | ||
reply_to_email_addr=reply_to_email_addr, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from arq import Retry | ||
|
||
from polar.worker import JobContext, PolarWorkerContext, compute_backoff, task | ||
|
||
from .sender import SendEmailError, get_email_sender | ||
|
||
|
||
@task("email.send", max_tries=10) | ||
async def email_send( | ||
ctx: JobContext, | ||
to_email_addr: str, | ||
subject: str, | ||
html_content: str, | ||
from_name: str, | ||
from_email_addr: str, | ||
email_headers: dict[str, str], | ||
reply_to_name: str | None, | ||
reply_to_email_addr: str | None, | ||
polar_context: PolarWorkerContext, | ||
) -> None: | ||
email_sender = get_email_sender() | ||
|
||
try: | ||
await email_sender.send( | ||
to_email_addr=to_email_addr, | ||
subject=subject, | ||
html_content=html_content, | ||
from_name=from_name, | ||
from_email_addr=from_email_addr, | ||
email_headers=email_headers, | ||
reply_to_name=reply_to_name, | ||
reply_to_email_addr=reply_to_email_addr, | ||
) | ||
except SendEmailError as e: | ||
raise Retry(compute_backoff(ctx["job_try"])) from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.