amadorschulze92
amadorschulze92

Reputation: 136

Airflow: how to force fail bash operator

I am running a series of python scripts (ex: script1.py, script2.py) in a script (ex: do_stuff.sh) which I am running using the airflow BashOperator. I was wondering if there was a way I could fail the BashOperator from within a python script if a specific condition is not met? I do not need the script itself to fail per say just the BashOperator so I can trigger a clean_up task.

script1.py:

def main(x)
    if x == 0:
        raise ValueError('BashOperator FAILS')
    else:
        print x
if __name__ == '__main__':
    import plac
    plac.call(main)

Sorry if my question is basic, I am still a newbie at airflow/scripting.

Thank you for your help!

Upvotes: 3

Views: 5799

Answers (1)

jhnclvr
jhnclvr

Reputation: 9507

In your bash command you should be able to just do:

exit 123

In this case you are exiting with error code 123, but you could use whatever error code you want.

EDIT:

Inside python, throwing the exception the way you are saying will also fail the task:

raise ValueError('This will exit bash with an error.')

Upvotes: 3

Related Questions