From 4ac714acfd3192a397028f1260c5929e80ef15b5 Mon Sep 17 00:00:00 2001 From: Alexandre Pretto Nunes Date: Wed, 28 Jan 2015 14:42:47 -0200 Subject: #249 make the sending process a deferred --- service/pixelated/adapter/services/mail_sender.py | 4 +++- service/pixelated/adapter/services/mail_service.py | 12 ++++++++---- service/pixelated/resources/mails_resource.py | 18 ++++++++++++++---- 3 files changed, 25 insertions(+), 9 deletions(-) (limited to 'service/pixelated') diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 50c17ba5..1c2d7ce1 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -35,4 +35,6 @@ class MailSender(): file=StringIO(mail.to_smtp_format()), deferred=resultDeferred) - return reactor.connectTCP('localhost', 4650, senderFactory) + reactor.connectTCP('localhost', 4650, senderFactory) + + return resultDeferred diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 3b70890b..26b4e162 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -45,10 +45,14 @@ class MailService: return not(not(self.querier.get_header_by_chash(mail_id))) def send(self, last_draft_ident, mail): - self.mail_sender.sendmail(mail) - if last_draft_ident: - self.mailboxes.drafts().remove(last_draft_ident) - return self.mailboxes.sent().add(mail) + result = self.mail_sender.sendmail(mail) + + def success(_): + if last_draft_ident: + self.mailboxes.drafts().remove(last_draft_ident) + return self.mailboxes.sent().add(mail) + result.addCallback(success) + return result def mark_as_read(self, mail_id): return self.mail(mail_id).mark_as_read() diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index a6eb0fe0..40b29cfc 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -1,6 +1,7 @@ import json from pixelated.adapter.model.mail import InputMail -from pixelated.resources import respond_json +from pixelated.resources import respond_json, respond_json_deferred +from twisted.web import server from twisted.web.resource import Resource @@ -102,10 +103,19 @@ class MailsResource(Resource): draft_id = content_dict.get('ident') if draft_id: self._search_engine.remove_from_index(draft_id) - _mail = self._mail_service.send(draft_id, _mail) - self._search_engine.index_mail(_mail) + sendDeferred = self._mail_service.send(draft_id, _mail) - return respond_json(_mail.as_dict(), request) + def onSuccess(mail): + self._search_engine.index_mail(mail) + respond_json_deferred(mail.as_dict(), request) + + def onError(error): + return respond_json_deferred({'message': _format_exception(error)}, request, status_code=422) + + sendDeferred.addCallback(onSuccess) + sendDeferred.addErrback(onError) + + return server.NOT_DONE_YET except Exception as error: return respond_json({'message': _format_exception(error)}, request, status_code=422) -- cgit v1.2.3