Implementing an SmtpNotifier
Now that you've successfully created a failure callback, your team lead has asked that all successful Dag runs be sent via email to a team alias. After reviewing the Airflow documentation, you decide that using Airflow's built-in notifier framework is your best option.
The dag and task objects are already imported, the alert_on_failure callback function and the get_sales_data and process_sales_data tasks are already defined for you.
This exercise is part of the course
Introduction to Apache Airflow in Python
Exercise instructions
- Import the SMTP notifier object.
- Add a success callback attribute to the Dag.
- Send the notification to [email protected].
- Include a subject.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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()