-
Notifications
You must be signed in to change notification settings - Fork 0
Add email client to send emails via celery tasks #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stefanotroncaro
wants to merge
7
commits into
feat/mailpit-integration
Choose a base branch
from
feat/email-service-async
base: feat/mailpit-integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2661982
feat: celery task email client default on api container
stefanotroncaro e541d46
feat: per email retry configuration
stefanotroncaro 37a96a8
feat: improve request client error message
stefanotroncaro 015d1e0
feat: add posilibity to pass error message in email context
stefanotroncaro 371ca4a
refactor: minor
stefanotroncaro 897e060
refactor: email client selection code
stefanotroncaro 3cb1cad
refactor: for legibility
stefanotroncaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,42 +1,46 @@ | ||
| from uuid import UUID | ||
| from app.emails.exceptions.email_client_exception import EmailClientException | ||
| from typing import Final | ||
|
|
||
| from celery import Task | ||
|
|
||
| from app.common.exceptions import ExternalProviderException | ||
| from app.common.schemas.pagination_schema import ListFilter | ||
| from app.emails.services.emails_service import EmailService | ||
| from app.db.session import SessionLocal | ||
| from app.emails import Email, EmailService, get_client | ||
| from app.main import celery | ||
|
|
||
|
|
||
| from app.core.config import get_settings | ||
| from app.users.schemas.user_schema import UserInDB | ||
| from app.users.services.users_service import UsersService | ||
|
|
||
| settings = get_settings() | ||
|
|
||
| BACKOFF_EXPONENTIAL_GROWTH_BASE: Final[int] = 2 | ||
|
|
||
|
|
||
| @celery.task | ||
| def send_reminder_email() -> None: | ||
| session = SessionLocal() | ||
| try: | ||
| service = EmailService() | ||
|
|
||
| with SessionLocal() as session: | ||
| users = UsersService(session).list(ListFilter(page=1, page_size=100)) | ||
| for user in users.data: | ||
| EmailService().send_user_remind_email( | ||
| UserInDB.model_validate(user) | ||
| ) | ||
| finally: | ||
| session.close() | ||
|
|
||
|
|
||
| @celery.task( | ||
| autoretry_for=(EmailClientException,), | ||
| retry_backoff=settings.SEND_WELCOME_EMAIL_RETRY_BACKOFF_VALUE, | ||
| max_retries=settings.SEND_WELCOME_EMAIL_MAX_RETRIES, | ||
| retry_jitter=False, | ||
| ) | ||
| def send_welcome_email(user_id: UUID) -> None: | ||
| session = SessionLocal() | ||
| service.send_user_remind_email(UserInDB.model_validate(user)) | ||
|
|
||
|
|
||
| @celery.task(bind=True) | ||
| def send_email(self: Task, serialized_email: dict) -> None: | ||
| email = Email(**serialized_email) | ||
| client = get_client() | ||
|
|
||
| try: | ||
| user = UsersService(session).get_by_id(user_id) | ||
| if user: | ||
| EmailService().send_new_user_email(UserInDB.model_validate(user)) | ||
| finally: | ||
| session.close() | ||
| client.send_email(email) | ||
| except ExternalProviderException as exc: | ||
| if not email.context: | ||
| raise | ||
|
|
||
| multiplicator = BACKOFF_EXPONENTIAL_GROWTH_BASE**self.request.retries | ||
| countdown_in_seconds = email.context.backoff_in_seconds * multiplicator | ||
|
|
||
| raise self.retry( | ||
| exc=exc, | ||
| max_retries=email.context.max_retries, | ||
| countdown=countdown_in_seconds, | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from .mailpit_email_client import MailpitEmailClient | ||
| from .example_email_client import ExampleEmailClient | ||
| from .celery_task_email_client import CeleryTaskEmailClient |
This file contains hidden or 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,21 @@ | ||
| from app.emails.clients.base import BaseEmailClient | ||
| from app.emails.schema.email import Email, EmailContext | ||
|
|
||
|
|
||
| class CeleryTaskEmailClient(BaseEmailClient): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| from app.celery.tasks.emails import send_email | ||
|
|
||
| self.task = send_email | ||
|
|
||
| def send_email(self, /, email: Email) -> None: | ||
| if not email.context: | ||
| email.context = EmailContext() | ||
|
|
||
| serialized_email = email.model_dump( | ||
| mode="json", | ||
| exclude_unset=True, | ||
| ) | ||
|
|
||
| self.task.delay(serialized_email) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.