Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #31 from fedora-infra/feature/producer-errors
Browse files Browse the repository at this point in the history
a00e51c - Log errors from the routine polling producers.
  • Loading branch information
ralphbean committed Sep 16, 2014
2 parents fe20ca0 + ec40383 commit a138144
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
41 changes: 25 additions & 16 deletions fmn/consumer/backends/mail.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from fmn.consumer.backends.base import BaseBackend
import fedmsg.meta

from kitchen.text.converters import to_bytes

import datetime
import smtplib
import email


confirmation_template = """
confirmation_template = u"""
{username} has requested that notifications be sent to this email address
* To accept, visit this address:
{acceptance_url}
Expand All @@ -16,7 +18,7 @@
email {support_email} if you have any concerns/issues/abuse.
"""

reason = """
reason = u"""
You received this message due to your preference settings at
{base_url}/{user}/email/{filter_id}
"""
Expand All @@ -42,36 +44,43 @@ def send_mail(self, session, recipient, subject, content):
return

email_message = email.Message.Message()
email_message.add_header('To', recipient['email address'])
email_message.add_header('From', self.from_address)
to_addr = ('utf-8', None, to_bytes(recipient['email address']))
email_message.add_header('To', to_addr)
from_addr = ('utf-8', None, to_bytes(self.from_address))
email_message.add_header('From', from_addr)

subject_prefix = self.config.get('fmn.email.subject_prefix', '')
if subject_prefix:
subject = '{0} {1}'.format(
subject_prefix.strip(), subject.strip())

email_message.add_header('Subject', subject)
email_message.add_header('Subject', ('utf-8', None, to_bytes(subject)))

# Since we do simple text email, adding the footer to the content
# before setting the payload.
footer = self.config.get('fmn.email.footer', '')
footer = to_unicode(self.config.get('fmn.email.footer', ''))

if 'filter_id' in recipient and 'user' in recipient:
base_url = self.config['fmn.base_url']
footer = reason.format(base_url=base_url, **recipient) + footer

if footer:
content += '\n\n--\n{0}'.format(footer.strip())
content += u'\n\n--\n{0}'.format(footer.strip())

email_message.set_payload(content)
email_message.set_payload(('utf-8', None, to_bytes(content)))

server = smtplib.SMTP(self.mailserver)
server.sendmail(
self.from_address.encode('utf-8'),
[recipient['email address'].encode('utf-8')],
email_message.as_string().encode('utf-8'),
)
server.quit()
try:
server.sendmail(
to_bytes(self.from_address),
[to_bytes(recipient['email address'])],
to_bytes(email_message.as_string()),
)
except:
self.log.info("%r" % email_message.as_string())
raise
finally:
server.quit()
self.log.debug("Email sent")

def handle(self, session, recipient, msg):
Expand All @@ -87,7 +96,7 @@ def _format_line(msg):
return timestamp.strftime("%c") + ", " + payload

n = len(queued_messages)
subject = "Fedora Notifications Digest (%i updates)" % n
subject = u"Fedora Notifications Digest (%i updates)" % n
content = "\n".join([
_format_line(queued_message.message)
for queued_message in queued_messages])
Expand All @@ -108,7 +117,7 @@ def handle_confirmation(self, session, confirmation):
support_email=self.config['fmn.support_email'],
username=confirmation.openid,
).strip()
subject = 'Confirm notification email'
subject = u'Confirm notification email'

recipient = {'email address': confirmation.detail_value}

Expand Down
2 changes: 1 addition & 1 deletion fmn/consumer/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def poll(self):
self.work(session)
session.commit()
except:
log.exception('Error during routine work.')
session.rollback()
raise


class ConfirmationProducer(FMNProducerBase):
Expand Down

0 comments on commit a138144

Please sign in to comment.