"""Slack notification callbacks for Airflow DAGs.""" import logging from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator from lib import constants logger = logging.getLogger('ows1') def slack_failure_callback(context): """Send Slack notification on DAG task failure.""" message = constants.SLACK_MESSAGE.format( title=':red_circle: DAG Task Failed', task=context.get('task_instance').task_id, dag=context.get('task_instance').dag_id, exec_date=context.get('execution_date'), log_url=context.get('task_instance').log_url, ) try: slack_notification = SlackWebhookOperator( task_id='slack_failure_notification', slack_webhook_conn_id=constants.SLACK_CONN_NAME, message=message, channel=constants.SLACK_NOTIFICATION_CHANNEL, ) slack_notification.execute(context=context) except Exception as e: logger.error(f'Failed to send Slack notification: {e}') def slack_success_callback(context): """Send Slack notification on DAG success.""" message = constants.SLACK_MESSAGE.format( title=':large_green_circle: DAG Completed', task=context.get('task_instance').task_id, dag=context.get('task_instance').dag_id, exec_date=context.get('execution_date'), log_url=context.get('task_instance').log_url, ) try: slack_notification = SlackWebhookOperator( task_id='slack_success_notification', slack_webhook_conn_id=constants.SLACK_CONN_NAME, message=message, channel=constants.SLACK_NOTIFICATION_CHANNEL, ) slack_notification.execute(context=context) except Exception as e: logger.error(f'Failed to send Slack notification: {e}')