简体   繁体   English

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

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

We have a lot of DAGs running on Airflow. 我们有许多DAG在Airflow上运行。 When something fails, we want to be notified, or make a specific action: I have tried via 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

This works, but it is necessary to decorate any function we want to have this behavior on. 这行得通,但是有必要修饰我们要启用此行为的任何功能。

I saw this and try to implement it like this: 我看到了这个,并尝试像这样实现它:

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
}

And then on the DAG definition, I use [...] default_args=dag_args [...] , but this option is not working. 然后在DAG定义上,我使用[...] default_args=dag_args [...] ,但是此选项不起作用。

What is the best way to accomplish this? 做到这一点的最佳方法是什么?

Thanks 谢谢

Simplest way: airflow sends mail on retry and fail if email_on_retry and email_on_failure attributes from BaseOperator are true (default true), and airflow mail configuration is set. 最简单的方法:如果来自BaseOperator的email_on_retryemail_on_failure属性为true(默认为true),并且设置了气流邮件配置,则email_on_retry会在重试时发送邮件并失败。

With custom operator: 使用自定义运算符:

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
}

The easiest way, IMO is to define it as default argument if DAG failed. 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), } 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),}

You can also use a sendgrid operator if you want to specify the behavior of sending email based on your task dependency. 如果要基于任务依赖性指定发送电子邮件的行为,也可以使用sendgrid运算符。 https://github.com/apache/airflow/blob/master/airflow/contrib/utils/sendgrid.py 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