在Apache Airflow中,有没有一种方法可以捕获bash命令的错误?

时间:2018-10-12 14:15:34

标签: python airflow

在Apache Airflow中,是否可以捕获失败的bash命令产生的原始错误消息,而不是由Apache Airflow产生的回溯错误来捕获,该错误告诉您该行失败了,但并非确切为什么失败了?

Dag中的示例行:

gsutil_rsync = BashOperator(
        task_id="task1",
        bash_command='gsutil rsync -r s3://bucket/ gs://bucket',
        dag=dag)

1 个答案:

答案 0 :(得分:1)

我用python函数和PythonOperator编写了此解决方案,并在xcom_push=True中设置了PythonOperator

import subprocess
from datetime import datetime

from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2018, 10, 15),
    'email': 'me@airflow.com',
    'email_on_failure': False,
    'retries': 1,
    'retry_delay': 1,
}


def run_bash():
    result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
    return result.stdout

run_bash()

with DAG('bash_dag', schedule_interval="@daily", default_args=default_args) as dag:
    start_brach = DummyOperator(task_id='start')

    gsutil_rsync_py = PythonOperator(
        task_id="task1",
        python_callable=run_bash,
        xcom_push=True,
        dag=dag)


    start_brach.set_downstream(gsutil_rsync_py)

得出结果;

enter image description here