【问题标题】:How do I setup Airflow's email configuration to send an email on errors?如何设置 Airflow 的电子邮件配置以发送有关错误的电子邮件?
【发布时间】:2017-06-15 17:53:44
【问题描述】:

我试图通过传入不起作用的 Bash 行 (thisshouldnotrun) 故意使 Airflow 任务失败并出错。气流正在输出以下内容:

[2017-06-15 17:44:17,869] {bash_operator.py:94} INFO - /tmp/airflowtmpLFTMX7/run_bashm2MEsS: line 7: thisshouldnotrun: command not found
[2017-06-15 17:44:17,869] {bash_operator.py:97} INFO - Command exited with return code 127
[2017-06-15 17:44:17,869] {models.py:1417} ERROR - Bash command failed
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
    result = task_copy.execute(context=context)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in execute
    raise AirflowException("Bash command failed")
AirflowException: Bash command failed
[2017-06-15 17:44:17,871] {models.py:1433} INFO - Marking task as UP_FOR_RETRY
[2017-06-15 17:44:17,878] {models.py:1462} ERROR - Bash command failed
Traceback (most recent call last):
  File "/home/ubuntu/.local/bin/airflow", line 28, in <module>
    args.func(args)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/bin/cli.py", line 585, in test
    ti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/utils/db.py", line 53, in wrapper
    result = func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
    result = task_copy.execute(context=context)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in execute
    raise AirflowException("Bash command failed")
airflow.exceptions.AirflowException: Bash command failed

Airflow 会针对此类错误发送电子邮件吗?如果没有,针对这些错误发送电子邮件的最佳方式是什么?

我什至不确定airflow.cfg 是否设置正确...由于最终目标是测试电子邮件警报通知,我想确保正确设置airflow.cfg。这是设置:

[email]
email_backend = airflow.utils.email.send_email_smtp


[smtp]
# If you want airflow to send emails on retries, failure, and you want to use
# the airflow.utils.email.send_email_smtp function, you have to configure an
# smtp server here
smtp_host = emailsmtpserver.region.amazonaws.com 
smtp_starttls = True
smtp_ssl = False
# Uncomment and set the user/pass settings if you want to use SMTP AUTH
# smtp_user = airflow_data_user
# smtp_password = password
smtp_port = 587 
smtp_mail_from = airflow_data_user@domain.com

smtp_starttls 是什么?我在文档或在线中找不到任何信息。如果我们需要 2 因素身份验证来查看电子邮件,这对 Airflow 来说会是个问题吗?

这是我的 Bash 命令:

task1_bash_command = """
export PATH=/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
export rundate=`TZ='America/Los_Angeles' date +%F -d "yesterday"`
export AWS_CONFIG_FILE="/home/ubuntu/.aws/config"

/home/ubuntu/bin/snowsql -f //home/ubuntu/sql/script.sql 1> /home/ubuntu/logs/"$rundate"_dev.log 2> /home/ubuntu/logs/"$rundate"_error_dev.log

if [ -e /home/ubuntu/logs/"$rundate"_error_dev.log ]
then
    exit 64
fi

还有我的任务:

task1 = BashOperator(
    task_id = 'run_bash',
    bash_command = task1_bash_command,
    dag = dag,
    retries = 2,
    email_on_failure = True,
    email = 'username@domain.com')

【问题讨论】:

    标签: bash email smtp airflow apache-airflow


    【解决方案1】:

    smtp_starttls 基本上是指使用TLS

    如果您想改用 SSL,请将其设置为 False 并将 smtp_ssl 设置为 True。您可能需要smtp_usersmtp_password

    Airflow 不会处理两步验证。但是,如果您使用的是 AWS,您可能不需要它,因为您的 SMTP (SES) 凭证与您的 AWS 凭证不同。

    here

    编辑: 要让气流在失败时发送电子邮件,需要在您的任务中设置几件事,email_on_failureemail

    请看这里的例子:

    def throw_error(**context):
        raise ValueError('Intentionally throwing an error to send an email.')
    
    
    
    t1 = PythonOperator(task_id='throw_error_and_email',
                        python_callable=throw_error,
                        provide_context=True,
                        email_on_failure=True,
                        email='your.email@whatever.com',
                        dag=dag)
    

    【讨论】:

    • 感谢您的澄清。我仍在尝试了解 Airflow 将捕获或不会捕获哪些类型的错误 - 我的示例超出了 Airflow 的范围吗?
    • 它应该捕获任何任务失败,但您必须以某种方式定义您的任务。请查看我对答案的编辑以获取示例。
    • 是的,我认为我的问题是试图让 Bash 抛出错误,所以与 Airflow 无关
    • 你知道 Airflow 认为什么是 Bash 错误吗?当我调用 thisshouldnotrun 命令时,系统按预期返回 127 错误,但 Airflow 似乎并未将其视为失败。它甚至不重试任务。知道如何让它抛出一个完整的错误吗?
    • 我没有阅读所有这些 cmets,只是建议您不必使用 BashOperator 来运行您的 bash 命令。我使用的是 PythonOperator,我用 Python 做所有事情。您可以使用subprocess.run(...) 或该 Python 库的变体之一来运行 linux 命令。然后,您可以自己评估返回值,如果该值不是您所期望的,则手动抛出AirflowException。如果抛出 AirflowException,它将始终将任务标记为失败。所以不要拘泥于运营商的创意:)
    【解决方案2】:

    使用以下链接创建气流 dag。
    How to trigger daily DAG run at midnight local time instead of midnight UTC time

    方法 1: 您可以在本地设置 SMTP 并使其在作业失败时发送电子邮件。

    [email]
    email_backend = airflow.utils.email.send_email_smtp
    
    [smtp]
    smtp_host = localhost
    smtp_starttls = False
    smtp_ssl = False
    smtp_port = 25
    smtp_mail_from = noreply@company.com
    

    方法 2:您可以使用 Gmail 发送电子邮件。 我写了一篇文章来做到这一点。 https://helptechcommunity.wordpress.com/2020/04/04/airflow-email-configuration/

    【讨论】:

      【解决方案3】:

      如果我们需要 2 因素身份验证来查看电子邮件,这对 Airflow 来说会是个问题吗?

      您可以使用谷歌应用密码来绕过 2 因素身份验证

      https://support.google.com/mail/answer/185833?hl=en-GB

      来源 - https://docs.aws.amazon.com/mwaa/latest/userguide/configuring-env-variables.html

      【讨论】:

        猜你喜欢
        • 2011-07-28
        • 2016-11-02
        • 2016-06-08
        • 2016-03-10
        • 2013-05-05
        • 2017-02-22
        • 1970-01-01
        • 2013-07-06
        • 2015-08-11
        相关资源
        最近更新 更多