airflow icon indicating copy to clipboard operation
airflow copied to clipboard

mark dag_run as failed internally, eg, in one of the internal tasks

Open ChristianYeah opened this issue 6 years ago • 4 comments

airflow: 1.10.10

I searched google for marking a single dag run as failed if some task logic doesn't meet with a particular condition.

for example, if task A requests the URL, the expected result should be error_code == 0. Now I manually do something like print(1/0) to raise the exception if I get error_code 1.

Is that possible to manually mark the dag_run as failed internally?

Thank you very much

ChristianYeah avatar Jun 10 '20 10:06 ChristianYeah

can we explain the requirement with clear example?

barmanand avatar Jun 19 '20 16:06 barmanand

can we explain the requirement with clear example?

import airflow
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
import requests


default_args = {
    'owner': 'airflow',
}

dag = DAG(
    dag_id='test_max_active_runs',
    default_args=default_args,
    schedule_interval=None,
    start_date=airflow.utils.dates.days_ago(1),
    max_active_runs=1,
    catchup=False
)


def example_func():
    data = requests.get("some url").json()
    if data.get('code', -1) == 0:
        #  no error in the response
        #  further process
        #  eg.
        print(data.get('results', []))
    else:
        #  error in response
        print(data.get('message', ''))
        # mark the task as failed here
        # something like below
        fail_this_task()


example = PythonOperator(
    task_id='example',
    python_callable=example_func,
    dag=dag,
)

is that possible I can fail the task dynamically if I get the response of "code" except 0?

@barmanand , thank you very much

ChristianYeah avatar Jun 23 '20 09:06 ChristianYeah

yes @ChristianYeah You can raise AirflowException and it will fail the task

barmanand avatar Jun 23 '20 09:06 barmanand

@barmanand thank you

ChristianYeah avatar Jun 23 '20 10:06 ChristianYeah