littlefish/lfsmailer.py
def emit(self, record):
        """
        Emit a record.

        Format the record and send it to the specified addressees.
        """
        try:
            # First, remove all records from the rate limiter list that are over a minute old
            now = timetool.unix_time()
            one_minute_ago = now - 60
            new_rate_limiter = [x for x in self.rate_limiter if x > one_minute_ago]
            log.debug('Rate limiter %s -> %s' % (len(self.rate_limiter), len(new_rate_limiter)))
            self.rate_limiter = new_rate_limiter

            # Now, get the number of emails sent in the last minute.  If it's less than the threshold, add another
            # entry to the rate limiter list
            recent_sends = len(self.rate_limiter)
            send_email = recent_sends < self.max_sends_per_minute
            if send_email:
                self.rate_limiter.append(now)

            msg = self.format(record)
            msg = self.add_details(msg)

            # Finally send the message!
            if send_email:
                if DEBUG_ERROR_EMAIL_SENDING:
                    log.info('@@@> ! Sending error email to {} !'.format(self.toaddrs))
                send_text_mail(self.toaddrs, self.subject, msg, self.fromaddr)
            else:
                log.info('!! WARNING: Not sending email as too many emails have been sent in the past minute !!')
                log.info(msg)

        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            self.handleError(record)
def emit(self, record):
        """
        Emit a record.

        Format the record and send it to the specified addressees.
        """
        try:
            # First, remove all records from the rate limiter list that are over a minute old
            now = timetool.unix_time()
            one_minute_ago = now - 60
            new_rate_limiter = [x for x in self.rate_limiter if x > one_minute_ago]
            log.debug('Rate limiter %s -> %s' % (len(self.rate_limiter), len(new_rate_limiter)))
            self.rate_limiter = new_rate_limiter

            # Now, get the number of emails sent in the last minute.  If it's less than the threshold, add another
            # entry to the rate limiter list
            recent_sends = len(self.rate_limiter)
            send_email = recent_sends < self.max_sends_per_minute
            if send_email:
                self.rate_limiter.append(now)

            msg = self.format(record)
            msg = self.add_details(msg)

            # Finally send the message!
            if send_email:
                if DEBUG_ERROR_EMAIL_SENDING:
                    log.info('@@@> ! Sending error email to {} !'.format(self.toaddrs))
                send_text_mail(self.toaddrs, self.subject, msg, self.fromaddr)
            else:
                log.info('!! WARNING: Not sending email as too many emails have been sent in the past minute !!')
                log.info(msg)

        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            self.handleError(record)
Emit a record.

        Format the record and send it to the specified addressees.