如何获取失败的BashOperator的退出代码?

时间:2019-11-28 15:40:29

标签: bash airflow

我可以基于this helpful tutorial为失败的流程集成Slack通知。

def task_fail_slack_alert(context):
    """
    Sends message to a slack channel.
    If you want to send it to a "user" -> use "@user",
        if "public channel" -> use "#channel",
        if "private channel" -> use "channel"
    """
    slack_channel = BaseHook.get_connection(SLACK_CONN_ID).login
    slack_token = BaseHook.get_connection(SLACK_CONN_ID).password
    failed_alert = SlackAPIPostOperator(
        task_id='slack_failed',
        channel=slack_channel,
        token=slack_token,
        text="""
            :red_circle: Task Failed. 
            *Task*: {task}  
            *Dag*: {dag} 
            *Execution Time*: {exec_date}  
            *Log Url*: {log_url} 
            """.format(
            task=context.get('task_instance').task_id,
            dag=context.get('task_instance').dag_id,
            ti=context.get('task_instance'),
            exec_date=context.get('execution_date'),
            log_url=context.get('task_instance').log_url,
        )
    )
    return failed_alert.execute(context=context)

task_with_failed_slack_alerts = BashOperator(
    task_id='fail_task',
    bash_command='exit 1',
    on_failure_callback=slack_failed_task,
    provide_context=True,
    dag=dag)

从上面的代码示例中,我可以看到我们可以使用传递给context函数的task_fail_slack_alert(context)变量来获取有关任务的信息,例如task=context.get('task_instance').task_id

我想知道我们是否可以通过包括失败的实际原因来使此松弛通知更具参考性。如果我们的bash脚本以特定的退出代码退出,这是否传递到context字典中并发送到on_failure_callback函数?

例如:

if context.get('task_instance').error_code == 2:
    message_error = 'Duplicate Key Violation'
else:
    message_error = 'Unknown error, investigate logs'

failed_alert = SlackAPIPostOperator(
        task_id='slack_failed',
        channel=slack_channel,
        token=slack_token,
        text="""
            :red_circle: Task Failed. 
            *Task*: {task}  
            *Dag*: {dag} 
            *Execution Time*: {exec_date}  
            *Log Url*: {log_url}
            *Error*: {message_error} 
            """.format(
            task=context.get('task_instance').task_id,
            dag=context.get('task_instance').dag_id,
            ti=context.get('task_instance'),
            exec_date=context.get('execution_date'),
            log_url=context.get('task_instance').log_url,
            message_error=message_error,
        )
    )
return failed_alert.execute(context=context)

1 个答案:

答案 0 :(得分:1)

来自BashOperator的{​​{3}}:

:param xcom_push: If xcom_push is True, the last line written to stdout 
  will also be pushed to an XCom when the bash command completes.
:type xcom_push: bool

编辑:有关XCom是什么以及如何使用的更多信息,请查看文档source code