Example #1
0
    def _send_event_notifications(self, event, extra):
        t = self.template
        to_send = []

        if event.type == 'comment':
            emails = t.admin_emails | t.uploader_emails
            detail = i18n.new_comment
            body = i18n.new_comment_body.format(
                username=event.username,
                comment=event.content
            )
            to_send = ((emails, body),)

        elif event.type == 'run':
            run = extra
            detail = i18n.import_result
            to_send = self._get_run_finish_notifications(run)

        elif event.type == 'update':
            emails = t.admin_emails | t.uploader_emails
            detail = i18n.import_update
            body =  i18n.import_updated_body.format(
                username=event.username)
            to_send = ((emails, body),)

        else:
            raise Exception('Unknown event type %r' % event.type)

        config = self.template.manager.project.env.config

        subject = i18n.mail_summary.format(
            project_name=config.project_name,
            hostname=socket.gethostname(),
            template_label=self.template.label,
            detail=detail)

        for emails, body in to_send:
            if not emails:
                continue
            utils.send_mail(config, list(emails), subject, body)
    def send_restart_mail(self, fail_kind, restart_count, last_start_s):
        if not self.config.send_mail_on_restart:
            return

        log.info('Sending restart mail')
        hostname = socket.gethostname()
        LINE_COUNT = 500
        try:
            last_log = utils.tail(open(self.config.log_file, 'rb'), LINE_COUNT)
        except IOError:
            last_log = "[Not available]"

        subject = ('Synthese {fail_kind} on {hostname} (project: {project}, '
            'restarts: {restart_count})'.format(
                fail_kind=fail_kind,
                hostname=hostname,
                project=self.config.project_name,
                restart_count=restart_count))
        body = '''
Detected Synthese {fail_kind} on {hostname}. It is going to restart.
Total restart count: {restart_count}. Seconds since last start: {uptime_s}.

Last {line_count} lines of log:
{last_log}

Have a nice day,
The synthese.py wrapper script.
'''.format(
            fail_kind=fail_kind,
            hostname=hostname,
            restart_count=restart_count,
            line_count=LINE_COUNT,
            last_log=last_log,
            uptime_s=int(time.time() - last_start_s))

        utils.send_mail(self.env.config, self.config.mail_admins, subject, body)