mark dag_run as failed internally, eg, in one of the internal tasks
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
can we explain the requirement with clear example?
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
yes @ChristianYeah You can raise AirflowException and it will fail the task
@barmanand thank you