From bba0e7233e047b7e4be5786f4b654cb7feb17455 Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 09:13:19 +0400 Subject: [PATCH 1/7] SparkPostCeleryEmailBackend and tasks.py --- sparkpost/django/email_backend.py | 10 ++++++++++ sparkpost/django/tasks.py | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 sparkpost/django/tasks.py diff --git a/sparkpost/django/email_backend.py b/sparkpost/django/email_backend.py index 1e44a10..9ae6f60 100644 --- a/sparkpost/django/email_backend.py +++ b/sparkpost/django/email_backend.py @@ -4,6 +4,7 @@ from sparkpost import SparkPost from .message import SparkPostMessage +from .tasks import send_messages class SparkPostEmailBackend(BaseEmailBackend): @@ -37,3 +38,12 @@ def _send(self, message): params = getattr(settings, 'SPARKPOST_OPTIONS', {}).copy() params.update(message) return self.client.transmissions.send(**params) + + +class SparkPostCeleryEmailBackend(SparkPostEmailBackend): + def send_messages(self, email_messages): + """ + Send emails, returns celery result object (AsyncResult) + When task will be complete, it will contain integer representing number of successful emails + """ + return send_messages.delay(self, email_messages) diff --git a/sparkpost/django/tasks.py b/sparkpost/django/tasks.py new file mode 100644 index 0000000..d23b20c --- /dev/null +++ b/sparkpost/django/tasks.py @@ -0,0 +1,23 @@ +from celery import chord +from celery.task import task + + +@task() +def send_messages(obj, email_messages): + return chord(send_message.s(obj, email_message) for email_message in email_messages)(send_summary.s()) + + +@task() +def send_message(obj, message): + try: + response = obj._send(message) + except Exception: + if not obj.fail_silently: + raise + else: + return response['total_accepted_recipients'] + + +@task() +def send_summary(send_results): + return sum([send_result.result for send_result in send_results]) From ce5cc1fc30481d0527f5a258606596d259116364 Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 09:25:56 +0400 Subject: [PATCH 2/7] fix send_message() task --- sparkpost/django/tasks.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sparkpost/django/tasks.py b/sparkpost/django/tasks.py index d23b20c..0e3ef6c 100644 --- a/sparkpost/django/tasks.py +++ b/sparkpost/django/tasks.py @@ -1,6 +1,8 @@ from celery import chord from celery.task import task +from .message import SparkPostMessage + @task() def send_messages(obj, email_messages): @@ -10,7 +12,7 @@ def send_messages(obj, email_messages): @task() def send_message(obj, message): try: - response = obj._send(message) + response = obj._send(SparkPostMessage(message)) except Exception: if not obj.fail_silently: raise From 697e1f6b8753764e7c370aa053cd671c9f5d9b5c Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 09:27:43 +0400 Subject: [PATCH 3/7] fix send_summary() task --- sparkpost/django/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sparkpost/django/tasks.py b/sparkpost/django/tasks.py index 0e3ef6c..32e2f7a 100644 --- a/sparkpost/django/tasks.py +++ b/sparkpost/django/tasks.py @@ -22,4 +22,4 @@ def send_message(obj, message): @task() def send_summary(send_results): - return sum([send_result.result for send_result in send_results]) + return sum([send_result for send_result in send_results]) From 996cf148c2f0100fb5f30376009b3a33fd89c51c Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 09:42:44 +0400 Subject: [PATCH 4/7] fix comment --- sparkpost/django/tasks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sparkpost/django/tasks.py b/sparkpost/django/tasks.py index 32e2f7a..fad5a5c 100644 --- a/sparkpost/django/tasks.py +++ b/sparkpost/django/tasks.py @@ -6,6 +6,11 @@ @task() def send_messages(obj, email_messages): + """ + Celery task for 'send_messages' EmailBackend method. + It sends all email messages in parallel via 'send_message' task, + and then it reduces all results via `send_summary` task (celery chord is convenient) + """ return chord(send_message.s(obj, email_message) for email_message in email_messages)(send_summary.s()) From dc9aafd5ac8c8d6dba01f75fafb05b0afe7fe7a3 Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 10:24:22 +0400 Subject: [PATCH 5/7] fix flake8 --- sparkpost/django/email_backend.py | 3 ++- sparkpost/django/tasks.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/sparkpost/django/email_backend.py b/sparkpost/django/email_backend.py index 9ae6f60..34c045b 100644 --- a/sparkpost/django/email_backend.py +++ b/sparkpost/django/email_backend.py @@ -44,6 +44,7 @@ class SparkPostCeleryEmailBackend(SparkPostEmailBackend): def send_messages(self, email_messages): """ Send emails, returns celery result object (AsyncResult) - When task will be complete, it will contain integer representing number of successful emails + When task will be complete, it will contain integer + representing number of successful emails """ return send_messages.delay(self, email_messages) diff --git a/sparkpost/django/tasks.py b/sparkpost/django/tasks.py index fad5a5c..8aa800d 100644 --- a/sparkpost/django/tasks.py +++ b/sparkpost/django/tasks.py @@ -9,9 +9,11 @@ def send_messages(obj, email_messages): """ Celery task for 'send_messages' EmailBackend method. It sends all email messages in parallel via 'send_message' task, - and then it reduces all results via `send_summary` task (celery chord is convenient) + and then it reduces all results via `send_summary` task + (celery chord is convenient) """ - return chord(send_message.s(obj, email_message) for email_message in email_messages)(send_summary.s()) + return chord(send_message.s(obj, email_message) + for email_message in email_messages)(send_summary.s()) @task() From 4f1fe2f7c0aed015480a0967d0d84e51365d44f4 Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 10:44:11 +0400 Subject: [PATCH 6/7] fix tox.ini --- tox.ini | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index db71c33..30e6846 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = {py27,py34}-django{17,18}, py35-django{18,19} +envlist = {py27,py34}-django{17,18}-celery, py35-django{18,19}-celery [testenv] deps = @@ -7,10 +7,11 @@ deps = django17: Django>=1.7,<1.8 django18: Django>=1.8,<1.9 django19: Django>=1.9,<1.10 + celery: celery==3.1.23 commands = py.test test/ -[testenv:py35-django19] +[testenv:py35-django19-celery] commands = flake8 sparkpost test From 34fbd21944188d9c4822b5e723e9f5b42d08debb Mon Sep 17 00:00:00 2001 From: Anton Kuzmichev Date: Wed, 13 Apr 2016 10:48:51 +0400 Subject: [PATCH 7/7] fix dev requirements --- dev-requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 7187845..bbfe2bb 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -2,4 +2,5 @@ wheel twine Django>=1.7,<1.10 -tornado>=3.2 \ No newline at end of file +tornado>=3.2 +celery>=3.1.23 \ No newline at end of file