繁体   English   中英

出现故障时,气流会使所有的障碍者做特定的事情

[英]Airflow, on failure, make all dags do something in specific

我们有许多DAG在Airflow上运行。 发生故障时,我们希望收到通知或采取特定措施:我已经通过装饰器尝试过

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定义上,我使用[...] default_args=dag_args [...] ,但是此选项不起作用。

做到这一点的最佳方法是什么?

谢谢

最简单的方法:如果来自BaseOperator的email_on_retryemail_on_failure属性为true(默认为true),并且设置了气流邮件配置,则email_on_retry会在重试时发送邮件并失败。

使用自定义运算符:

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失败,则将其定义为默认参数。

default_args = {'owner':'airflow','depends_on_past':False,'start_date':datetime(2015,6,1),'email':['airflow@example.com'], 'email_on_failure':False, ' email_on_retry':False ,'retries':1,'retry_delay':timedelta(minutes = 5),#'queue':'bash_queue',#'pool':'backfill',#'priority_weight':10,#' end_date':datetime(2016,1,1),}

如果要基于任务依赖性指定发送电子邮件的行为,也可以使用sendgrid运算符。 https://github.com/apache/airflow/blob/master/airflow/contrib/utils/sendgrid.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM