Notifier 만들기
Notifier 만들기 (Creating a Notifier)
Airflow에서 알림을 보내기 위한 BaseNotifier 추상 클래스를 확장해 나만의 Notifier를 만드는 방법을 설명하는 문서예요. notify 메서드를 오버라이드하고 template_fields를 지정하는 방식으로 커스텀 알림을 구현하는 예시를 코드와 함께 살펴볼게요.
출처: 문서
본문
BaseNotifier는 Airflow에서 각종 on_*__callback을 사용해 알림을 보내는 기본 구조를 제공하는 추상 클래스예요. provider가 자신의 특정 요구에 맞게 확장·커스터마이징하도록 의도된 클래스예요.
BaseNotifier 클래스를 확장하려면 그것을 상속하는 새 클래스를 만들어야 해요. 이 새 클래스에서 알림을 보내는 자신만의 구현으로 notify 메서드를 오버라이드해요. notify 메서드는 Airflow context 단일 파라미터를 받는데, 여기에는 현재 태스크와 실행에 대한 정보가 담겨 있어요.
또한 template_fields 속성을 설정해서 어떤 속성을 템플릿으로 렌더링할지 지정할 수 있어요.
Notifier 클래스를 만드는 예시를 볼게요:
from airflow.sdk import BaseNotifier
from my_provider import async_send_message, send_message
class MyNotifier(BaseNotifier):
template_fields = ("message",)
def __init__(self, message: str):
self.message = message
def notify(self, context: Context) -> None:
# Send notification here. For example:
title = f"Task {context['task_instance'].task_id} failed"
send_message(title, self.message)
async def async_notify(self, context: Context) -> None:
# Only required if your Notifier is going to support asynchronous code. For example:
title = f"Task {context['task_instance'].task_id} failed"
await async_send_message(title, self.message)
커뮤니티가 관리하는 notifier 목록은 Notifications을 확인해 보세요.
Notifier 사용하기 (Using Notifiers)
이벤트 기반 Dag 콜백에서 Notifier를 사용하는 방법은 Callbacks을 참고하세요.