Airflow: `'TaskInstance' object has no attribute 'task'` after retrieving task instances via DagRun.get_task_instances()

Viewed 1480

I'm running composer-1.16.6-airflow-1.10.15.

For a daily scheduled DAG, I want to write a custom on_failure_notification that only sends a notification if a task instance has failed for multiple days sequentially. My plan is to get the failed task instances of the dag run and check for each the last successful execution date:

def my_on_failure_notification(context):
    failed_tis = context["dag_run"].get_task_instances(state=State.FAILED)
    tis_to_notify_about = [ti.task_id for ti in failed_tis if ti.previous_execution_date_success < days_ago(2)]

This fails with the following trace:

[...]
File "/home/airflow/gcs/dags/xxx.py", line 94, in my_on_failure_notification
ti.task_id for ti in failed_tis if ti.previous_execution_date_success < days_ago(2)
File "/usr/local/lib/airflow/airflow/models/taskinstance.py", line 625, in previous_execution_date_success
prev_ti = self._get_previous_ti(state=State.SUCCESS)
File "/usr/local/lib/airflow/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/airflow/airflow/models/taskinstance.py", line 582, in _get_previous_ti
dag = self.task.dag
AttributeError: 'TaskInstance' object has no attribute 'task'

I assume this happens because the TIs were retrieved as SQLAlchemy models, which don't contain the task attribute. Is this intended behaviour? Is there a suggested alternative?

0 Answers
Related