Is it possible to retrieve the last successful task execution date during a task execution on airflow?

Viewed 2188

on Airflow, we currently are using the {{ prev_execution_date_success }} at the dag level to execute queries.

I was wondering if it was possible to have it by task (i.e. retrieving the last successful execution date of a task in particular and not of the whole DAG)

Thanks for your help

1 Answers

from the current DAG run you can access to the task instance and look up for the previous task in success state.

from airflow.models.taskinstance import TaskInstance
from airflow.utils.state import State
ti = TaskInstance(task_id=your_task_id,
                  dag_id=your_task_id,
                  execution_date=execution_date)
prev_task_success_state = ti.get_previous_ti(state=State.SUCCESS)

Note that get_previous_ti returns TaskInstance object thus you can access anything related to the task.

Related