iT邦幫忙

2025 iThome 鐵人賽

DAY 20
0
AI & Data

五年前 IT 路人轉職資料分析,前進資料科學之路系列 第 20

資料工程 - airflow (4) smtp 設定

  • 分享至 

  • xImage
  •  

如果你在 airflow 上有很多排程,且不同排程跑得週期不同(每日、每週或每月),也同時有不同的人來維護自己寫的排程時。
那麼在查看哪些排程正常、哪些有狀況就會變得有些吃力。

這時候,除了 DAG decorator 上標註 owner 或是 tags 來分類由誰來維護、是關於什麼專案之外,還有其他可以自動通知自己排程跑失敗的方法嗎?
其實就在 DAG decorator 中的 default_args,包含排程失敗時會寄 email 給誰。

但寄件發送方要怎麼設定呢?就也是這個 SMTP 的來源怎麼設定呢?

Step 1.
在 config 資料夾下(與 docker-compose.yaml 同一層),在 airflow.cfg 檔中,設定 email 寄送的內容

[email]
email_backend = airflow.utils.email.send_email_smtp
from_email = "your_name <your_email>"
html_content_template = /opt/airflow/config/html_content_template.txt

Step 2.
設定 email 的寄送內容,也就是上面提到的 html_content_template,我分享一下我看到有人使用的模板

Try {{try_number}} out of {{max_tries + 1}}<br>
Exception:<br>{{exception_html}}<br>
Host: {{ti.hostname}}<br>

Step 3.
設定 SMTP。
可以用自己的 gmail 來進行設定,可參考這個 stackoverflow 的步驟 : https://stackoverflow.com/questions/51829200/how-to-set-up-airflow-send-email

Step 4.
在 DAG decorator 中加入如下 default_args

@DAG(
    ...
    default_args = {
        'email': ['your_email@gmail.com'],
        'email_on_failure': True,
        'email_on_retry': True
    }
    ...
)

那麼後續在 DAG 出跑失敗時,就會寄送通知 email 到第4步驟中所標註的 email 地址。

參考資料 - airflow email configuration : https://airflow.apache.org/docs/apache-airflow/stable/howto/email-config.html


上一篇
資料工程 - airflow (3) DAG 的切分方式
下一篇
資料工程 - 簡易 Linux 指令
系列文
五年前 IT 路人轉職資料分析,前進資料科學之路24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言