Search code examples
pythonairflowdirected-acyclic-graphs

how to get the list of all tasks along with their status for current dag run in airflow


Need help to extract the list of all tasks along with their current status [Success/Failed] for the current dag run. I have a task with a python operator which executes at the end of the workflow. The responsibility of this task is to return the no of tasks executed with the status.


Solution

  • You can create a PythonOperator that read all tasks from the dag_run.

    The xcom of task_id="tasks" is :

    enter image description here

    with DAG(
        dag_id="get_tasks",
        description="get tasks",
        schedule_interval=None,
        start_date=datetime.datetime(2021, 1, 1),
        tags=["tasks"],
    ) as dag:
        start_dag = EmptyOperator(task_id="start")
        end_dag = EmptyOperator(task_id="end")
    
    
        def get_tasks(**context):
            dagrun: DAG = context["dag_run"]
            tasks = {}
            for ti in dagrun.get_task_instances():
                tasks[ti.task_id] = ti.state
            return tasks
    
        tasks = PythonOperator(
            task_id="tasks",
            python_callable=get_tasks,
        )
    
        start_dag >> tasks >> end_dag