bash - 如何设置 Airflow 的电子邮件配置以发送有关错误的电子邮件?

标签 bash email smtp airflow

我试图通过传入不起作用的 Bash 行 (thisshouldnotrun) 来故意使 Airflow 任务失败并出错。 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?我在文档或网上找不到任何信息。如果我们需要双因素身份验证才能查看电子邮件,这对 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')

最佳答案

smtp_starttls 基本上意味着使用 TLS

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

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

参见 here .

编辑: 为了让 Airflow 在失败时发送电子邮件,需要在您的任务中设置几件事,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)

关于bash - 如何设置 Airflow 的电子邮件配置以发送有关错误的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44573923/

相关文章:

php - 在发送电子邮件时建立收件人给了我 Laravel 的一个异常(exception)

smtp - 如何在通过 laravel 5.4 不使用 php 邮件功能发送邮件时删除 “via” 和服务器名称

linux - SED - 在每个由大写字母和空格组成的输入行之后插入一个空行

email - G Suite 电子邮件迁移未完成,卡在 99%

linux - 将进程的 STDOUT 发送到屏幕和第二个进程,但等待两者完成

php - 将邮件标记为未读

Jenkins邮件无法连接失败

c# - GMail + C# + Web.Config : Send Mail Works Programmatically, 使用 Web.Config 值引发异常

mysql - Bitnami 谷歌服务器上的 Cronjob 不工作

linux - 如何将数组值添加到文本文件中特定行的开头