-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mailer worker for sending emails
Add an h/mailer.py module with a send() function for user code to send emails asynchronously off the main thread, and a hypothesis-worker process that sends the emails. Change the account activation and forgot password emails to use this module. Remove unused imports from accounts/views.py since it no longer handles sending emails itself. Remove accounts/worker.py since it's no longer used. Update tests.
- Loading branch information
Showing
6 changed files
with
102 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
web: gunicorn -w ${WEB_CONCURRENCY:-1} --paster conf/${HYP_ENV:-production}.ini | ||
notification: hypothesis-worker conf/${HYP_ENV:-production}.ini notification | ||
nipsa: hypothesis-worker conf/${HYP_ENV:-production}.ini nipsa | ||
activation: hypothesis-worker conf/${HYP_ENV:-production}.ini activation | ||
email: hypothesis-worker conf/${HYP_ENV:-production}.ini email | ||
assets: hypothesis assets conf/${HYP_ENV:-production}.ini | ||
initdb: hypothesis initdb conf/${HYP_ENV:-production}.ini |
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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
# -*- coding: utf-8 -*- | ||
"""A module for sending emails asynchronously. | ||
Most code that sends emails should do so through this module. | ||
User code should call the send() function to cause an email to be sent | ||
asynchronously. | ||
""" | ||
import json | ||
|
||
import pyramid_mailer | ||
import pyramid_mailer.message | ||
|
||
|
||
def send(request, recipients, subject, body): | ||
"""Cause an email to be sent asynchronously. | ||
:param request: the request object for the request that wants to send the | ||
:type request: pyramid.request.Request | ||
:param recipients: the list of email addresses to send the email to | ||
:type recipients: list of unicode strings | ||
:param subject: the subject of the email | ||
:type subject: unicode | ||
:param body: the body of the email | ||
:type body: unicode | ||
""" | ||
request.get_queue_writer().publish( | ||
"email", {"recipients": recipients, "subject": subject, "body": body}) | ||
|
||
|
||
def worker(request): | ||
"""A hypothesis-worker function for sending emails. | ||
Connects to the "email" topic that send() above publishes to and | ||
sends an email for each message received. | ||
""" | ||
def handle_message(_, message): | ||
"""Receive a message from nsq and send it as an email.""" | ||
body = json.loads(message.body) | ||
email = pyramid_mailer.message.Message( | ||
subject=body["subject"], recipients=body["recipients"], | ||
body=body["body"]) | ||
pyramid_mailer.get_mailer(request).send_immediately(email) | ||
|
||
reader = request.get_queue_reader("email", "mailer") | ||
reader.on_message.connect(handle_message) | ||
reader.start(block=True) |
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