How to use BranchPythonOperator with task failure?
Correct me if I'm misunderstanding how these are used.
I'm attempting to use the BranchPythonOperator using the previous task's state as the condition.
After the previous task has run, I use on_success_callback or on_failure_callback to write a file that contains the task_id that should be used.
When running the BranchPythonOperator, I check the task_id recorded in the file in order to select which branch the worker should select.
So I have a few questions:
- Is there an easier way to check the previous task's state?
- I know there is an upstream_failed state that allows the scheduler to continue with the rest of the DAG. How would I use this to make the BranchPythonOperator (and the rest of the DAG) run? Or am I misunderstanding its usage?
Thank you very much.
Airflow wasn't really designed for "conditional pathing" originally, but there are ways to do this now. Note that anything downstream any non-successful task won't run as it is today. I'm thinking about adding a OnErrorOperator that would be triggered and succeed when any directly upstream task is in error.
In the meantime your can use a disconnected ExternalTaskSensor which would wait on a task in the same DAG, and a specific state. Keep in mind that it can't have the task you are targeting as a parent if you want it to trigger. http://pythonhosted.org/airflow/code.html#airflow.operators.ExternalTaskSensor
I'm curious, what is your use case? What does the task that might fail do, and what do you do downstream if it fails?
#232 should solve your first question. Hopefully you'll be able to do something like
def branch_op_callable(**kwargs):
state = kwargs['ti'].xcom_pull(from_tasks=upstream_task_id)
# some magic here to extract state, haven't decided
# if it's the raw value or a data structure
if state == 'foo':
return 'branch 1'
else:
return 'branch 2'
One of the motivations is to avoid having to write state to files to use it downstream. Would be great to learn about your use case to make sure it handles it!
We're currently using cron to schedule our jobs and would like to migrate to a new system (Airflow is very promising!).
The jobs run scripts that are run daily. The scripts are configured to print to stdout to log error messages that cron then e-mails out. The rest of the job continues normally in order to finish as much work as possible.
It wouldn't be so hard to view the task's log in the UI, except there are over 26 of them. It would be neat to use the new Slack Operator in order to notify us of a task failure. The proposed solution would be continue writing to stdout but also track whether or not an error was logged. At the end of the script it would return zero or a non-zero exit code.
Thanks for the replies!
I'm thinking about adding a OnErrorOperator that would be triggered and succeed when any directly upstream task is in error.
Maxime did you still thinking about this functionality ? It would be very helpful ...Thank u !
@zstan : http://pythonhosted.org/airflow/concepts.html#trigger-rules
Also if you want something to run atomically with the task, there's a BaseOperator.on_failure_callback
http://pythonhosted.org/airflow/code.html#baseoperator