Skip to content
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

Celery Email backend instead of base classes? #14

Open
GitRon opened this issue Jun 10, 2024 · 0 comments
Open

Celery Email backend instead of base classes? #14

GitRon opened this issue Jun 10, 2024 · 0 comments

Comments

@GitRon
Copy link
Contributor

GitRon commented Jun 10, 2024

Maybe it's a better approach (having background workers and further email changes in mind), to create an email backend for celery instead of creating a base-class for all types of async dispatching types.

Thx to @baskoopmans for these code snippets.

class CeleryEmailBackend(BaseEmailBackend):
 """
 Add the email messages batched to a celery task to send the messages trough another backend
 """
   def send_messages(self, email_messages, queue=None):
      queue = queue or EMAIL_BACKEND_CELERY_QUEUE
      email_messages = [jsonpickle.encode(x) for x in email_messages]
      task_email_backend_celery_send_messages.apply_async(queue=queue, args=[email_messages])
      return len(email_messages)
@task(task_acks_late=True, ignore_result=False)
def task_email_backend_celery_send_messages(email_messages):
   email_messages = [jsonpickle.decode(x) for x in email_messages]
   backend = settings.EMAIL_BACKEND_CELERY
   with get_connection(backend) as connection:
     result = connection.send_messages(email_messages)
     return result(bearbeitet)

The combination of these 2 work fine, and as a EMAIL_BACKEND_CELERY i'm running anymail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant