5

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tsabsch
  • 2,131
  • 1
  • 20
  • 28
  • An alternative can be to query the database directly instead of using the task instances. Have you tried that? This example that I found on might be helful: https://github.com/deliverbi/Airflow_Code/blob/master/Check_previous_run.py#L50 – Alvaro Jul 14 '21 at 10:43
  • I haven't looked into workarounds yet, thanks for the suggestion. I wanted to clarify the current behaviour first. – tsabsch Jul 15 '21 at 11:26
  • 1
    Have the same issue. Getting TI from DagRun object. Is there any suggestions on how to provide a Task object instance for TI? – alaptiko Apr 19 '22 at 14:44

0 Answers0