首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Airflow默认on_failure_callback

Airflow默认on_failure_callback
EN

Stack Overflow用户
提问于 2017-07-26 03:57:41
回答 2查看 23.2K关注 0票数 24

在我DAG文件中,我定义了一个on_failure_callback()函数,用于在失败的情况下发布一个Slack。

如果我在DAG中为每个运算符指定了on_failure_callback=on_failure_callback(),它就能很好地工作。

有没有办法自动(例如,通过default_args,或者通过我的DAG对象)分派给我所有的操作员?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-19 15:46:36

我终于找到了这样做的方法。

您可以将on_failure_callback作为default_args进行传递

代码语言:javascript
复制
class Foo:
  @staticmethod
  def get_default_args():
      """
      Return default args
      :return: default_args
      """

      default_args = {
          'on_failure_callback': Foo.on_failure_callback
      }

      return default_args

  @staticmethod
  def on_failure_callback(context):
     """
     Define the callback to post on Slack if a failure is detected in the Workflow
     :return: operator.execute
     """

     operator = SlackAPIPostOperator(
         task_id='failure',
         text=str(context['task_instance']),
         token=Variable.get("slack_access_token"),
         channel=Variable.get("slack_channel")
     )

     return operator.execute(context=context) 
票数 29
EN

Stack Overflow用户

发布于 2022-01-05 16:35:26

在此延迟回答,但您可以在DAG的默认值中指定on_failure_callback。您只需编写一个自定义函数,确保它可以在上下文中使用。示例:

代码语言:javascript
复制
def failure_callback(context):
    message = [
        ":red_circle: Task failed",
        f"*Dag*: {context['dag_run'].dag_id}",
        f"*Run*: {context['dag_run'].run_id}",
        f"*Task*: <{context.get('task_instance').log_url}|*{context.get('task_instance').task_id}* failed for execution {context.get('execution_date')}>",
    ]

    # Replace this return with whatever you want
    # I usually send a Slack notification here
    return "\n".join(message)


with DAG(
    ...
    default_args={
        ...
        "on_failure_callback": failure_callback,
    },
) as dag:
    ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45312439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档