How can I get the tags in the on_dag_failure function on Airflow?

1.3k Views Asked by At

Here is my dag and task, when the task is failed it will call the on_dag_failure.

        dag = DAG('my_dag_id',
              catchup=False,
              schedule_interval='00 01 17 * *',
              description = 'My dag desc',
              default_args=default_args,
              tags=['TAG_1','TAG_2'],
    )

    run_this_0 = BashOperator(
        task_id='my_task_id',
        bash_command='some cmd',
        on_failure_callback = on_dag_failure,
        execution_timeout=None,
        dag=dag
)

In the on_dag_failure, I want to do get the tags that is define in the dag. Is there any way to get it?

def on_dag_failure(context):
    tags = dag.tags #how to get the tags from the dag
    tags_str = ""

    for tag in tags:
        tags_str += tag + " "
    
    print(tags_str)
1

There are 1 best solutions below

0
On BEST ANSWER

The models.DAG object should be available in the context variable provided to the function:

def on_dag_failure(context):
  tags_str = " ".join(context["dag"].tags)
  print(tags_str)