Ejemplo n.º 1
0
def _send_email(email_config, about, start_time, end_time=None):
    """Send email about job or audit that is starting or ending.

    Arguments:
        email_config (dict): Top-level email configuration dictionary.
        about (str): A short string that says what the email
            notification is about, e.g., ``'job'`` or ``'audit'``.
        start_time (time.struct_time): Start time of job or audit.
        end_time (time.struct_time): End time of job or audit. This
            argument must not be specified if the job or audit is
            starting.

    """
    state = 'starting' if end_time is None else 'ending'
    if email_config is None:
        _log.info(
            'Skipping email notification because email config is '
            'missing; about: %s; state: %s', about, state)
        return

    _log.info('Sending email; about: %s; state: %s', about, state)

    # This part of the content is common for both starting and
    # ending states.
    time_fmt = '%Y-%m-%d %H:%M:%S %z (%Z)'
    content = """
    About: {}
    Started: {}
    """.format(about, time.strftime(time_fmt, start_time))
    content = textwrap.dedent(content).lstrip()

    # This part of the content is added only for ending state.
    if state == 'ending':
        duration = time.mktime(end_time) - time.mktime(start_time)
        mm, ss = divmod(duration, 60)
        hh, mm = divmod(mm, 60)

        end_content = """
        Ended: {}
        Duration: {:02.0f} h {:02.0f} m {:02.0f} s
        """.format(time.strftime(time_fmt, end_time), hh, mm, ss)

        content = content + textwrap.dedent(end_content).lstrip()

    util.send_email(content=content, **email_config)
    _log.info('Sent email; about: %s; state: %s', about, state)
Ejemplo n.º 2
0
 def done(self):
     """Send the buffered events as an email alert."""
     self._kwargs['content'] = '\n\n'.join(self._buffer)
     util.send_email(**self._kwargs)