Python 气流,在发生故障时,使所有DAG执行特定的操作

Python 气流,在发生故障时,使所有DAG执行特定的操作,python,etl,decorator,airflow,Python,Etl,Decorator,Airflow,我们有很多DAG依靠气流运转。当某件事情失败时,我们希望得到通知,或者做出具体的行动:我已经通过decorator尝试过了 def on_failure_callback(f): @wraps(f) def wrap(*args, **kwargs): try: return f(*args, **kwargs) except Exception as e: return f"An exception {e} on ocurred on{f}"

我们有很多DAG依靠气流运转。当某件事情失败时,我们希望得到通知,或者做出具体的行动:我已经通过decorator尝试过了

def on_failure_callback(f):
  @wraps(f)
  def wrap(*args, **kwargs):
    try:
      return f(*args, **kwargs)
    except Exception as e:
      return f"An exception {e} on ocurred on{f}" 
  return wrap
这是可行的,但是有必要装饰任何我们想要的功能

我看到并尝试这样实现它:

def on_failure_callback(context):
    operator = PythonOperator(
        python_callable=failure)

    return operator.execute(context=context)


def failure():
    return 'Failure in the failure func'


dag_args = {
    "retries": 2,
    "retry_delay": timedelta(minutes=2),
    'on_failure_callback': on_failure_callback
}
然后在DAG定义中,我使用了[…]默认参数=DAG参数[…],但是这个选项不起作用

实现这一目标的最佳方式是什么


谢谢,最简单的方法是:如果BaseOperator的email_on_retry和email_on_failure属性为true,并且设置了airflow mail配置,则airflow在重试时发送邮件并失败

使用自定义运算符:

def on_failure_callback(context):
    # with mail:
    error_mail = EmailOperator(
        task_id='error_mail',
        to='user@example.com',
        subject='Fail',
        html_content='a task failed',
        mime_charset='utf-8')
    error_mail.execute({})  # no need to return, just execute

    # with slack:
    error_message = SlackAPIPostOperator(
        task_id='error_message',
        token=getSlackToken(),
        text='a task failed',
        channel=SLACK_CHANNEL,
        username=SLACK_USER)
    error_message.execute({})  # no need to return, just execute

dag_args = {
    "retries": 2,
    "retry_delay": timedelta(minutes=2),
    'on_failure_callback': on_failure_callback
}

最简单的方法:如果BaseOperator的“重试时发送邮件”和“重试时发送邮件”和“失败时发送邮件”属性为true,则气流发送邮件失败默认为true,并且气流邮件配置已设置

使用自定义运算符:

def on_failure_callback(context):
    # with mail:
    error_mail = EmailOperator(
        task_id='error_mail',
        to='user@example.com',
        subject='Fail',
        html_content='a task failed',
        mime_charset='utf-8')
    error_mail.execute({})  # no need to return, just execute

    # with slack:
    error_message = SlackAPIPostOperator(
        task_id='error_message',
        token=getSlackToken(),
        text='a task failed',
        channel=SLACK_CHANNEL,
        username=SLACK_USER)
    error_message.execute({})  # no need to return, just execute

dag_args = {
    "retries": 2,
    "retry_delay": timedelta(minutes=2),
    'on_failure_callback': on_failure_callback
}

IMO最简单的方法是,如果DAG失败,将其定义为默认参数

默认参数={ “所有者”:“气流”, “依赖于过去”:False, “开始日期”:日期时间2015,6,1, '电子邮件':['airflow@example.com'], “失败时发送电子邮件”:False, “重试时发送电子邮件”:False, “重试”:1, “重试延迟”:timedeltaminutes=5, “队列”:“bash_队列”, “池”:“回填”, “优先级权重”:10, “结束日期”:日期时间2016年1月1日, }


如果要根据任务相关性指定发送电子邮件的行为,也可以使用sendgrid操作符

IMO最简单的方法是,如果DAG失败,将其定义为默认参数

默认参数={ “所有者”:“气流”, “依赖于过去”:False, “开始日期”:日期时间2015,6,1, '电子邮件':['airflow@example.com'], “失败时发送电子邮件”:False, “重试时发送电子邮件”:False, “重试”:1, “重试延迟”:timedeltaminutes=5, “队列”:“bash_队列”, “池”:“回填”, “优先级权重”:10, “结束日期”:日期时间2016年1月1日, }


如果要根据任务相关性指定发送电子邮件的行为,也可以使用sendgrid操作符

你想要什么样的通知?邮件,slack还是别的?@gokmust slack,但我认为配置任何东西都会更好。你想要什么样的通知?邮件、松弛还是其他什么?@gokmustslack,但我觉得做任何事情都会更好