Sending Slack Alerts on Airflow Task’s Failure

apache-airflow-providers-http
def slack_notification(context):
slack_msg = """
:red_circle: Task Failed.
*Task*: {task}
*Dag*: {dag}
*Execution Time*: {exec_date}
*Error Msg*: {error_message}
*Log Url*: {log_url}
""".format(
task=context.get('task_instance').task_id,
dag=context.get('task_instance').dag_id,
ti=context.get('task_instance'),
exec_date=context.get('execution_date'),
error_message=context.get('exception') or context.get('reason'),
log_url=context.get('task_instance').log_url,
)
failed_alert = SlackWebhookOperator(
task_id='slack_notification',
http_conn_id='slack_webhook',
channel='#slack-channel',
message=slack_msg)
return failed_alert.execute(context=context)
default_args = {
'owner': 'Airflow',
'depends_on_past': False,
'start_date': days_ago(2),
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(seconds=60),
'on_failure_callback': slack_notification
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store