Search code examples
pythonubuntuairflow-schedulerairflow

Airflow: How can Python script signal a failed task when run using BashOperator


When running a Python script using BashOperator of Airflow 1.10.9,

task_1 = BashOperator(
    task_id='task_1',
    bash_command='/path/to/env/python /path/to/task_1.py'
    dag=dag)

what can be done inside the Python script to signal to Airflow that this task has failed?

task_1.py

if __name__ == '__main__':
    print('Task failed!')
    # How do we tell Airflow that the `task_1` has failed?

Solution

  • Raise a Python Exception that should send exit code of 1 to BashOperator which should fail.