0

How to get the execution status of overall DAG, which can be used to trigger the mail.

def on_success_dag(context):
    dag = context.get("task_instance").dag_id
    state=context.get("task_instance").state
    SUBJECT=""
    MSG = """
            SUCCESSFULLY COMPLETED THE DAG:{dag}
            DURATION:{time}
            """.format(
        dag=context.get("task_instance").dag_id,
        time=context.get("task_instance").duration,
    )
    if state == "success":
      SUBJECT="Airflow success alert for "+dag_id+" DAG"
    email.send(MESSAGE, SUBJECT)
    
# DAG DEFINITION
with DAG(
    dag_id="testing_email_for_dag",
    schedule_interval=None,
    default_args={
        "start_date": datetime(2023, 6, 20),
        "retries": 0,
        "on_execute_callback": on_trigger,
        "on_failure_callback": on_trigger,
        "on_success_callback": on_trigger,
        "catchup": False,
    },
    on_success_callback=on_success_dag,
    render_template_as_native_obj=True,
    tags=["test"],
) as dag:

@task
def print_hello():
    print("hello")

(print_hello())

Is this right way to get the details post successful execution Above all how to get the dag_status because.

1 Answers1

0

Why you need another dag status ?

Just add a task at the end of your dag to trigger email when all tasks before it are success

Code run
  • 165
  • 9