首页 > 解决方案 > 气流:使用电子邮件操作员时无法分配请求的地址错误

问题描述

无法接收任务失败的电子邮件,甚至无法使用 EmailOperator

嗨,大家好,

即使添加了发送所需的参数,我也无法从我的邮箱接收电子邮件。

下面是我的默认参数的样子——

default_args = {
    'owner': 'phonrao',
    'depends_on_past': False,
    #'start_date': datetime(2019, 3, 28),
    'start_date': airflow.utils.dates.days_ago(2),
    'email': ['phonrao@gmail.com'],
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
    #'on_failure_callback': report_failure,
    #'end_date': datetime(2020,4 ,1),
    #'schedule_interval': '@hourly',
}

我在两者之间几乎没有 HttpsOperator 任务——它们运行良好并且成功,但它们不会在错误时发送电子邮件(我故意尝试引入错误以检查它们是否发送任何电子邮件)。下面是我的任务的一个例子。

t1 = SimpleHttpOperator(
                   task_id='t1',
                   http_conn_id='http_waterfall',
                   endpoint='/update_data',
                   method='POST',
                   headers={"Content-Type":"application/json"},
                                   xcom_push=True,
                                   log_response=True,
                   dag=dag,
)

这是我的 EmailOperator 任务

t2 = EmailOperator(
              dag=dag,
                  task_id="send_email",
                  to='phonrao@gmail.com',
                          subject='Success',
                      html_content="<h3>Success</h3>" 
)

t2 >> t1 

以下是来自日志的错误:

[2019-04-02 15:28:21,305] {{base_task_runner.py:101}} INFO - Job 845: Subtask send_email [2019-04-02 15:28:21,305] {{cli.py:520}} INFO - Running <TaskInstance: schedulerDAG.send_email 2019-04-02T15:23:08.896589+00:00 [running]> on host a47cd79aa987
[2019-04-02 15:28:21,343] {{logging_mixin.py:95}} INFO - [2019-04-02 15:28:21,343] {{configuration.py:255}} WARNING - section/key [smtp/smtp_user] not found in config
[2019-04-02 15:28:21,343] {{models.py:1788}} ERROR - [Errno 99] Cannot assign requested address
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/airflow/models.py", line 1657, in _run_raw_task
    result = task_copy.execute(context=context)
  File "/usr/local/lib/python3.6/site-packages/airflow/operators/email_operator.py", line 78, in execute
    mime_subtype=self.mime_subtype, mime_charset=self.mime_charset)
  File "/usr/local/lib/python3.6/site-packages/airflow/utils/email.py", line 55, in send_email
    mime_subtype=mime_subtype, mime_charset=mime_charset, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/airflow/utils/email.py", line 101, in send_email_smtp
    send_MIME_email(smtp_mail_from, recipients, msg, dryrun)
  File "/usr/local/lib/python3.6/site-packages/airflow/utils/email.py", line 121, in send_MIME_email
    s = smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT) if SMTP_SSL else smtplib.SMTP(SMTP_HOST, SMTP_PORT)
  File "/usr/local/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.6/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python3.6/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/local/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/local/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
[2019-04-02 15:28:21,351] {{models.py:1817}} INFO - All retries failed; marking task as FAILED

下面是我的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 = localhost
smtp_starttls = True
smtp_ssl = False
# Uncomment and set the user/pass settings if you want to use SMTP AUTH
# smtp_user = airflow
# smtp_password = airflow
smtp_port = 25
smtp_mail_from = airflow@example.com

有没有人遇到过这个问题以及如何解决这个问题的任何建议?

标签: smtpairflow

解决方案


如果您的气流在 Kubernetes 上运行(由 helm chart 安装),您应该查看“airflow-worker-0” pod,并确保 SMTP_HOST 或 SMTP_USER ... 的环境变量在配置中可用。简单调试,访问airflow-worker的容器,然后运行python,然后尝试这些命令以确保它正常工作。

import airflow 
airflow.utils.email.send_email('example@gmail.com', 'Airflow TEST HERE', 'This is airflow status success')

我有同样的问题,通过解决 SMTP 的环境变量。现在它起作用了。


推荐阅读