<commit_before>from email.mime.text import MIMEText
import logging
import smtplib
import time

from . import BaseProcessor

log = logging.getLogger(__name__)


class NotificationProcessor(BaseProcessor):

    def __init__(self, notification_queue, sent_notification_queue, finished_queue, email_config):
        self.notification_queue = notification_queue
        self.sent_notification_queue = sent_notification_queue
        self.finished_queue = finished_queue

        self.email_config = email_config
        self.smtp = None
        self._smtp_connect()

        # Types as key, method used to process that type as value
        self.notification_types = {'email': self._send_email}

    def _send_email(self, notification):
        """ Send the notification via email
            Returns the notification upon success, None upon failure
        """
        try:
            msg = MIMEText("%s\nAlarm %s transitioned to the %s state at %s\nFull Data:\n%s"
                           % (notification.message,
                              notification.alarm_name,
                              notification.state,
                              notification.alarm_timestamp,
                              notification.to_json()))
            msg['Subject'] = '%s: %s' % (notification.state, notification.alarm_name)
            msg['From'] = self.email_config['from_addr']
            msg['To'] = notification.address
            self.smtp.sendmail(self.email_config['from_addr'], notification.address, msg.as_string())
            log.debug('Sent email to %s, notification %s' % (self.email_config['from_addr'], notification.to_json()))
        except smtplib.SMTPException, e:
            log.error("Error sending Email Notification:%s\nError:%s" % (notification.to_json(), e))
            self._smtp_connect()  # Reconnect in case connection problems caused the failed email
        else:
            return notification

    def _smtp_connect(self):
        """ Connect to the smtp server
        """
        log.info('Connecting to Email Server %s' % self.email_config['server'])
        smtp = smtplib.SMTP(
            self.email_config['server'], self.email_config['port'], timeout=self.email_config['timeout'])

        if self.email_config['user'] is not None:
            smtp.login(self.email_config['user'], self.email_config['password'])

        self.smtp = smtp
<commit_msg>Handle reconnecting to smtp servers without dropping an notifications.<commit_after>from email.mime.text import MIMEText
import logging
import smtplib
import time

from . import BaseProcessor

log = logging.getLogger(__name__)


class NotificationProcessor(BaseProcessor):

    def __init__(self, notification_queue, sent_notification_queue, finished_queue, email_config):
        self.notification_queue = notification_queue
        self.sent_notification_queue = sent_notification_queue
        self.finished_queue = finished_queue

        self.email_config = email_config
        self.smtp = None
        self._smtp_connect()

        # Types as key, method used to process that type as value
        self.notification_types = {'email': self._send_email}

    def _send_email(self, notification):
        """ Send the notification via email
            Returns the notification upon success, None upon failure
        """
        try:
            msg = MIMEText("%s\nAlarm %s transitioned to the %s state at %s\nFull Data:\n%s"
                           % (notification.message,
                              notification.alarm_name,
                              notification.state,
                              notification.alarm_timestamp,
                              notification.to_json()))
            msg['Subject'] = '%s: %s' % (notification.state, notification.alarm_name)
            msg['From'] = self.email_config['from_addr']
            msg['To'] = notification.address
            self.smtp.sendmail(self.email_config['from_addr'], notification.address, msg.as_string())
            log.debug('Sent email to %s, notification %s' % (self.email_config['from_addr'], notification.to_json()))
        except smtplib.SMTPServerDisconnected, e:
            log.debug('SMTP server disconnected. Will reconnect and retry message.')
            self._smtp_connect()
            try:
                self.smtp.sendmail(self.email_config['from_addr'], notification.address, msg.as_string())
                log.debug('Sent email to %s, notification %s' % (self.email_config['from_addr'], notification.to_json()))
            except smtplib.SMTPException, e:
                log.error("Error sending Email Notification:%s\nError:%s" % (notification.to_json(), e))
        except smtplib.SMTPException, e:
            log.error("Error sending Email Notification:%s\nError:%s" % (notification.to_json(), e))
        else:
            return notification

    def _smtp_connect(self):
        """ Connect to the smtp server
        """
        log.info('Connecting to Email Server %s' % self.email_config['server'])
        smtp = smtplib.SMTP(
            self.email_config['server'], self.email_config['port'], timeout=self.email_config['timeout'])

        if self.email_config['user'] is not None:
            smtp.login(self.email_config['user'], self.email_config['password'])

        self.smtp = smtp

{'fraction_non_alphanumeric': 0.10278614457831325, 'fraction_numerical': 0.0, 'mean_word_length': 2.4930966469428006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '411467', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1389, 'n_words': 331}