简体   繁体   English

气流电子邮件操作员成功/失败

[英]Airflow Email Operator Success / Failure

I'm currently working on a DAG that will email a list of users whether the DAG has completed successfully or has failed.我目前正在开发一个 DAG,无论 DAG 是成功完成还是失败,它都会通过电子邮件发送用户列表。 I am trying to have the flow of the DAG look like the example here:我试图让 DAG 的流程看起来像这里的例子:

from datetime import datetime
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.operators.email_operator import EmailOperator

def print_hello():
    return 'Hello world!'

default_args = {
        'owner': 'peter',
        'start_date':datetime(2018,8,11),
}

dag = DAG('hello_world', description='Simple tutorial DAG',
          schedule_interval='* * * * *',
          default_args = default_args, catchup=False)

hello_operator = PythonOperator(task_id='hello_task', python_callable=print_hello, dag=dag)

email_success = EmailOperator(
        task_id='send_email',
        to='to@gmail.com',
        subject='Airflow Alert Success',
        html_content=""" <h3>Email Test Success</h3> """,
        dag=dag
)

email_failure = EmailOperator(
        task_id='send_email',
        to='to@gmail.com',
        subject='Airflow Alert Failure',
        html_content=""" <h3>Email Test Failed</h3> """,
        dag=dag
)

hello_operator.set_downstream(email_success,email_failure)

Is there a built in operator that I can use with airflow to decide whether the email_success operator is sent when the DAG completes or if email_failure operator is executed when the DAG fails for any reason?是否有一个内置的运算符可以与气流一起使用来决定是在 DAG 完成时发送 email_success 运算符还是在 DAG 因任何原因失败时执行 email_failure 运算符?

Thank you谢谢

I ran into this question while searching for how to get Airflow to email me upon success.我在寻找如何让 Airflow 在成功后给我发电子邮件时遇到了这个问题。

Airflow is able to email on failure or retry with the following in default_args. Airflow 能够在失败时发送电子邮件或使用 default_args 中的以下内容重试

default_args = {
    'email': ['some_email@gmail.com'],
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 1,
    'retry_delay': timedelta(minutes=5)
 }

This should help with part of your question regarding failure, and you could use your 'email_success' EmailOperator as the last task in your sequence.这应该有助于解决您关于失败的部分问题,您可以使用“email_success”EmailOperator 作为序列中的最后一个任务。

I think you can can use the BranchOperator to decide to send an email on Failure or Success.我认为您可以使用BranchOperator来决定发送有关失败或成功的电子邮件。 I have the same scenario whereas I send an email on failure and just ran a DummyOperator if success.我有同样的情况,而我在失败时发送电子邮件,如果成功则只运行 DummyOperator。 在此处输入图片说明

You can check this related question about branchOperator How does Airflow's BranchPythonOperator work?您可以查看有关 branchOperator 的相关问题Airflow 的 BranchPythonOperator 如何工作?

I guess what you are looking for are Trigger Rules我猜你要找的是触发规则

You could set the trigger rule for the email that should be sent when nothing fails to all_success (default) and the trigger rule for the email that should be sent on failure to all_failed .您可以为all_success (默认)设置没有失败时应发送的电子邮件的触发规则,以及all_failed失败时应发送的电子邮件的触发规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM