Michelle Wu

    Michelle Wu

    1 year ago
    Hey guys. I’ve been running into problems with
    EmailTask
    in
    state_handlers
    . I want to receive a email message that shows
    task
    and
    new_state
    when failures occur. My handler looks something like this:
    def email_on_failure(task, old_state, new_state):
        if new_state.is_failed():
             task = EmailTask(
                subject="Prefect alert: {}".format(new_state),
                msg = "Task: {}; New State: {}".format(task, new_state),
                email_from='xxx',
                email_to='xxx',
                smtp_server='smtp.gmail.com',
                smtp_port=xxx,
                smtp_type='STARTTLS'
            ).run()
    In tests, I can always receive the email with the correct subject, but the
    task
    and the
    new_state
    was always missing in the
    msg
    …Anyone has encountered similar problems?
    Jim Crist-Harif

    Jim Crist-Harif

    1 year ago
    hmmm, I wonder if this has to do with html interpreting the
    <>
    brackets on the state as something? Try using
    html.escape
    to fix this:
    import html
    
    def email_on_failure(task, old_state, new_state):
        if new_state.is_failed():
             task = EmailTask(
                subject="Prefect alert: {}".format(new_state),
                msg = html.escape("Task: {}; New State: {}".format(task, new_state)),
                email_from='xxx',
                email_to='xxx',
                smtp_server='<http://smtp.gmail.com|smtp.gmail.com>',
                smtp_port=xxx,
                smtp_type='STARTTLS'
            ).run()
    Michelle Wu

    Michelle Wu

    1 year ago
    @Jim Crist-Harif It worked perfectly, thank you!