การใช้งาน SmtpNotifier
หลังจากสร้าง failure callback สำเร็จแล้ว หัวหน้าทีมได้ขอให้ส่งอีเมลแจ้งผลทุกครั้งที่ DAG รันสำเร็จไปยัง team alias เมื่อศึกษาเอกสารของ Airflow แล้ว คุณตัดสินใจใช้ notifier framework ที่ Airflow มีให้ในตัว
ออบเจกต์ dag และ task ถูก import ไว้แล้ว รวมถึงฟังก์ชัน callback alert_on_failure และ task get_sales_data กับ process_sales_data ก็ถูกกำหนดไว้ให้แล้วเช่นกัน
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Apache Airflow เบื้องต้นด้วย Python
คำแนะนำการฝึกหัด
- Import ออบเจกต์ SMTP notifier
- เพิ่ม attribute success callback ให้กับ DAG
- ส่งการแจ้งเตือนไปยัง [email protected]
- ระบุหัวเรื่อง (subject) ของอีเมล
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import the notifier
from airflow.providers.smtp.notifications.smtp import ____
# Add a success callback notifier
@dag(dag_id='sales_etl_dag',
on_failure_callback=alert_on_failure,
____=SmtpNotifier(
to="____",
from_email="[email protected]",
____="sales_etl_dag run succeeded!"
))
def sales_etl_dag():
get_sales_data() >> process_sales_data()
sales_etl_dag()