Complete the code to set an SLA for a task in Airflow.
task = PythonOperator(task_id='my_task', python_callable=my_func, sla=[1])
The sla parameter expects a timedelta object to define the SLA duration.
Complete the code to enable SLA miss notifications via email in Airflow's DAG.
default_args = {'email': ['admin@example.com'], 'email_on_[1]': True}To receive notifications on SLA misses, set email_on_sla_miss to True.
Fix the error in the SLA miss callback function definition.
def sla_miss_callback([1]): print(f"SLA missed for task: [1]['task_instance'].task_id")
The SLA miss callback receives a context dictionary with task and DAG info.
Fill both blanks to correctly access the task instance and send an email in the SLA miss callback.
def sla_miss_callback([1]): task_instance = [2]['task_instance'] send_email('admin@example.com', f"SLA missed for {task_instance.task_id}")
The callback receives context and accesses task_instance from it.
Fill all three blanks to define a DAG with SLA miss callback and email notification.
default_args = {
'owner': 'airflow',
'start_date': datetime(2024, 1, 1),
'email': ['admin@example.com'],
'email_on_[1]': True,
'sla_miss_callback': [2]
}
dag = DAG('sla_example', default_args=default_args, schedule_interval='@daily')
task = PythonOperator(task_id='task1', python_callable=my_func, sla=[3], dag=dag)Set email_on_sla_miss to True, assign the callback function, and define SLA as timedelta.
