Airflow by default on_failure_callback

In my DAG file, I defined the on_failure_callback () function to publish Slack in case of failure.

This works well if I specify for each statement in my DAG: on_failure_callback = on_failure_callback ()

Is there a way to automate (via default_args, for example, or through my DAG object) sending to all my operators?

+4
source share
1 answer

I finally found a way to do this.

You can pass your on_failure_callback as default_args

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) 
+7
source

Source: https://habr.com/ru/post/1682258/


All Articles