簡體   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